开发者

Java: Observer or synchronized+notify() on a serial port incoming data

I have a serial port with data coming in. I implemented the serial port connection with RXTX library with a serial port event listener. So whenever there is incoming data available in the serial port, the synchronized function serialEvent(SerialPortEvent oEvent) will run. Basically the program will be in a infinite loop as data coming in continuously. I organized the incoming data into an array of integer. Now I want this array to be shared with my other classes that will take this array to do different task开发者_运维知识库s. I can say, as the serial event listener running in a loop and the array keeps changing, I want all other classes to share this changed array data. I have learnt something about java.util.observables before. And as I search online, I see other people using sychronized () and notify()/notifyAll() to share a variable between threads.

I am not sure which one of them is the best to use? If they both work in this case, what are the most important part I need to be aware of? Is there any other way still achieve what I want?


It's kind of difficult to tell without additional information about your application. When you say "other classes", do you mean "other threads" ?

If your other classes are threads and you want them to wait for new data, you can have them wait() and have your serial port event listener notifyAll() whenever new data becomes available. This will awake the threads that are waiting. But note that this only solves the notification: You will still need to handle concurrent access to the data array, since it is possible that new data arrives while the other threads are accessing it.

On the other hand, if you use Observable, then when you call Observable.notifyObservers(), this will result in the update() method of each Observer being invoked, all in the context of the current thread (the thread that is calling Observable.notifyObservers()). If you want other threads to access the data, you will still need some way to notify them (for example, your update() method might end up actually calling notify()).

If you provide more information about what you are trying to do, we might be able to help you further.


I'm sorry that my question was not clear. I should have given some code which I'm working with. please refer to the following link:

http://www.arduino.cc/playground/Interfacing/Java

Basically this sample code is different from those classic rxtx examples with event based communication on serial port by declaring "synchronized" keyword on some functions. The only part which i am working on is within the serial event listener:

public synchronized void serialEvent(SerialPortEvent oEvent) {

    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {
            ////////////////////////////////////
            1) I will parse the incoming data into an integer array
            2) this array will be shared with other classes, let me
              explain some of them below.....
            ////////////////////////////////////
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
}

Function A: an applet doing 2D/3D graphic using the data in this shared array.

Function B: a GUI will output/print some numbers calculated from the shared array to a textarea in a jframe. It can also open up Function A applet with a button. But there are some buttons in the GUI will change some variables in Function A to cause different behavior to process the shared array by Function A.

This is what I want:

As the serial data coming in and parsed into the data array....

1)Function B will immediately print the numbers associated with this data array. Only if the user click the button to show up the Function A applet, Function A will display 2D/3D graphic.

2) When Function A and B are both active, their own variables are subject to change along with the incoming serial data.

I want Function A and B catch the change in the shared data array right away as it is changed in the serialEvent(). I know the most simplest way is to pass the data array as an reference and call each function before the next incoming data become available. But there is the problem mentioned previously by Grodriguez. There might be some lost incoming data while Function A or B is in progress. I did something about MVC in java years ago, and it was using obervables between different classes or GUIs to display temperature. Like when I enter a temperature in the textbox, the other GUI with the slider will change to the input temperature. And as I changed the slider here, it will change the temperature in number in the other GUI. The serial event reminds me of this application here. I am not sure whether it's the best method to apply here or there may be other better and easier ways to do exactly what I want.

Thank you for your patience again.


Your question is terribly unclear, but I can give you one piece of general advice.

The simple way to use a serial port object is to open an input or output stream and just read / write bytes. The event stuff is intended for cases where you don't want a thread to block waiting to read data.

I suggest you read through these RXTX examples, especially the first two that illustrate the stream and event based approaches.

If this doesn't help, update the question with some Java code or pseudo-code that explains what you are trying to do.

FOLLOW UP

Judging from your "answer", you seem to misunderstand what synchronized does. Simply stated, it just stops two threads from executing certain regions of code at the same time.

Your problem also involves one thread somehow telling another thread that new data has arrived and needs to be processed. And, you have also got the problem that the data has to be processed before the first thread overwrites it with the next chunk of data.

I recommend that you start by reading the Java Tutorial stream on Concurrency. This probably won't directly address your problem, but it should get you started in thinking about the problem in the right way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜