All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
Data Structures | Enumerations | Functions
include/c/urg_connection.h File Reference

Process communications. More...

#include "urg_serial.h"
#include "urg_tcpclient.h"

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.

Detailed Description

Process communications.

Author:
Satofumi KAMIMURA
Id:
urg_connection.h,v e5d1719877a2 2015/05/07 04:12:14 jun

Enumeration Type Documentation

anonymous enum

Defines constants.

Enumerator:
URG_CONNECTION_TIMEOUT 

Return value in case of timeout.

Connection type.

Enumerator:
URG_SERIAL 

Serial/USB connection.

URG_ETHERNET 

Ethernet connection.


Function Documentation

void connection_close ( urg_connection_t connection)

Disconnection.

Closes the connection with the device

Parameters:
[in,out]connectionConnection resource
  connection_close(&connection); 
See also:
connection_open()
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

Parameters:
[in,out]connectionConnection resource
[in]connection_typeConnection type
[in]deviceDevice name
[in]baudrate_or_portBaudrate or port number
Return values:
0Success
<0Error

The connection_type is either of:

  • URG_SERIAL ... Serial connection
    • URG_ETHERNET .. Ethernet connection

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;
  } 
See also:
connection_close()
int connection_read ( urg_connection_t connection,
char *  data,
int  max_size,
int  timeout 
)

Receive.

Reads data from the communication channel

Parameters:
[in,out]connectionConnection resource
[in]dataBuffer to store received data
[in]max_sizeMaximum size of the buffer
[in]timeoutTimeout [msec]
Return values:
>=0Number of bytes received
<0Error

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); 
See also:
connection_write(), connection_readline()
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.

Parameters:
[in,out]connectionConnection resource
[in]dataBuffer to store received data
[in]max_sizeMaximum size of the buffer
[in]timeoutTimeout [msec]
Return values:
>=0Number of bytes received
<0Error

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.

See also:
connection_write(), connection_read()
int connection_write ( urg_connection_t connection,
const char *  data,
int  size 
)

Send.

Writes data over the communication channel

Parameters:
[in,out]connectionConnection resource
[in]dataData to send
[in]sizeNumber of bytes to send
Return values:
>=0Number of bytes sent
<0Error

Example

  n = connection_write(&connection, "QT\n", 3); 
See also:
connection_read(), connection_readline()