1

I am using libserial library to interact with a modem using C++. The C++ code sends an AT command:

my_serial_stream << "AT+CSQ" << '\r' ;

The modem responds with a response, either ERROR or OK,

The c++ code to read the response:

while( serial_port.rdbuf()->in_avail() > 0  ) 
{
    char next_byte;
    serial_port.get(next_byte);
    std::cerr << std::hex << (int)next_byte << " ";
} 
std::cerr << std::endl;

I would like to handle the response such that if the response is OK, the modem sends another command and if the response is ERROR, the modem resends the first command.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
farai
  • 11
  • 1
  • First of all, shouldn't you be sending `"\r\n"` as newline (it's usually the common newline, `'\r'` used alone was only used on old Macs)? Secondly, why not simple read a whole line from the serial port (using e.g. [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline)), and just compare the text to `"OK"`? – Some programmer dude Jun 27 '14 at 12:13
  • The standard when sending to the modem is indeed `\r`, but the modem is expected to send `\r\n` when echoing. The terminal program will only ever generate a single character even for the return key, while the modem will try to go for a legible display. – Simon Richter Jun 27 '14 at 12:32
  • @JoachimPileborg Using `getline` could stop the thread when there is no modem connected or when it is in transparent data transfer mode. So farai should write "The modem responds with either `ERROR`, `OK`, or probably not at all." – harper Jun 27 '14 at 14:59

0 Answers0