'why jssc serialevent handler not triggered for the second command

I'm working on serial port communication in java using jssc. I'm able to send and receive data from serial port for one command but now I want to send list of commands one after the other and receive response for each command . my working code now

public class SerialComm implements Runnable {

private SerialPort serialPort;
String portname;
boolean received=false;
static String status;
String command;
public static String getStatus() {
return status;
}

public static void setStatus(String status) {
SerialComm.status = status;
}

int count=0;
public SerialComm(String portname,String command) {
    // TODO Auto-generated constructor stub
    this.portname=portname;
    this.command=command;
}

public void initCOMPort(String portname,String command) {
    serialPort = new SerialPort(portname);
    try {
        serialPort.openPort();
        serialPort.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
       // serialPort.addEventListener(new PortReader(), SerialPort.MASK_RXCHAR);
        
        serialPort.addEventListener(new SerialPortEventListener() {
            @Override
            public void serialEvent(SerialPortEvent event) {

                System.out.println("Listener got event");
                if (event.isRXCHAR() && event.getEventValue() > 0) {
                    try {
                        String receivedData = serialPort.readString(event.getEventValue());                    
                        System.out.println("Recvied Data : " + receivedData);
                        if(receivedData.contains("cldr"))
                        {
                            count++;
                            System.out.println("count "+count);
                            if(count==1)
                            {
                                received=true;
                                serialPort.removeEventListener();
                                setStatus("received");
                            }
                        }

                    }
                    catch (SerialPortException ex) {
                        System.out.println("JSSC -> initPort() : Error in receiving string from COM-port: " + ex);
                    }
                }
            }
        });
        
        
        String ls = System.lineSeparator(), dlc = "dl load /ramdisk/tool_libraries/", gtc="google_tools/combined/";
        //String portcmd="hellotest"+ls+"hellotest"+ls+"hellotest"+ls,
                
                String portcmd="ramdisk --mount /RAM/ramdisk.ext2"+ls+
                dlc + gtc + "google_tools.so"+ls
                + dlc + "google_tests/combined/google_tests.so"+ls+
                "rm /RAM/ramdisk.ext2"+ls+ "chip_id";
                String[] jj=portcmd.split(ls);
                //for(String comm:jj) { System.out.println("Sending command \"" + comm + "\"");
                    if(serialPort.writeString(command + "\n")) { 
                        setStatus("sent");System.out.println("Data sent"); }
                    // } 
    } catch (SerialPortException ex) { System.out.println("Error in writing data to port: " + ex);
    } }

private class PortReader implements SerialPortEventListener {
    BufferedWriter writer = null;
    @Override public void serialEvent(SerialPortEvent event) {
    System.out.println("Listener got event");
        if (event.isRXCHAR() && event.getEventValue() > 0) {
            try {
                String receivedData = serialPort.readString(event.getEventValue());                    
                System.out.println("Recvied Data : " + receivedData);
                if(receivedData.contains("cldr"))
                {
                    count++;
                    System.out.println("count "+count);
                    if(count==1)
                    {
                        received=true;
                        setStatus("received");
                    }
                }
      catch (SerialPortException ex) { System.out.println("Error in receiving response from port: " + ex);
            }} } }

@Override public void run() {
    // TODO Auto-generated method stub
    initCOMPort(portname,command);
}
}

I call the above code from mainclass like this

else if(runcommand.equalsIgnoreCase("Serialhello"))
     {
         
         String ls = System.lineSeparator(), dlc = "dl load /ramdisk/tool_libraries/", gtc="google_tools/combined/";
            String portcmd="hellotest"+ls+"hellotest"+ls+"hellotest"+ls;
        

                    String[] jj=portcmd.split(System.lineSeparator());
                    for(int i=0;i<jj.length;i++)
                    {   
                        if(i==0)
                        {
                        SerialComm serial =new SerialComm(sibsiteno,jj[i].trim());
                        serial.initCOMPort(sibsiteno,jj[i].trim());
                        }
                        else 
                        {SerialComm serial =new SerialComm(sibsiteno,jj[i].trim());
                            if(serial.getStatus().equalsIgnoreCase("received"))
                            {
                                 serial.initCOMPort(sibsiteno,jj[i].trim());
                            }
                            else
                            {
                                System.out.println("current status : "+serial.getStatus());
                                System.out.println("waiting");
                            }
                        }
                        Thread.sleep(30*1000);
                    }

     }

I'm getting the following output now

current output

from the output i could see that the serialevent listener is not triggered for the second time.

so how to handle this i want to run list of commands one by one i.e after receiving the output for first command i have to run second command and so on. so why my event listener is not triggered for the second time? and is there anyother way to handle my situation.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source