2

我正在尝试使用javax.comm. 我编写了一个程序(基于SimpleReadSimpleWrite示例),它将获取从设备接收到的任何数据并将其打印到System.out并获取输入的任何行System.in并将其发送到设备,然后是\r\n.

该设备有一个帮助功能,我正在使用它来测试通信(发送hlp并且设备回复它将执行的命令列表)。当我第一次发送时hlp,我得到了预期的响应,但第二次发送时hlp我没有看到任何响应。

这是一个已知问题javax.comm吗?或者是否有某种新消息开始命令我需要在消息之间查找并发送到我的设备?

更多详细信息:

我正在使用运行 Windows XP 的东芝笔记本电脑。我已经将电路板连接到示波器,并且“发送数据”测试点在hlp发送第一个时会改变电压(这是我在程序中看到文本响应时。但是“发送数据”测试点不会改变电压响应第二条和后续消息(当我没有看到文本响应时)。因此我很确定问题是我的程序没有正确发送数据(而不是我的程序没有正确接收响应)。

我的程序的读写方法:

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;
    }
}
4

0 回答 0