jssc always timeout

801 views Asked by At

So I'm trying to send command to my device using JSSC library, but haven't succeeded yet. This is my code, build from several example online:

static SerialPort tPort = new SerialPort("/dev/ttyUSB0");
public static void main(String[] args) {
    String[] portNames = SerialPortList.getPortNames();
    for (String tPortName : portNames) {
        System.out.println(tPortName);
    }

    try {
        tPort.openPort();
        tPort.setParams(
                SerialPort.BAUDRATE_9600, 
                SerialPort.DATABITS_8, 
                SerialPort.STOPBITS_1, 
                SerialPort.PARITY_NONE);
        tPort.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_IN|SerialPort.FLOWCONTROL_XONXOFF_OUT);

        int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR; //Prepare mask
        tPort.writeBytes("AT".getBytes()); //Write data to port
        tPort.setEventsMask(mask); //Set mask
        tPort.addEventListener(new SerialPortReader()); //Add SerialPortEventListener

    } catch (SerialPortException ex) {
        ex.printStackTrace(System.out);
    }
}

static class SerialPortReader implements SerialPortEventListener {
    public void serialEvent(SerialPortEvent event) {
        if(event.isRXCHAR()){//If data is available
            System.out.println("available!");
            if(event.getEventValue() == 10){//Check bytes count in the input buffer
                //Read data, if 10 bytes available 
                try {
                    byte buffer[] = tPort.readBytes(10);
                } 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");
            }
        }
    }
}

when I ran it, the only output I got was

CTS - OFF
DSR - OFF

with no apparent response from the device. But when I use minicom using this configuration

    lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk
    x A -    Serial Device      : /dev/ttyUSB0                              x
    x B - Lockfile Location     : /var/lock                                 x
    x C -   Callin Program      :                                           x
    x D -  Callout Program      :                                           x
    x E -    Bps/Par/Bits       : 9600 8N1                                  x
    x F - Hardware Flow Control : No                                        x
    x G - Software Flow Control : No                                        x
    x                                                                       x
    x    Change which setting?                                              x
    mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj

the device return something.

What did I do wrong?

edit: I tried reading with timeout method, and the method keep throwing timeout.

2

There are 2 answers

2
Strelok On

At the very least you should call addEventListener BEFORE sending to the port with writeBytes.

You should also set .setFlowControl(SerialPort.FLOWCONTROL_NONE) if you want exactly the same settings as minicom.

0
ndriks On

Apparently I missed the spec which specify that in order to be considered a message, the input string must end with CR. After adding the CR character, the device has returned the something. stupid mistake (doh).