我正在尝试将字节序列发送到串行端口,但我无法从端口本身读取回复。
我正在使用Realterm发送相同的消息来处理串口并收到回复。我也正在使用Eltima的串口监视器进行监视,并且写入操作是成功的,但我无法阅读任何内容
我正在使用JFrame接口,并且我声明了一个名为SerialDevice的单独类,它使用其方法(取自jssc)实现串行端口作为属性。
串口应使用57600,8位数据,1个停止位和RTS
似乎RXCHAR()事件永远不会发生......
public class SerialDevice {
private SerialPort serialPort;
public SerialDevice(String pname){
serialPort = new SerialPort(pname);
}
public void open() throws SerialPortException{
//Open port
serialPort.openPort();
}
public void setParameters() throws SerialPortException{
// We expose the settings. You can also use this line:
// serialPort.setParams(9600, 8, 1, 0);
serialPort.setParams(SerialPort.BAUDRATE_57600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
//serialPort.setRTS(true);
serialPort.setFlowControlMode(
SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT
);
}
public void close()throws SerialPortException{
//Close Port
serialPort.closePort();
}
public void SendGetInfo() throws SerialPortException{
byte[] ms = { (byte)0x01, (byte)0x80, (byte)0x04, (byte)0x00, (byte)0x2A,
(byte)0x81, (byte)0x00, (byte)0x00 };
serialPort.writeBytes(ms);
}
public void readOnEvent() throws SerialPortException{
// Prepare mask:
int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS
+ SerialPort.MASK_DSR;
// Set mask:
serialPort.setEventsMask(mask);
// Add SerialPortEventListener:
serialPort.addEventListener(new SerialPortReader());
}
/*
* In this class must implement the method serialEvent, through it we learn
* about events that happened to our port. But we will not report on all
* events but only those that we put in the mask. In this case the arrival
* of the data and change the status lines CTS and DSR
*/
public class SerialPortReader implements SerialPortEventListener {
public void serialEvent(SerialPortEvent event) {
if (event.isRXCHAR()) { //If data is available
try {
System.out.println("OK");
byte buffer[] = serialPort.readBytes(event.getEventValue());
System.out.println(buffer);
} catch (SerialPortException ex) {
System.out.println(ex);
}
}
else if (event.isCTS()) { //If CTS line has changed state
if(event.getEventValue() == 1){//If line is ON
System.out.println("CTS - ON");
}
else {
System.out.println("CTS - OFF");
}
}
else if(event.isDSR()){ //If DSR line has changed state
if(event.getEventValue() == 1){//If line is ON
System.out.println("DSR - ON");
}
else {
System.out.println("DSR - OFF");
}
}
}
}
以下代码是鼠标按键动作
的一部分 try{
boolean availablePorts = false;
String[] portNames = SerialPortList.getPortNames();
for(int i = 0; i < portNames.length; i++){
if (jTextField2.getText().equals(portNames[i])) {
availablePorts =true;
}
}
if (!availablePorts) throw new Exception();
/**creation of a new serial Device*/
SerialDevice serial = new SerialDevice(jTextField2.getText());
try{
//port Initialize
serial.open();
serial.setParameters();
//Sending 1st message
//serial.SendGetInfo();
serial.readOnEvent();
serial.SendGetInfo();
//Closing port
serial.close();
} catch (Exception ex) {
jLabel2.setText("Unable to Open Port");
}
请帮我找出没有事件发生的原因