
Serial Port Event Listener In Java
Thanks for the code, its helped me verify/clarify some of my own. I’m currently working on the event driven model but I’m running into some runtime issues where the string is getting all jumbled up. These problems mysteriously disappear when I run the code in a debugger so I believe this is essentially a threading/timing issue – the event appears to be getting called a subsequent time before the previous event has completed processing causing all sorts of issues. I guess I’ll have to go to the other read approach or put a lock around the inputstream – I’m still not sure the later will correct the problem. I’m interested in knowing if you run into the same issue and your thoughts for a work around. Comment by arduino and rxtx guy — August 22, 2008 @. Hi Andreas, Sorry if I have to format your comment a bit.
Sandinista clash rarest pokemon. Anyway, don’t be too pessimistic. Your case is a classic example of “processing data in interrupt handler”.
This java examples will help you to understand the usage of gnu.io. If (serialPort!= null) { try { serialPort.addEventListener(serialPortEventListener); serialPort. Once the listener is implemented, it can be used to listen to particular serial port events. To do so, an instance of the listener needs to be added to the serial port. Further, the reception of each event type needs to be requested individually.
While some processing in interrupt handler is allowed, it’s not a very good practice, since you may miss the incoming interrupt. Second problem in your program is that you created a lot of thread new Thread(new Parser(this,read_str)) Some thread may come with full packet, but I bet most of it comes with partial packet data. Third problem, which is the worst, you didn’t start the thread at all. It suppose to be new Thread(new Parser(this,read_str)).start(); The fourth problem, a minor one, you should check whether it’s a DATA_AVAILABLE event or not. There might be some other event that comes from serial port (other idiots in the company may accidentally activate it, causing problem in your side).
My suggestion is change the DATA_AVAILABLE interrupt handler (or event listener as they call it in Java) just to read the data and put it in the buffer. Then when you detect the end of packet, signal the other thread to parse the buffer. Don’t create too much of thread. It will slow down the system. Just run 1-thread (other than the main one), and have it wait for the signal to start the parsing.
Hope that helps Comment by — November 14, 2008 @. I read a serial connected device. It produces data in a specified time intervals. Length of produced data is 9. Such as; it sends one packet (its length 9 characters) to com port by every 10 seconds. I use a simulator, it reads data correctly, namely simulator shows 9 length of new data in every 10 seconds.
On the other hand i developed an event based java program that uses rxtx 2.1.7 but i cannot get the correct data every time. My code like yours.
What am i missing? I want to illustrate my problem with an example; sometimes i read data part by part. At first, i read 5 length of data, then the other part of 4 length, i read. But simulator reads once everytime. And the other problem is; sometimes i read the same data again.
At first, i read 9 length of data. Then a part of this data, such as 7 length of the same data, i read again. My real problem is that. Any help is appreciated. Comment by Berat Sencer — March 17, 2011 @.