Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

java - When requesting com port returns the same request

I`m trying to send AT-command via COM-port, but reseived only the same command.

package SerialConnections;

import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static ru.telemetria.qa.utils.Utilities.waitTime;

public class M234Serial {
    private static Logger log = LoggerFactory.getLogger(M234Serial.class);
    private SerialPort serialPort;
    private byte[] receivedData;
    private boolean isReceived;

    public M234Serial() throws Exception {

        serialPort = new SerialPort("COM55");
    }

    public void sendCommand() throws Exception {
        open();

        String command = "AT^SCFG?";
        serialPort.writeBytes(command.getBytes());
        log.debug("Send request: " + command);

        while (!isReceived) {}

        close();
    }

    private void open() throws Exception {
        serialPort.openPort();
        serialPort.setParams(SerialPort.BAUDRATE_115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        serialPort.addEventListener(new SerialPortEventListener() {
            @Override
            public void serialEvent(SerialPortEvent serialPortEvent) {
                try {
                    waitTime(System.currentTimeMillis(), 2000);
                    receivedData = serialPort.readBytes();
                    log.debug("Received message: " + StringUtils.asciiToString(receivedData));
                    isReceived = true;
                    serialPort.removeEventListener();
                } catch (SerialPortException spe) {
                    spe.printStackTrace();
                }
            }
        });
    }

    private void close() throws Exception {
        serialPort.closePort();
    }

    public static void main(String[] args) throws Exception {
        log.debug("Create instance..");
        M234Serial serial = new M234Serial();
        serial.sendCommand();
        log.debug("End");
    }
}

Log:

16:19:21.910 [main] DEBUG SerialConnections.M234Serial - Create instance..

16:19:21.974 [main] DEBUG SerialConnections.M234Serial -Send request: AT^SCFG?

16:19:23.976 [EventThread COM55] DEBUG SerialConnections.M234Serial - Received message: AT^SCFG?

16:19:23.977 [main] DEBUG SerialConnections.M234Serial - End

What am I doing wrong and how can I fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Busy waiting like while (!isReceived) {} will yield horrible performance so if keeping that structure you should change the variable from a boolean to a mutex/semaphore or something similar. But you should not keep it, so I mention this just for reference.


Start by fetching a copy of the V.250 modem standard and read at least all of chapter 5. That will teach you a lot of basic AT command handling, like for instance that an AT command line should be terminated with .

The AT^SCFG command is obviously a proprietary manufacturer specific command so I do not have a documentation reference for that. Most mobile phone related AT commands standardized by 3GPP are given in 27.007, although some (SMS related) are given in 27.005

As mentioned at the beginning the structure needs to be changed. You should never, never, never, ever use waitTime, sleep or anything similar to wait for response from a modem. It's as useful as kicking dogs that stand in your way in order to get them to move. Yes you might be lucky and have it actually work sometimes, but at some point you will be sorry for taking that approach...

The only reliably approach is to do something similar to

serialPort.openPort();
...
// start sending AT^SCFG?
serialPort.writeBytes("AT^SCFG?
");
do {
    line = readLine(serialPort);
} while (! is_final_result_code(line))
// Sending of AT^SCFG? command finished (successfully or not)
...
serialPort.closePort();

where the readLine function reads one and one byte from the serial port until it has received a complete line terminated with and then returns that line.

You can look at the code for atinout for an example for the is_final_result_code function (you can also compare to isFinalResponseError and isFinalResponseSuccess in ST-Ericsson's U300 RIL, although note that CONNECT is not a final result code, it is an intermediate result code, so the name isFinalResponseSuccess is strictly speaking not 100% correct).


The issue with the command send being received back is related to the modem echoing the command. This can be disabled with the ATE command but with a proper parsing structure like above this normally does not matter because you just read the echoed command as a line that will be ignored.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...