简体   繁体   中英

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...

已解决:这只是port.setSerialPortParams的错误,我的波特率为115200,但读取值为9600。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM