Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JAVA和jssc库从Arduino串行端口读取字符串_Java_Arduino_Serial Port_Jssc - Fatal编程技术网

使用JAVA和jssc库从Arduino串行端口读取字符串

使用JAVA和jssc库从Arduino串行端口读取字符串,java,arduino,serial-port,jssc,Java,Arduino,Serial Port,Jssc,字符串串行输入必须包含序列号卡RFID(MIFARE),如A45F45A7(8字节)。有时,当我接近arduino的RFID读卡器时,字符串类似于A45F45(截断),丢失任何字符。有比while循环更好的解决方案吗?(更加优雅和高效)使用Arduino IDE串行监视器,卡的序列号是正确的 public static void connectionToCom(SerialPort serialPort, ComboBox<String> cbxComPort, TextArea t

字符串串行输入必须包含序列号卡RFID(MIFARE),如A45F45A7(8字节)。有时,当我接近arduino的RFID读卡器时,字符串类似于A45F45(截断),丢失任何字符。有比while循环更好的解决方案吗?(更加优雅和高效)使用Arduino IDE串行监视器,卡的序列号是正确的

public static void connectionToCom(SerialPort serialPort, ComboBox<String> cbxComPort, TextArea txaMessages) throws SerialPortException
{       
    int baudrate = 9600; int databits = 8; int stopbits = 1; int parity = 0;

    serialPort.openPort() ;
    serialPort.setParams(baudrate, databits, stopbits, parity) ;

    String Serial_Input = null;

    try {
        while (true)
        {
            if (serialPort.readString() != null)
            {
                Serial_Input = serialPort.readString(8);

                System.out.println("Card Serial: " + Serial_Input + "\n");
                //serialPort.closePort();
            }
        }
    } 
    catch (SerialPortException ex){
        txaMessages.appendText(ex.toString());
    }
}
public static void connectioncom(SerialPort SerialPort、ComboBox cbxComPort、TextArea txaMessages)抛出SerialPortException
{       
int波特率=9600;int数据位=8;int停止位=1;int奇偶校验=0;
serialPort.openPort();
serialPort.setParams(波特率、数据位、停止位、奇偶校验);
字符串串行输入=空;
试一试{
while(true)
{
if(serialPort.readString()!=null)
{
串行输入=serialPort.readString(8);
System.out.println(“卡序列:+Serial_Input+”\n”);
//serialPort.closePort();
}
}
} 
捕获(SerialPortException例外){
appendText(例如toString());
}
}

您可以使用addEventListener方法(SerialPortEventListener listener,int-mask)。每当您通过serialPort接收字节时,它就会调用回调方法

不完整字符串的问题可能是2个问题

  • 代码在接收整个字符串之前执行。要解决这个问题,您必须添加一个代码来验证您正在接收的字符串的长度

  • 您正在使用readString两次。第一次使用时可能会丢失一些字节的字符串