我有一个通过 com 端口连接到 PC 的账单验证器。我尝试向票据验证器发送数据并接收输出(票据验证器通过 ccnet 协议进行通信)这是我的代码 -
public static void main(String[] args) throws SerialPortException {
String s[]=SerialPortList.getPortNames();
for (String x :s){
System.out.print(x);
SerialPort serialPort=new SerialPort(x);
}
try {
SerialPort serialPort=new SerialPort("COM4");
serialPort.openPort();
serialPort.setParams(9600, 8, 1, SerialPort.PARITY_NONE);
serialPort.writeString("RESET");
serialPort.setEventsMask(SerialPort.MASK_RXCHAR);
serialPort.addEventListener(new list());
System.out.println(serialPort.readBytes(5));
} catch (SerialPortException sex){
System.out.print(sex);
}
串口事件监听器:
private static class list implements SerialPortEventListener{
@Override
public void serialEvent(SerialPortEvent spe) {
if (spe.isRXCHAR()&&spe.getEventValue()>0){
try {
String data =s erialPort.readString(spe.getEventValue());
} catch (SerialPortException ex) {
Logger.getLogger(Terminal.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
所以我得到串口异常端口很忙,我该如何解决?