Java RXTX问题-接收的字节数太多

Java RXTX问题-接收的字节数太多,java,serial-port,inputstream,rxtx,bufferedinputstream,Java,Serial Port,Inputstream,Rxtx,Bufferedinputstream,我在从微控制器检索数据时遇到了一些困难。我正在以2000字节的数据块传输数据,并编写了一个线程来处理这2000字节,然后再进行新调用以发送下一个2k字节。在大多数情况下,它工作得很好,但有时由于某种原因,我倾向于得到一个字节太多,或者一个字节太少,这只是在案例2中。如果我使用案例1,它总是完美无瑕地工作,但由于某些原因,它非常缓慢。我们说的是10秒内大约2000个字节,当我将serialport设置为115.200波特时,速度太慢了 案例1始终有效,但速度非常慢 public void seri

我在从微控制器检索数据时遇到了一些困难。我正在以2000字节的数据块传输数据,并编写了一个线程来处理这2000字节,然后再进行新调用以发送下一个2k字节。在大多数情况下,它工作得很好,但有时由于某种原因,我倾向于得到一个字节太多,或者一个字节太少,这只是在案例2中。如果我使用案例1,它总是完美无瑕地工作,但由于某些原因,它非常缓慢。我们说的是10秒内大约2000个字节,当我将serialport设置为115.200波特时,速度太慢了

案例1始终有效,但速度非常慢

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:

            try
            {
                byte singleData = (byte) buffer.read();
                gasArray.setMessage(singleData);
                    if (gasArray.isBusy())
                    {
                        gasArray.setProcessing();
                        while (gasArray.isProcessing())
                        {
                            continue;
                        }
                    }
                    else
                        gasArray.appendChat("Incoming data: " + singleData);
                }
        }
案例2有时会卡住,但速度非常快

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:

            try
            {
                while (buffer.available() > 0)
                {
                    byte singleData = (byte) buffer.read();
                    gasArray.setMessage(singleData);
                    if (gasArray.isBusy())
                    {
                        gasArray.setProcessing();
                        while (gasArray.isProcessing())
                        {
                            continue;
                        }
                    }
                    else
                        gasArray.appendChat("Incoming data: " + singleData);
                }
        }
还有另一个工作线程处理传入的数据并执行一些操作,这不是同步问题或类似的问题。归根结底,要么得到过多的一个字节,要么得到一到几个字节,这会导致我计算字节数的线程陷入困境,期望多得到一个字节。我使用RealTerm作为一个串行控制台程序来检索相同的内容,它每次都能快速准确地检索。在添加BufferedInputStream时,案例2的效果似乎更好,但问题仍然偶尔发生

我的问题是: 可用的方法真的那么不可靠而导致这些问题吗?或者这是串行通信或RXTX库的问题? 有没有更好的处理方法?检索2000个字节,处理它们,然后再请求2000个字节。 在串行端口上接收数据的速度是否应该如此之慢


任何有例子的想法都会很有帮助。

在案例1中,你应该读入字节数组并处理你得到的所有数据

在99%的情况下,可用的方法没有多大用处,当从已经告诉您数据已准备好的输入设备读取数据时,当然也没有多大用处