中的代码与 arduino 进行通信
我正在尝试使用http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/
使用 termios 打开并对话以非阻塞方式连接到arduino,效果很好。我想知道的是,在 Java 上使用 rxtx 时,我可以注册一个回调函数,当线路上有数据时就会调用该函数,这样我就不必手动检查。我用谷歌搜索但找不到任何有关如何注册回调函数的信息?
我在 Linux/OS X 上,我试图避免启动一个新线程只是为了观察/读取线路上的数据。
I am trying to communicate with an arduino using the code from,
http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/
which uses termios to open and talk to an arduino in a non blocking way which works fine. What I was wondering is when on Java using rxtx I can register a callback function that is called when ever there is data on the line so that I don't have to manually check. I googled but could not find any info on how to register a callback function?
I am on Linux/OS X and I am trying to avoid launching a new thread just to watch/read data on the line.
发布评论
评论(2)
Posix 本身没有定义一种方法来分配串行数据到达时调用的回调函数。如果您不想使用单独的线程,执行此类处理的标准方法是使用
select
库函数。这允许您的程序定义一组您的程序感兴趣的文件描述符,然后进入睡眠状态。如果您声明感兴趣的文件描述符之一发生了有趣的事情(例如新数据可供读取),Select 将自动唤醒您的进程。这避免了忙等待和轮询多个描述符的活动。另一种选择是使用像 libevent 这样的库,它位于 Posix 层之上并提供回调基础设施。 http://monkey.org/~provos/libevent/
Posix itself does not define a way to assign a callback function to be called when serial data arrives. The standard way to do this type of processing if you don't want to use a seperate thread is to use the
select
library function. This allows you program to define a set of file descriptors your program is interested in and then go to sleep. Select will automatically wake up your process if something interesting happens to one of the file descriptors you've declared interest in (such as new data becoming available for reading). This avoids having to busy-wait and poll multiple descriptors for activity.Another option would be to use a library like libevent which sits on top of the Posix layer and provides the callback infrastructure. http://monkey.org/~provos/libevent/
Boost.Asio可以在使用时提供回调功能串口。它在 Linux 和 Mac OS X 上运行。
Boost.Asio can provide callback functionality when using serial ports. It runs on Linux and Mac OS X.