-
Notifications
You must be signed in to change notification settings - Fork 313
Description
Hi there,
My aim is to connect via rs232 a control cabinet from an engine of a ferry. The software part was implemented by a java service and jSerialComm library.
To get data from this control cabinet there must be a request defined by the following 8 bytes:
byte[] SEND_DATA = new byte[]{(byte) 0x01, (byte) 0x03, (byte) 0x00, (byte) 0x35, (byte) 0x00, (byte) 0x01, (byte) 0x94, (byte) 0x04};
The response is always of length 7 and has the following structure 01 03 02 XX XX YY YY with XX XX = numeric value and YY YY as a CRC.
Before I have implemented my service I have tested the connection via a standard Serial-Tool. As you can see the request is defined by 8 bytes, the response contains 7 bytes.:
Here are the technical parameters for this connection:
Now, in Java:
My instantiation of the serialPort is done by:
SerialPort[] serialPorts = SerialPort.getCommPorts();
if (serialPorts.length == 0) {
Thread.sleep(5000);
throw new NoPortException("No ports are found");
}
log.info("Found {} ports:", serialPorts.length);
serialPort = serialPorts[0]; // There should be always just one port -> So take [0]
log.info("Open port: {}", serialPort.getSystemPortName());
serialPort.setComPortParameters(9600, 8, 1, SerialPort.NO_PARITY);
serialPort.openPort();
serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING, 50, 0);
and the trigger loop looks like:
while (true) {
byte[] bytes = new byte[7];
serialPort.writeBytes(Main.SEND_DATA, Main.SEND_DATA.length);
log.info("Write bytes ({}): {}", Main.SEND_DATA.length, bytesToHex(Main.SEND_DATA));
int numRead = serialPort.readBytes(bytes, bytes.length);
log.info("Read bytes ({}): {}", numRead, bytesToHex(bytes));
//DO something
Thread.sleep(1000);
}
If I take a look into my logging file, I can see the following:
The first request and response were fine. Starting from the second request (and all requests afterwards) the response (read bytes) are just random or empty.
Is there anything missing at my code to get instantly the correct response (like any flush at read/write, ...)?
Based on the standard serial tool, I can click "Send" as fast as I can and the responses are fine.
Thanks for any advice.