When requesting com port returns the same request

可紊 提交于 2019-11-27 05:33:00

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.

I suggest the following improvements to your code:

  • Replace waitTime(System.currentTimeMillis(), 2000); in the event listener with if ( serialPortEvent.isRXCHAR() ) { ...
  • Make sure you terminate your AT command properly; usually a linefeed and/or carriage return is required at the end of each command string. Check the documentation of your device.
  • Make isReceived volatile, i.e. private volatile boolean isReceived;, if it is to be shared among different threads.

To avoid busy-waiting you can use standard Java synchronization primitives, like this:

private volatile boolean isReceived;

private final Object syncObject = new Object();

// ...

private void waitForReceived() {
  synchronized(syncObject) {
    while( !isReceived ) {
      syncObject.wait();
    }
  }
}


private void signalReceived() {
  synchronized(syncObject) {
    isReceived = true;
    syncObject.notifyAll();
  }
} 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!