]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/lidars/hokuyo/lib/serial_utils.c
Create LIDAR lib for hadling both rangefinders - SICK and Hokuyo
[eurobot/public.git] / src / lidars / hokuyo / lib / serial_utils.c
1 /*!
2   \file
3   \brief \83V\83\8a\83A\83\8b\91\97\8eó\90M\82Ì\95â\8f\95
4
5   \author Satofumi KAMIMURA
6
7   $Id: serial_utils.c 1308 2009-09-16 07:35:45Z satofumi $
8 */
9
10 #include "serial_utils.h"
11 #include "serial_ctrl.h"
12
13 #include <stdio.h>
14 #include <ctype.h>
15
16
17 /* \89ü\8ds\82©\82Ì\94»\92è */
18 int serial_isLF(const char ch)
19 {
20   return ((ch == '\r') || (ch == '\n')) ? 1 : 0;
21 }
22
23
24 /* \8eó\90M\83f\81[\83^\82Ì\93Ç\82Ý\94ò\82Î\82µ */
25 void serial_skip(serial_t *serial, int total_timeout, int each_timeout)
26 {
27   char recv_ch;
28
29   /* \8f\91\82«\96ß\82µ\82½\95\8e\9a\82ð\83N\83\8a\83A */
30   serial->last_ch_ = '\0';
31
32   if (each_timeout <= 0) {
33     each_timeout = total_timeout;
34   }
35
36   // !!! total_timeout \82ð\82±\82Ì\83\8b\81[\83v\8fð\8c\8f\82É\93K\97p\82·\82×\82«
37   while (1) {
38     int n = serial_recv(serial, &recv_ch, 1, each_timeout);
39     if (n <= 0) {
40       break;
41     }
42   }
43 }
44
45
46 /* \89ü\8ds\82Ü\82Å\82Ì\93Ç\82Ý\82¾\82µ */
47 int serial_getLine(serial_t *serial, char* data, int data_size_max,
48                    int timeout)
49 {
50   /* \82P\95\8e\9a\82¸\82Â\93Ç\82Ý\82¾\82µ\82Ä\95]\89¿\82·\82é */
51   int filled = 0;
52   int is_timeout = 0;
53
54   while (filled < data_size_max) {
55     char recv_ch;
56     int n = serial_recv(serial, &recv_ch, 1, timeout);
57     if (n <= 0) {
58       is_timeout = 1;
59       break;
60     } else if (serial_isLF(recv_ch)) {
61       break;
62     }
63     data[filled++] = recv_ch;
64   }
65   if (filled == data_size_max) {
66     --filled;
67     serial_ungetc(serial, data[filled]);
68   }
69   data[filled] = '\0';
70
71   if ((filled == 0) && is_timeout) {
72     return -1;
73   } else {
74     return filled;
75   }
76 }