0

I am writing a serial port communication application in Java using a library called jSSC (java-simple-serial-connector) with an external device.

After sending a message and waiting for its return, I have a timeout that will abort the reading if there is no reading after certain time (2 second). If that happens, the port is closed and re-opened for further message exchange.

What I noticed is that, if for some reason, the timeout happens, and the port is closed and reopened, further message reading from the port will be interfered (i.e., the message read cannot be decoded). It looks as if previous message from the device is still on the line and continue to come in for the next reading operation.

I'm not very familiar with serial port so I'm not totally sure if this is the case. I would think (but may well be wrong) the previous message sending by the other part is discarded after I close the connection from my side (as in a TCP/IP communication).

Can anyone shed some light on this topic as to how exactly the serial port behave after close/re-open? Is it possible for the other part (a device) to continue sending the old message even after I close the connection in the application?

Wudong
  • 2,320
  • 2
  • 32
  • 46
  • hai can u pls answer my question in this link http://stackoverflow.com/questions/33561947/close-my-ports-programatically-using-netbeans – Sruthi Acg Nov 18 '15 at 11:46

2 Answers2

1

In your case I think in case of time out you should perform a purge then closing Port before you try to reopen it again.

if (serialPort! = null && serialPort.isOpened ()) {
  serialPort.purgePort (1);
  serialPort.purgePort (2);
  serialPort.closePort ();
} 
Ahmed Gawad
  • 135
  • 1
  • 10
  • thanks for ur input. can you explain a bit more? what purgePort do? – Wudong Sep 22 '15 at 13:21
  • Purging means cleaning any data in the port buffer. an other point: may be this garbage data is coming from your device when it is disconnected it keeps some data in its IO buffers then when it reconnected to PC serial port it shoots its load to the PC so you would face this garbage data. – Ahmed Gawad Sep 22 '15 at 20:21
0

You should purge the port not at the time of closing but at the time when serial port is opened. Operating systems and drivers maintain their own buffers for serial port data. When purge is called it actually calls flush function of operating system (and driver) and that is where data received previously gets discarded (deleted). Also try to use any other library like serial communication manager.

samuel05051980
  • 369
  • 2
  • 2