upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
Public Member Functions | Protected Attributes
UARTAT Class Reference

API for a Generic AT command based UART device. More...

Detailed Description

This is a generic UART device driver for accessing UART based devices that utilize an "AT" command set. Typically these devices are Radios, Modems, and similar devices that are configured and controlled by emitting "AT" commands.

string defaultDev = string("/dev/ttyUSB0");
if (argc > 1)
defaultDev = string(argv[1]);
cout << "Using device: " << defaultDev << endl;
// Instantiate a UARTAT sensor on defaultDev at 115200 baud.
upm::UARTAT sensor(defaultDev, 115200);
// This is a simplistic example that tries to configure the LE910,
// and use it's built-in socket capabilities to connect to a
// remote host, obtain a small piece of data, and return it. It's
// mainly intended to show you how you can use the various AT
// commands supported by the LE910 to perform simple tasks.
//
// You must have a valid SIM card with an active data plan for
// this example to do anything interesting.
//
// See the LE910 AT Commands reference for full information on
// what is possible with this device. The uartat driver is
// intended to make it a little easier to control AT-style
// devices, but is by no means a full-featured communication
// infrastructure. A "real" application will probably need to be
// much more sophisticated with regard to parsing, doing retries,
// etc.
//
// For experimenting with various AT commands, try using an
// interactive terminal emulator like minicom or screen.
// make sure we are in command mode
if (!sensor.inCommandMode()) {
cout << "Not in command mode, switching..." << endl;
sensor.commandMode("+++", 1000);
}
// flter out CR's in responses by default
sensor.filterCR(true);
cout << "Configuring modem..." << endl;
// discard any waiting characters
sensor.drain();
// reset modem
sensor.command("ATZ\r");
// turn off command echo, set verbosity to 1, enable data
// connection mode
sensor.command("ATE0 V1 +FCLASS=0\r");
sensor.drain();
// Now issue some commands and output the results.
cout << "Modem and SIM information:" << endl;
std::string buffer;
buffer = sensor.commandWithResponse("AT+ICCID\r", bufferLength);
if (!buffer.empty())
cout << "ICCID (SIM ID): " << buffer << endl;
buffer = sensor.commandWithResponse("AT+CGSN=1\r", bufferLength);
if (!buffer.empty())
cout << "IMEI: " << buffer << endl;
// see if we are on the network....
buffer = sensor.commandWithResponse("AT+CREG?\r", bufferLength);
if (!buffer.empty()) {
cout << buffer << endl;
// look for "CGREG: 0,1" or "CGREG: 0,5"
if (sensor.find(buffer, "CREG: 0,1") || sensor.find(buffer, "CREG: 0,5")) {
cout << "Connected to the cell data network." << endl;
// wait up to 5 seconds for responses now...
sensor.setResponseWaitTime(5000);
// setup PDP context (socket 1). An ERROR repsonse is
// possible if the PDP context is already set up.
sensor.command("AT#SGACT=1,1\r");
// setup a TCP socket to nist.gov and read the timestamp.
cout << "Connecting to time-a.nist.gov, TCP port 13" << endl;
// Wait up to 60 seconds to find the NO CARRIER
// string, which will be present at the end, if the
// connection succeeded and the requested data was
// obtained.
buffer = sensor.commandWaitFor("AT#SD=1,0,13,\"time-a.nist.gov\"\r",
bufferLength,
"\nNO CARRIER\n",
60000);
if (!buffer.empty()) {
// print out the response
cout << "RESPONSE: " << endl << buffer << endl;
} else {
cout << "No response." << endl;
}
// destroy PDP context
sensor.setResponseWaitTime(250);
sensor.command("AT#SGACT=1,0\r");
} else {
cout << "You do not appear to be connected to the network..." << endl;
}
} else {
cout << "Error executing query\n" << endl;
}
// reset the modem
sensor.command("ATZ\r");
cout << "Exiting" << endl;

Public Member Functions

 UARTAT (unsigned int uart, unsigned int baudrate)
 
 UARTAT (std::string uart_path, unsigned int baudrate)
 
 ~UARTAT ()
 
std::string readStr (size_t size)
 
int writeStr (std::string buffer)
 
void setBaudrate (unsigned int baudrate)
 
void setResponseWaitTime (unsigned int wait_time)
 
bool dataAvailable (unsigned int millis)
 
bool commandMode (const std::string cmd_chars, unsigned int guard_ms)
 
bool inCommandMode ()
 
void drain ()
 
std::string commandWithResponse (const std::string cmd, size_t resp_len)
 
std::string commandWaitFor (const std::string cmd, size_t resp_len, const std::string waitString, unsigned int millis)
 
void command (const std::string cmd)
 
std::string stringCR2LF (std::string str)
 
void setFlowControl (UARTAT_FLOW_CONTROL_T fc)
 
bool find (const std::string buffer, const std::string str)
 
void filterCR (bool enable)
 

Protected Attributes

uartat_context m_uartat
 

Constructor & Destructor Documentation

UARTAT ( unsigned int  uart,
unsigned int  baudrate 
)

UARTAT object constructor for a UART specified by MRAA number.

Parameters
uartSpecify which uart to use.
baudrateSpecify the baudrate to use.

Here is the call graph for this function:

UARTAT ( std::string  uart_path,
unsigned int  baudrate 
)

UARTAT object constructor for a UART specified by PATH (ex: /dev/ttyUSB0)

Parameters
uart_pathSpecify path of UART device.
baudrateSpecify the baudrate to use.
~UARTAT ( )

UARTAT object destructor

Here is the call graph for this function:

Member Function Documentation

std::string readStr ( size_t  size)

Read character data from the device.

Parameters
sizeThe maximum number of characters to read.
Returns
string containing the data read.

Here is the call graph for this function:

int writeStr ( std::string  buffer)

Write character data to the device.

Parameters
bufferThe string containing the data to write.
Returns
The number of bytes written.

Here is the call graph for this function:

void setBaudrate ( unsigned int  baudrate)

Set the baudrate of the device.

Parameters
baudrateThe baud rate to set for the device.

Here is the call graph for this function:

void setResponseWaitTime ( unsigned int  wait_time)

Set the default time, in milliseconds, to wait for data to arrive after sending a command.

Parameters
wait_timeThe response delay to set, in milliseconds.

Here is the call graph for this function:

bool dataAvailable ( unsigned int  millis)

Determine whether there is data available to be read. In the case of a UART, this function will wait up to "millis" milliseconds for data to become available. In the case of an I2C device, the millis argument is ignored and the function will return immediately, indicating whether data is available.

Parameters
millisThe number of milliseconds to wait for data to become available.
Returns
true if data is available to be read, false otherwise.

Here is the call graph for this function:

bool commandMode ( const std::string  cmd_chars,
unsigned int  guard_ms 
)

Place the device in AT command mode. Many devices operate in a transparent mode and an AT command mode. Command mode is required to issue AT based commands. When in transparent mode, the device will usually listen for a special sequence of characters and delays, indicating that AT command mode should be entered.

On most devices, the sequence is: <wait 1 second>+++<wait 1 second>

For most devices, the wait time is 1 second (1000 ms) and the character sequence is "+++". These options can often be configured on the device.

This function will wait millis milliseconds, write the command characters (typically "+++"), then wait millis milliseconds again. At this time a read will be attempted, looking for the "OK" response indicating command mode was successfully entered.

Parameters
cmd_charsThe character sequence to write, typically "+++".
guard_msThe number of milliseconds to delay before and after the cmd_chars are written.
Returns
true if AT command mode ("OK" detected) was successfully entered, false otherwise.

Here is the call graph for this function:

bool inCommandMode ( )

Check to see if the device is in command mode. This is accomplished by sending an "AT\r" command and seeing if "OK" or "0" is returned.

Returns
true if AT command mode was detected, false otherwise.

Here is the call graph for this function:

void drain ( )

Read and throw away any data currently available to be read. This is useful to avoid reading data that might have been the result of a previous command interfering with data you currently want to read. This function is automatically called by commandWithResponse(), command(), and commandWaitfor() prior to writing the requested command to the device.

Here is the call graph for this function:

string commandWithResponse ( const std::string  cmd,
size_t  resp_len 
)

Send an AT command and optionally return a response.

Parameters
cmdA character string containing the AT command to send, including the "AT" prefix and a terminating carriage return ("\r").
resp_lenThe maximum number of characters to read from the device.
Returns
The device response string, if any.

Here is the call graph for this function:

string commandWaitFor ( const std::string  cmd,
size_t  resp_len,
const std::string  waitString,
unsigned int  millis 
)

Send an AT command and return a response, while waiting for a specific string. If the string isn't found the returned string will be empty. If the string is found, the function will return immediately.

Parameters
cmdA character string containing the AT command to send, including the "AT" prefix and a terminating carriage return ("\r").
resp_lenThe maximum number of characters to read from the device.
waitStringThe string to look for. If found, the response will be returned immediately regardless of the timeout setting.
millisThe maximum number of milliseconds to wait for the string.
Returns
A string containing the response if the search string was found, otherwise and empty string is returned.

Here is the call graph for this function:

void command ( const std::string  cmd)

Send an AT command and ignore any response.

Parameters
cmdThe AT command to send, including the "AT" prefix and a terminating carriage return ("\r").

Here is the call graph for this function:

string stringCR2LF ( std::string  str)

This is a convenience method that converts each CR () in a string to a LF (
) and returns it. This is useful for outputting the response to an AT command for instance, which is often CR terminated.

Parameters
strThe string to convert
Returns
The converted string
void setFlowControl ( UARTAT_FLOW_CONTROL_T  fc)

Set a flow control method for the UART. By default, during initialization, flow control is disabled.

Parameters
fcOne of the UARTAT_FLOW_CONTROL_T values.

Here is the call graph for this function:

bool find ( const std::string  buffer,
const std::string  str 
)

Look for a string in a buffer. This is a utility function that can be used to indicate if a given string is present in a supplied buffer. The search is case sensitive.

Parameters
bufferThe string buffer in which to search.
strThe string to search for.
Returns
true if the string was found, false otherwise.

Here is the call graph for this function:

void filterCR ( bool  enable)

Filter out carriage returns (CR) from response buffers if enabled. This operates only on the response buffers returned from commandWithResponse(), command(), and commandWaitfor().

Parameters
enabletrue to filter out CR's, false otherwise

Here is the call graph for this function:

Collaboration diagram for UARTAT:
Collaboration graph
[legend]

The documentation for this class was generated from the following files: