字符串 Serial_Input 必须包含一个序列号卡 RFID (MIFARE),如 A45F45A7(8 字节)。有时当我将卡靠近 arduino 的 RFID 阅读器时,字符串就像这个 A45F45(截断),错过任何字符。有比while循环更好的解决方案吗?(更优雅高效)使用 Arduino IDE Serial Monitor 卡的序列号是正确的。
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());
}
}