javax.comm 似乎只发送一条消息

发布于 2024-11-26 11:32:24 字数 2082 浏览 1 评论 0原文

我正在尝试使用 javax.comm 通过串行端口从计算机与电路板进行通信。我编写了一个程序(基于 SimpleReadSimpleWrite 示例),该程序将从设备接收到的任何数据并将其打印到 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文