请求com端口时返回相同的请求 [英] When requesting com port returns the same request

查看:179
本文介绍了请求com端口时返回相同的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过COM端口发送AT命令,但只重新发送相同的命令。

  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;

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

public M234Serial()抛出异常{

serialPort = new SerialPort(COM55);
}

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

String command =AT ^ SCFG?;
serialPort.writeBytes(command.getBytes());
log.debug(发送请求:+命令);

while(!isReceived){}

close();
}

private void open()抛出异常{
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()抛出异常{
serialPort.closePort();
}

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

日志:



16:19:21.910 [main] DEBUG SerialConnections.M234Serial - 创建实例..



16:19:21.974 [main] DEBUG SerialConnections.M234Serial - 发送请求:AT ^ SCFG?



16:19:23.976 [EventThread COM55] DEBUG SerialConnections.M234Serial - 收到的消息:AT ^ SCFG?



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



我做错了什么我该如何解决?

解决方案

忙着等待而(!isReceived){} 将产生可怕的性能,所以如果保持该结构,你应该将变量从布尔值更改为互斥量/信号量或类似的东西。但你不应该保留它,所以我提到这只是为了参考。






首先提取 V.250 调制解调器标准并至少读取所有第5章。这将教你很多基本的AT命令处理,例如AT命令行应该以 \ r 终止。



AT ^ SCFG命令显然是专有的制造商特定命令,因此我没有相关的文档参考。由3GPP标准化的大多数与移动电话相关的AT命令在 27.007 中给出。 ,虽然有些(短信相关)在 27.005 中给出



如开头所述,结构需要改变。你应该永远,永远,永远不会使用 waitTime sleep 或类似的东西等待调制解调器的响应。它就像踢狗一样有用,可以阻挡它们移动。是的,你可能很幸运并且实际上有时会工作,但是在某些时候你会抱歉采取这种方法...



唯一可靠的方法是做某事类似于

  serialPort.openPort(); 
...
//开始发送AT ^ SCFG?
serialPort.writeBytes(AT ^ SCFG?\ r);
do {
line = readLine(serialPort);
} while(!is_final_result_code(line))
//发送AT ^ SCFG?命令完成(成功与否)
...
serialPort.closePort();

其中 readLine 函数读取一个和一个从串口开始的字节,直到收到以 \\\\ n 终止的完整行,然后返回该行。



您可以查看 atinout 例如 is_final_result_code 函数(你也可以比较 isFinalResponseError isFinalResponseSuccess ST-Ericsson的U300 RIL 中,虽然注意 CONNECT 不是最终结果代码,但它是一个中间结果代码,所以名称isFinalResponseSuccess严格来说不是100%正确的。)






发送回收命令的问题与回显命令的调制解调器有关。这可以使用 ATE 命令禁用,但是使用正确的解析结构,这通常无关紧要,因为您只是将echoed命令读作将被忽略的行。 / p>

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?

解决方案

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 \r.

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?\r");
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 \r\n 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.

这篇关于请求com端口时返回相同的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆