问题描述
我不知道我的代码发生了什么.我没有收到任何错误,也没有回应.我正在将数据写入串行端口并通过激活 port.notifyOnDataAvailable(true);
等待响应,但未触发此事件并且 inputstream.available() 始终返回 0.可能有什么问题?我在 linux 中使用 RXTX.
I have no idea of what is happening to my code. i am getting no errors and no response as well. I am writing the data to the serialport and waiting for the response by activating port.notifyOnDataAvailable(true);
but this event is not triggered and inputstream.available() returns 0 always. What might be wrong ? I am using RXTX in linux.
编辑
我在 main 方法上打开端口,并在应用程序内的按钮单击事件上发送消息.
I am opening the port on main method and sending the message on a button click event inside the application.
推荐答案
.available()
不能用于进程间通信(包括串口),因为它只检查是否有数据在当前进程中可用(在输入缓冲区中).
.available()
can not be used in inter-process communication (serial included), since it only checks if there is data available (in input buffers) in current process.
在串口通信中,当你发送一个messaga,然后立即调用available()
,你会得到0,因为串口还没有回复任何数据.
In serial communication, when you send a messaga and then immediately call available()
you will mostly get 0 as serial port did not yet reply with any data.
解决方法是在单独的线程中使用阻塞read()
(用interrupt()
来结束):
The solution is to use blocking read()
in a separate thread (with interrupt()
to end it):
线程中断未结束对输入流的阻塞调用阅读
这篇关于inputstream.available() 始终为 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!