使用JAVA和jssc库从Arduino串行端口读取字符串
字符串串行输入必须包含序列号卡RFID(MIFARE),如A45F45A7(8字节)。有时,当我接近arduino的RFID读卡器时,字符串类似于A45F45(截断),丢失任何字符。有比while循环更好的解决方案吗?(更加优雅和高效)使用Arduino IDE串行监视器,卡的序列号是正确的使用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
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个问题