ascallonisi ascallonisi - 23 days ago 5
Java Question

Java Arduino Serial Read

I'm making a Serial to MIDI interface in Java, with the serial data coming from an Arduino and being processed through the RXTX library.
Unfortunately, using the suggested code from Arduino website link, I'm unable to retrieve the serial data.
The example code uses

Serial.println
on the Arduino side and
BufferedReader.readLine()
on the Java side. In my codeI'm not writing a line through Serial.println, but a MIDI message.

Arduino:

void MIDI_TX(unsigned char MESSAGE, unsigned char PITCH, unsigned char VELOCITY)
{
status = MESSAGE + midichannel;
Serial.write(status);
Serial.write(PITCH);
Serial.write(VELOCITY);
}


I tried reading the serial data on a char buffer:

Java

char buf[] = new char[3];
reader.read(buf);
int channel = (int)buf[0] ;
int key = (int)buf[1] ;
int vel = (int)buf[2] ;


but I'm getting weird data...

Answer

SOLVED: It was just an error in the port.setSerialPortParams, I was writing at baudrate 115200 but reading at 9600.