Java:串行端口传入数据上的观察者或同步通知()

发布于 2024-09-30 23:00:23 字数 408 浏览 1 评论 0原文

我有一个有数据传入的串行端口。我使用串行端口事件侦听器实现了与 RXTX 库的串行端口连接。因此,只要串行端口中有可用的传入数据,同步函数serialEvent(SerialPortEvent oEvent)就会运行。基本上,随着数据不断传入,程序将处于无限循环中。我将传入的数据组织成一个整数数组。现在我希望与其他类共享该数组,这些类将使用该数组来执行不同的任务。我可以说,随着串行事件侦听器在循环中运行并且数组不断变化,我希望所有其他类共享这个更改后的数组数据。我之前学过一些关于 java.util.observables 的知识。而且当我在网上搜索时,我看到其他人使用sychronized()和notify()/notifyAll()在线程之间共享变量。

我不确定其中哪一个最好用?如果它们都适用于这种情况,那么我需要注意的最重要的部分是什么?还有其他方法可以达到我想要的吗?

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 tasks. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

≈。彩虹 2024-10-07 23:00:23

如果没有有关您的应用程序的其他信息,很难判断。当你说“其他类”时,你的意思是“其他线程”吗?

如果您的其他类是线程,并且您希望它们等待新数据,则可以让它们 wait() 并让串行端口事件侦听器 notifyAll() 每当有新数据时变得可用。这将唤醒正在等待的线程。但请注意,这只能解决通知问题:您仍然需要处理对数据数组的并发访问,因为在其他线程访问新数据时可能会到达新数据。

另一方面,如果您使用 Observable,那么当您调用 Observable.notifyObservers() 时,这将导致 update() 方法每个被调用的 Observer 都在当前线程的上下文中(调用 Observable.notifyObservers() 的线程)。如果您希望其他线程访问数据,您仍然需要某种方式来通知它们(例如,您的 update() 方法可能最终实际调用 notify() )。

如果您提供有关您正在尝试执行的操作的更多信息,我们也许能够为您提供进一步帮助。

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.

风苍溪 2024-10-07 23:00:23

很抱歉我的问题不清楚。我应该给出一些我正在使用的代码。请参考以下链接:

http://www.arduino.cc/playground/Interface/Java< /a>

基本上,这个示例代码与那些经典的 rxtx 示例不同,通过在某些函数上声明“synchronized”关键字,在串行端口上进行基于事件的通信。我正在处理的唯一部分是在串行事件侦听器中:

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

功能 A:使用此共享数组中的数据执行 2D/3D 图形的小程序。

功能 B:GUI 将从共享数组计算出的一些数字输出/打印到 jframe 中的文本区域。它还可以通过按钮打开功能 A 小程序。但是GUI中的一些按钮会更改函数A中的一些变量,从而导致函数A处理共享数组的不同行为。

这就是我想要的:

随着串行数据进入并解析到数据数组中......

1)函数B将立即打印与该数据数组相关的数字。只有当用户单击按钮显示功能 A 小程序时,功能 A 才会显示 2D/3D 图形。

2) 当功能 A 和 B 都激活时,它们各自的变量会随着传入的串行数据而改变。

我希望函数 A 和 B 在串行事件()中发生更改时立即捕获共享数据数组中的更改。我知道最简单的方法是将数据数组作为引用传递,并在下一个传入数据可用之前调用每个函数。但存在前面Grodriguez提到的问题。当功能 A 或 B 正在进行时,可能会丢失一些传入数据。几年前,我在 Java 中做了一些关于 MVC 的工作,它使用不同类或 GUI 之间的可观察量来显示温度。就像当我在文本框中输入温度一样,另一个带有滑块的 GUI 将更改为输入温度。当我更改此处的滑块时,它将更改另一个 GUI 中的温度数字。这个系列事件让我想起了这里的这个应用程序。我不确定这是否是在这里应用的最佳方法,或者可能还有其他更好、更简单的方法来完成我想要的事情。

再次感谢您的耐心等待。

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.

半衬遮猫 2024-10-07 23:00:23

你的问题非常不清楚,但我可以给你一条一般性建议。

使用串行端口对象的简单方法是打开输入或输出流并仅读取/写入字节。事件内容适用于您不希望线程阻塞等待读取数据的情况。

我建议您仔细阅读这些 RXTX 示例,尤其是说明该流的前两个示例和基于事件的方法。

如果这没有帮助,请使用一些 Java 代码或伪代码来更新问题,以解释您要执行的操作。

跟进

从你的“答案”来看,你似乎误解了synchronized的作用。简而言之,它只是阻止两个线程同时执行某些代码区域。

您的问题还涉及一个线程以某种方式告诉另一个线程新数据已到达并需要处理。而且,您还遇到一个问题,即必须在第一个线程用下一个数据块覆盖数据之前处理数据。

我建议您首先阅读有关 并发 的 Java 教程流。这可能不会直接解决您的问题,但它应该让您开始以正确的方式思考问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文