我的 SerialPortEvent 没有在连续循环中使用 jSSC 接收数据 [英] My SerialPortEvent does not receive data using jSSC in a continous loop

查看:16
本文介绍了我的 SerialPortEvent 没有在连续循环中使用 jSSC 接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用与我的 Arduino Uno 的串行通信,并使用了 jSSC-2.6.0 库.我正在使用 SeriaPortEvent 侦听器从串行端口 (Arduino) 接收字节并将它们存储在链表中.

I have been trying to use serial communication with my Arduino Uno and have used the library jSSC-2.6.0. I am using a SeriaPortEvent listener to receive bytes from the Serial Port (Arduino) and store them in a linked list.

public synchronized void serialEvent(SerialPortEvent serialPortEvent) {
    if (serialPortEvent.isRXCHAR()) { // if we receive data
        if (serialPortEvent.getEventValue() > 0) { // if there is some existent data
            try {
                byte[] bytes = this.serialPort.readBytes(); // reading the bytes received on serial port
                if (bytes != null) {
                    for (byte b : bytes) {
                        this.serialInput.add(b); // adding the bytes to the linked list

                        // *** DEBUGGING *** //
                        System.out.print(String.format("%X ", b));
                    }
                }           
            } catch (SerialPortException e) {
                System.out.println(e);
                e.printStackTrace();
            }
        }
    }

}

现在,如果我在循环中发送单个数据并且不等待任何响应,serialEvent 通常会将收到的字节打印到控制台.但是如果我尝试等到链表中有一些数据,程序就会继续循环,而 SerialEvent 永远不会向 LinkedList 添加字节,它甚至不会注册任何接收到的字节.

Now if I send individual data in a loop and don't wait for any response the serialEvent usually prints bytes received, to the Console. But If I try and wait till there is some data in the linked list the program just keeps on looping and the SerialEvent never adds bytes to the LinkedList, it even does not even register any bytes being received.

这是有效的,正确的字节由 Arduino 发送,由 SerialEvent 接收并打印到控制台:

while(true) {
    t.write((byte) 0x41);
}

但是这个方法只是停留在 this.available() 上,它返回 LinkedList 的大小,因为实际上没有数据从 Arduino 接收到或由 serialEvent 接收:

public boolean testComm() throws SerialPortException {
    if (!this.serialPort.isOpened()) // if port is not open return false
        return false;

    this.write(SerialCOM.TEST); // SerialCOM.TEST = 0x41

    while (this.available() < 1)
        ; // we wait for a response

    if (this.read() == SerialCOM.SUCCESS)
        return true;
    return false;
}

我已经调试了程序,有时还进行了调试,程序确实可以运行,但并非总是如此.此外,只有当我尝试检查链表中是否有一些字节时,程序才会卡住,即 while(available() <1).否则,如果我不检查,我最终会收到来自 Arduino 的正确字节响应

I have debugged the program and sometimes debugging, the program does work but not always. Also the program only gets stuck when i try and check if there is some bytes in the linkedlist i.e while(available() < 1). Otherwise if I dont check I eventually receive the correct response of bytes from Arduino

推荐答案

浪费了 4 个小时,我自己找到了答案.为了安全起见,我最好使用 readBytes() 方法,byteCount 为 1,timeOut 为 100 毫秒.所以现在读取方法看起来像这样.

Found the answer myself after wasting 4hours. I was better off using the readBytes() method with a byteCount of 1 and timeOut of 100ms just to be on the safe side. So now the read method looks like this.

    private byte read() throws SerialPortException{
    byte[] temp = null;
    try {
        temp = this.serialPort.readBytes(1, 100);
        if (temp == null) {
            throw new SerialPortException(this.serialPort.getPortName(),
                    "SerialCOM : read()", "Can't read from Serial Port");
        } else {
            return temp[0];
        }
    } catch (SerialPortTimeoutException e) {
        System.out.println(e);
        e.printStackTrace();
    }
    return (Byte) null;
}

这篇关于我的 SerialPortEvent 没有在连续循环中使用 jSSC 接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆