通信の処理 [詳細]
データ構造 | |
| struct | urg_connection_t | 
| 通信リソース  [詳細] | |
列挙型 | |
| enum | { URG_CONNECTION_TIMEOUT = -1 } | 
| 定数定義  [詳細] | |
| enum | urg_connection_type_t { URG_SERIAL, URG_ETHERNET } | 
| 通信タイプ  [詳細] | |
関数 | |
| int | connection_open (urg_connection_t *connection, urg_connection_type_t connection_type, const char *device, long baudrate_or_port) | 
| 接続   | |
| void | connection_close (urg_connection_t *connection) | 
| 切断   | |
| int | connection_set_baudrate (urg_connection_t *connection, long baudrate) | 
| ボーレートを設定する  | |
| int | connection_write (urg_connection_t *connection, const char *data, int size) | 
| 送信   | |
| int | connection_read (urg_connection_t *connection, char *data, int max_size, int timeout) | 
| 受信   | |
| int | connection_readline (urg_connection_t *connection, char *data, int max_size, int timeout) | 
| 改行文字までの受信   | |
通信の処理
| void connection_close | ( | urg_connection_t * | connection | ) | 
| int connection_open | ( | urg_connection_t * | connection, | 
| urg_connection_type_t | connection_type, | ||
| const char * | device, | ||
| long | baudrate_or_port | ||
| ) | 
接続
指定されたデバイスに接続する。
| [in,out] | connection | 通信リソース | 
| [in] | connection_type | 接続タイプ | 
| [in] | device | 接続名 | 
| [in] | baudrate_or_port | ボーレート / ポート番号 | 
| 0 | 正常 | 
| <0 | エラー | 
connection_type には
を指定する。
device, baudrate_or_port の指定は connection_type により指定できる値が異なる。 例えば、シリアル通信の場合は以下のようになる。
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 | ||
| ) | 
受信
データを受信する。
| [in,out] | connection | 通信リソース | 
| [in] | data | 受信データを格納するバッファ | 
| [in] | max_size | 受信データを格納できるバイト数 | 
| [in] | timeout | タイムアウト時間 [msec] | 
| >=0 | 受信データ数 | 
| <0 | エラー | 
timeout に負の値を指定した場合、タイムアウトは発生しない。
1 文字も受信しなかったときは URG_CONNECTION_TIMEOUT を返す。
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 | ||
| ) | 
改行文字までの受信
改行文字までのデータを受信する。
| [in,out] | connection | 通信リソース | 
| [in] | data | 受信データを格納するバッファ | 
| [in] | max_size | 受信データを格納できるバイト数 | 
| [in] | timeout | タイムアウト時間 [msec] | 
| >=0 | 受信データ数 | 
| <0 | エラー | 
data には、'\0' 終端された文字列が max_size を越えないバイト数だけ格納される。 つまり、受信できる文字のバイト数は、最大で max_size - 1 となる。
改行文字は '\r' または '\n' とする。
受信した最初の文字が改行の場合は、0 を返し、1 文字も受信しなかったときは URG_CONNECTION_TIMEOUT を返す。
| int connection_write | ( | urg_connection_t * | connection, | 
| const char * | data, | ||
| int | size | ||
| ) | 
送信
データを送信する。
| [in,out] | connection | 通信リソース | 
| [in] | data | 送信データ | 
| [in] | size | 送信バイト数 | 
| >=0 | 送信データ数 | 
| <0 | エラー | 
Example
n = connection_write(&connection, "QT\n", 3);
 1.8.0