javax.comm 似乎只发送一条消息
我正在尝试使用 javax.comm 通过串行端口从计算机与电路板进行通信。我编写了一个程序(基于 SimpleRead
和 SimpleWrite
示例),该程序将从设备接收到的任何数据并将其打印到 System.out
并将输入到 System.in
中的任何行发送到设备,后跟 \r\n
。
该设备有一个帮助功能,我用它来测试通信(发送hlp
,设备会回复它将执行的命令列表)。当我第一次发送 hlp
时,我得到了预期的响应,但第二次发送 hlp
时,我没有看到任何响应。
这是 javax.comm
的已知问题吗?或者是否有某种我需要在消息之间查找并发送到我的设备的新消息开始命令?
更多详细信息:
我使用的是运行 Windows XP 的东芝笔记本电脑。 我已将电路板连接到示波器,并且当发送第一个 hlp
时,“发送数据”测试点会改变电压(这是当我在程序中看到文本响应时)。但是“发送数据”测试点会改变电压。 data”测试点不会改变电压来响应第二条和后续消息(当我看到没有文本响应时)。因此,我非常确定问题是我的程序没有正确发送数据(而不是我的程序没有收到响应 )。
正确的阅读和写作方法 我的程序:
public void run() {
try {
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
String s = in.readLine();
s = s+"\r\n";
serialPort.getOutputStream().write(s.getBytes());
serialPort.getOutputStream().flush();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[100];
byte[] actualBytes = null;
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
actualBytes = new byte[numBytes];
for(int i =0; i < numBytes; i++){
actualBytes[i] = readBuffer[i];
}
}
System.out.print(new String(actualBytes));
} catch (IOException e) {}
break;
}
}
I am trying to communicate from my computer to a circuit board over a serial port using javax.comm
. I wrote a program (based on the SimpleRead
and SimpleWrite
examples) that will take any data received from the device and print it to System.out
and take any line typed into System.in
and send it to the device, followed by \r\n
.
The device has a help function which I am using to test communications (send hlp
and the device replies with a list of commands it will take). When I first send hlp
I get the expected response, but the 2nd time I send hlp
I see no response.
Is this a known problem with javax.comm
? Or is there some sort of start-of-new-message command I need to look up and send to my device between messages?
Further Details:
I'm using a Toshiba laptop running Windows XP.
I've hooked the board up to an oscilloscope and the "sending data" test point changes voltage when the first hlp
is sent (this is when I see a text response in my program. however the "sending data" test point does not change voltage in response to the second and subsequent messages (when I see no text response). Thus I am pretty sure that the issue is my program not sending the data properly (rather than my program not receiving the response properly).
The reading and writing methods of my program:
public void run() {
try {
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
String s = in.readLine();
s = s+"\r\n";
serialPort.getOutputStream().write(s.getBytes());
serialPort.getOutputStream().flush();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[100];
byte[] actualBytes = null;
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
actualBytes = new byte[numBytes];
for(int i =0; i < numBytes; i++){
actualBytes[i] = readBuffer[i];
}
}
System.out.print(new String(actualBytes));
} catch (IOException e) {}
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论