Process communications. More...
Go to the source code of this file.
Data Structures | |
struct | urg_connection_t |
Connection resources. More... | |
Enumerations | |
enum | { URG_CONNECTION_TIMEOUT = -1 } |
Defines constants. More... | |
enum | urg_connection_type_t { URG_SERIAL, URG_ETHERNET } |
Connection type. More... | |
Functions | |
int | connection_open (urg_connection_t *connection, urg_connection_type_t connection_type, const char *device, long baudrate_or_port) |
Connection. | |
void | connection_close (urg_connection_t *connection) |
Disconnection. | |
int | connection_set_baudrate (urg_connection_t *connection, long baudrate) |
Configures the baudrate. | |
int | connection_write (urg_connection_t *connection, const char *data, int size) |
Send. | |
int | connection_read (urg_connection_t *connection, char *data, int max_size, int timeout) |
Receive. | |
int | connection_readline (urg_connection_t *connection, char *data, int max_size, int timeout) |
Receive until end-of-line. |
Process communications.
anonymous enum |
void connection_close | ( | urg_connection_t * | connection | ) |
Disconnection.
Closes the connection with the device
[in,out] | connection | Connection resource |
connection_close(&connection);
int connection_open | ( | urg_connection_t * | connection, |
urg_connection_type_t | connection_type, | ||
const char * | device, | ||
long | baudrate_or_port | ||
) |
Connection.
Connects to the specified device
[in,out] | connection | Connection resource |
[in] | connection_type | Connection type |
[in] | device | Device name |
[in] | baudrate_or_port | Baudrate or port number |
0 | Success |
<0 | Error |
The connection_type is either of:
device and baudrate_or_port arguments are defined according to connection_type For example, in case of serial connection:
Example
connection_t connection; if (! connection_open(&connection, URG_SERIAL, "COM1", 115200)) { return 1; }
And, in case of ethernet connection:
Example
connection_t connection; if (! connection_open(&connection, URG_ETHERNET, "192.168.0.10", 10940)) { return 1; }
int connection_read | ( | urg_connection_t * | connection, |
char * | data, | ||
int | max_size, | ||
int | timeout | ||
) |
Receive.
Reads data from the communication channel
[in,out] | connection | Connection resource |
[in] | data | Buffer to store received data |
[in] | max_size | Maximum size of the buffer |
[in] | timeout | Timeout [msec] |
>=0 | Number of bytes received |
<0 | Error |
If timeout argument is negative then the function waits until some data is received
In case no data is received URG_CONNECTION_TIMEOUT is returned.
Example
enum { BUFFER_SIZE = 256, TIMEOUT_MSEC = 1000, }; char buffer[BUFFER_SIZE]; n = connection_read(&connection, buffer, BUFFER_SIZE, TIMEOUT_MSEC);
int connection_readline | ( | urg_connection_t * | connection, |
char * | data, | ||
int | max_size, | ||
int | timeout | ||
) |
Receive until end-of-line.
Reads data until the end-of-line character is detected.
[in,out] | connection | Connection resource |
[in] | data | Buffer to store received data |
[in] | max_size | Maximum size of the buffer |
[in] | timeout | Timeout [msec] |
>=0 | Number of bytes received |
<0 | Error |
If timeout argument is negative then the function waits until some data is received
The null terminator character '\0' is used at the end of data so that the number of bytes does not exceed max_size. This is, the maximum number of received characters is max_size - 1.
The end-of-line character is either '\r' or '\n'
In case no end-of-line is received then returns 0, if no data is received URG_CONNECTION_TIMEOUT is returned.
int connection_write | ( | urg_connection_t * | connection, |
const char * | data, | ||
int | size | ||
) |
Send.
Writes data over the communication channel
[in,out] | connection | Connection resource |
[in] | data | Data to send |
[in] | size | Number of bytes to send |
>=0 | Number of bytes sent |
<0 | Error |
Example
n = connection_write(&connection, "QT\n", 3);