Java SerialPort communication, wrong input bytes on receiver

自闭症网瘾萝莉.ら 提交于 2019-12-12 15:12:35

问题


When I`m sending bytes to receiver (using java), some of received byte values chaotically changing to 0x3F. (0x0 all the time changing to 0x3F)

java src:

serialPort = new SerialPort(portName);                       
  serialPort.openPort();
  serialPort.setParams(
    SerialPort.BAUDRATE_9600,
    SerialPort.DATABITS_8,
    SerialPort.STOPBITS_1,
    SerialPort.PARITY_NONE
  );
  serialPort.writeBytes(new byte[]{0,3,(byte)240,1,(byte)242});

When I`m using arduino for data trasmission, everything works fine.

Arduino src:

byte b[5]={0,3,240,1,242};
void setup() { Serial.begin(9600);}
void loop() {
  for(int i=0;i<5;i++){
    Serial.write(b[i]);
      delay(5);
  }
  delay(1000);
}

When I'm sending data from java to arduino, all bytes also looks fine.

When I'm sending data to receiver with VB or C# also - everything is fine.

When I'm sending data byte-by-byte with Thread.sleep(5) - no success.

What I`m doing wrong?

I`m using rxtx || jssc.

Current workaround - send data to receiver throught arduino. But its slow and wierd.

UPD:

After monitoring port activity: Success packet has couple WAIT_ON_MASK functions (up,down) Then in one function IRP_MJ_READ got all packet with proper bytes.

When I`m trying to send data with my java code, there is a lot SERIAL_GET_COMMSTATUS (up/down) functions, then IRP_MJ_READ with first 3 bytes converted to 0x3F, then again WAIT_ON_MASK & COMMSTATUS, and then remaing data byte-by-byte.

P.s. Receiver is an old car bc.


回答1:


use jserialComm.jar file and arduino.jar file to communicate directly with serial monitor (google search or directly downlord from link https://youtu.be/GLX21Bf2kXI)

Step 1 : upload code in arduino

step 2 : download 2 jar files 1. jSerialComm 2. arduino

step 3 : add this 2 jar files to your project and import arduino.* e.g. import arduino.*;

step 4 : create a new object of arduino by writing Arduino aa=new Arduino();

step 5 : initilise a port to the object of arduino bu using setPortDescription() method and pass a string of perticular port e.g. aa.setPortDescription("COM4"); "/dev/tty.usbserial-A9007UX1", // Mac OS X "/dev/ttyACM0", // Raspberry Pi "/dev/ttyUSB0", // Linux "COM3"or"COM4" // Windows

step 6 : open connection for communication by using openConnection(); method e.g. aa.openConnection();

step 7 : to write or send command use method serialWrite(String); e.g. aa.serialWrite("1"); //you can also use scanner function

step 8 : to read command from serial use serialRead(); method e.g. aa.serialWrite(); this will return a string

step 9 : close the connection by using closeConnection() method e.g. aa.closeConnection();




回答2:


In my case SerialPort.PARITY_EVEN (using jssc) solve the problem



来源:https://stackoverflow.com/questions/46399615/java-serialport-communication-wrong-input-bytes-on-receiver

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!