2

我正在尝试将字节序列发送到串行端口,但我无法从端口本身读取回复。我正在使用 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");
        }

请帮我找出为什么没有事件发生

4

0 回答 0