Go to the documentation of this file.00001
00010 #include "urg_c/urg_serial.h"
00011
00012
00013 enum {
00014 False = 0,
00015 True,
00016 };
00017
00018
00019 #if defined(URG_WINDOWS_OS)
00020 #include "urg_serial_windows.c"
00021 #else
00022 #include "urg_serial_linux.c"
00023 #endif
00024
00025
00026
00027 static int is_linefeed(const char ch)
00028 {
00029 return ((ch == '\r') || (ch == '\n')) ? 1 : 0;
00030 }
00031
00032
00033 static void serial_ungetc(urg_serial_t *serial, char ch)
00034 {
00035 serial->has_last_ch = True;
00036 serial->last_ch = ch;
00037 }
00038
00039
00040 int serial_readline(urg_serial_t *serial, char *data, int max_size, int timeout)
00041 {
00042
00043 int filled = 0;
00044 int is_timeout = 0;
00045
00046 while (filled < max_size) {
00047 char recv_ch;
00048 int n = serial_read(serial, &recv_ch, 1, timeout);
00049 if (n <= 0) {
00050 is_timeout = 1;
00051 break;
00052 } else if (is_linefeed(recv_ch)) {
00053 break;
00054 }
00055 data[filled++] = recv_ch;
00056 }
00057 if (filled >= max_size) {
00058 --filled;
00059 serial_ungetc(serial, data[filled]);
00060 }
00061 data[filled] = '\0';
00062
00063 if ((filled == 0) && is_timeout) {
00064 return -1;
00065 } else {
00066
00067 return filled;
00068 }
00069 }