3
votes

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.

2
0x3F is ? in ASCII. This often indicates a character encoding problem, but since you're dealing with binary data it shouldn't be an issue. It may still be related somehow.Kayaman
there is no ASCII, only bytes. But I'l checkIgor Bloom
That's exactly what I said in my comment. However if you're consistently getting bytes converted into 0x3F (i.e. ASCII ?) I have a hard time believing it to be just a coincidence.Kayaman
No,the protocol is much simplier ibus (bmw)Igor Bloom
So why are you using Thread.sleep()? Why are you deliberately wasting time, especially when it causes a bug?user207421

2 Answers

0
votes

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();

0
votes

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