3

I'm trying to send byte sequences to the serial port, but I can't read the reply from the port itself. I'm sending the same messages with Realterm to handle serial port and it receives replies. I'm also monitoring with serial port monitor by Eltima and the write operation is succesful, but I can't read anything.
I'm using JFrame interface and I declared a separate class named SerialDevice which implements a serial port as an attribute with its methods (taken from jssc).
The serial port should use 57600, 8 bits data, 1 stop bit and RTS.
It seems the RXCHAR() event never happens...

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");
                }
            }
        }
    }

The code below is part of the button mouseclicked action

    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");
        }

Please help me to find why no event happens

Arjan
  • 823
  • 1
  • 7
  • 18
edo1080
  • 61
  • 7

0 Answers0