我SerialPortEvent不以连续循环使用接收数据JSSC [英] My SerialPortEvent does not receive data using jSSC in a continous loop

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

问题描述

我一直在试图用我的Arduino的乌诺串行通信,并已使用的库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.

    *This works and the correct bytes are sent by Arduino received by SerialEvent and printed to the Console*
    while(true) {
                t.write((byte) 0x41);
            }


    *But this method just stucks at this.available() which returns the size of the LinkedList,
 as in no data is actually received from the Arduino or receieved by the 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;
        }

我已经调试的程序,有时调试,程序的工作,但并非总是如此。另外,节目当我尝试并检查是否有在链表即而一些字节只被卡住(可()。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()方法为1的BYTECOUNT和100ms的超时只是为了安全起见。所以,现在的读法是这样的。

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天全站免登陆