mbayulke / java-simple-serial-connector

Automatically exported from code.google.com/p/java-simple-serial-connector
0 stars 0 forks source link

SerialPortListener/SerialPortReader has been performed twice after every "serialPort.writeBytes" #64

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Dear friends,

Can anybody have same thing like this? After write bytes to serial port, 
SerialPortListener/SerialPortReader will be performed twice. I tried using 
SerialPortListener and SerialPortReader, it has same result. I list my code 
below, appreciate any help, many thanks.

What version of the product are you using? On what operating system?
jssc 2.8 on Windows XP 32bit.

try {
            serialPort.openPort();//Open serial port
            serialPort.setParams(SerialPort.BAUDRATE_9600, 
                                 SerialPort.DATABITS_8,
                                 SerialPort.STOPBITS_1,
                                 SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);

            int mask = SerialPort.MASK_RXCHAR;                                                
//            int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + 
SerialPort.MASK_DSR; 
            serialPort.setEventsMask(mask);                
            //serialPort.addEventListener(new SerialPortReader());  
            serialPort.addEventListener(new SerialPortListener());
        }
        catch (SerialPortException ex){
            System.out.println(ex);
        }

//        while(true)
//        {
            System.out.println("htCommands.size(): " + htCommands.size());
            for(Entry<byte[], Element> entry : htCommands.entrySet()) {
                byte[] byteCommand = entry.getKey();
                elRecived = entry.getValue();

                try{
                    serialPort.purgePort(SerialPort.PURGE_RXCLEAR); //PURGE_RXCLEAR: input buffer
//                  serialPort.purgePort(SerialPort.PURGE_TXCLEAR);
                    serialPort.writeBytes(byteCommand);
                }
                catch (SerialPortException ex){
                    System.out.println(ex);
                }

                try {
                    Thread.sleep(SEND_DURATION);
                } catch(InterruptedException ex) {
                    Thread.currentThread().interrupt();
                    System.out.println("InterruptedException:" + ex.toString());
                }
            }
            try {
                Thread.sleep(LOOP_DURATION);
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
                System.out.println("InterruptedException:" + ex.toString());
            }
//        }
    }

    static int TT = 0; //Used only for testing how many times that SerialPortListener has been performed
    /*
     * 1.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
     */
//    static class SerialPortReader implements SerialPortEventListener {
    static class SerialPortListener implements SerialPortEventListener {
        public void serialEvent(SerialPortEvent event) {

            TT++;
            try {
                Thread.sleep(REPLY_PAUSE);
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
                System.out.println("InterruptedException:" + ex.toString());
            }
            System.out.println("----------In SerialPortReader.");
            System.out.println("TT: " + TT);
            try {
                if(TT/2 == 1)
                {
                    byte[] reply = serialPort.readBytes();
                    System.out.println("----------Com.bytesToHexString(reply)-1: " + Com.bytesToHexString(reply));
                }

                if(TT/2 == 2){
                    System.out.println("----------in TT 2." );
                    byte[] reply1 = serialPort.readBytes(25);
                    System.out.println("----------Com.bytesToHexString(reply)-2: " + Com.bytesToHexString(reply1));
                }
            }
            catch (NullPointerException ex) {
                System.out.println("NullPointerException: " + ex);
            }
            catch (SerialPortException ex) {
                System.out.println(ex);
            }
        }

Original issue reported on code.google.com by saucer...@gmail.com on 18 Mar 2014 at 2:19