current
src
urg_serial.c
Go to the documentation of this file.
1
10
#include "
urg_c/urg_serial.h
"
11
12
13
enum
{
14
False
= 0,
15
True
,
16
};
17
18
19
#if defined(URG_WINDOWS_OS)
20
#include "
urg_serial_windows.c
"
21
#else
22
#include "
urg_serial_linux.c
"
23
#endif
24
25
26
// 改行かどうかの判定 static int is_linefeed(const char ch) { return ((ch == '\r') || (ch == '\n')) ? 1 : 0; } static void serial_ungetc(urg_serial_t *serial, char ch) { serial->has_last_ch = True; serial->last_ch = ch; } int serial_readline(urg_serial_t *serial, char *data, int max_size, int timeout) { /* 1文字ずつ読み出して評価する */ int filled = 0; int is_timeout = 0; while (filled < max_size) { char recv_ch; int n = serial_read(serial, &recv_ch, 1, timeout); if (n <= 0) { is_timeout = 1; break; } else if (is_linefeed(recv_ch)) { break; } data[filled++] = recv_ch; } if (filled >= max_size) { --filled; serial_ungetc(serial, data[filled]); } data[filled] = '\0'; if ((filled == 0) && is_timeout) { return -1; } else { //fprintf(stderr, "%s\n", data); return filled; } }
27
static
int
is_linefeed
(
const
char
ch)
28
{
29
return
((ch ==
'\r'
) || (ch ==
'\n'
)) ? 1 : 0;
30
}
31
32
33
static
void
serial_ungetc
(
urg_serial_t
*serial,
char
ch)
34
{
35
serial->
has_last_ch
=
True
;
36
serial->
last_ch
= ch;
37
}
38
39
40
int
serial_readline
(
urg_serial_t
*serial,
char
*data,
int
max_size,
int
timeout)
41
{
42
/* 1文字ずつ読み出して評価する */
43
int
filled = 0;
44
int
is_timeout = 0;
45
46
while
(filled < max_size) {
47
char
recv_ch;
48
int
n =
serial_read
(serial, &recv_ch, 1, timeout);
49
if
(n <= 0) {
50
is_timeout = 1;
51
break
;
52
}
else
if
(
is_linefeed
(recv_ch)) {
53
break
;
54
}
55
data[filled++] = recv_ch;
56
}
57
if
(filled >= max_size) {
58
--filled;
59
serial_ungetc
(serial, data[filled]);
60
}
61
data[filled] =
'\0'
;
62
63
if
((filled == 0) && is_timeout) {
64
return
-1;
65
}
else
{
66
//fprintf(stderr, "%s\n", data);
67
return
filled;
68
}
69
}
serial_read
int serial_read(urg_serial_t *serial, char *data, int max_size, int timeout)
データを受信する
Definition:
urg_serial_linux.c:193
serial_ungetc
static void serial_ungetc(urg_serial_t *serial, char ch)
Definition:
urg_serial.c:33
urg_serial_t
シリアル通信用
Definition:
urg_serial.h:40
urg_serial_linux.c
シリアル通信
urg_serial_t::last_ch
char last_ch
Definition:
urg_serial.h:53
serial_readline
int serial_readline(urg_serial_t *serial, char *data, int max_size, int timeout)
改行までのデータを受信する
Definition:
urg_serial.c:40
urg_serial_windows.c
シリアル通信
False
@ False
Definition:
urg_serial.c:14
urg_serial.h
シリアル通信
True
@ True
Definition:
urg_serial.c:15
is_linefeed
static int is_linefeed(const char ch)
Definition:
urg_serial.c:27
urg_serial_t::has_last_ch
char has_last_ch
Definition:
urg_serial.h:52
urg_c
Author(s): Satofumi Kamimura
, Katsumi Kimoto, Adrian Boeing
autogenerated on Wed Mar 2 2022 01:08:11