我使用jSerialComm库从Arduino获取数据。如何跟踪与Arduino的通信中断并在可能的情况下将其恢复?以下是与SerialPort相关的部分代码。
SerialPort comPort = SerialPort.getCommPorts()[0];
comPort.openPort();
comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
final Timer t = new Timer(100, null);
t.addActionListener(new ActionListener() {
int countBanana = 0;
int CheckLife=0;
int b;
int c;
String s = null;
@Override
public void actionPerformed(ActionEvent e) {
try {
{
while (comPort.bytesAvailable() == 0) {
try {
Thread.sleep(20);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
byte[] readBuffer = new byte[comPort.bytesAvailable()];
int numRead = comPort.readBytes(readBuffer, readBuffer.length);
System.out.println("Read " + numRead + " bytes.");
System.out.println(String.valueOf((char) readBuffer[0])+String.valueOf((char) readBuffer[1])+String.valueOf((char) readBuffer[2]));
c = Integer.parseInt(String.valueOf((char) readBuffer[0])+String.valueOf((char) readBuffer[1])+String.valueOf((char) readBuffer[2]));
}}
catch (Exception m){}
if(c<101){
b = c;
}