serial.h
Go to the documentation of this file.
1 #ifndef NSERIAL_H
2 #define NSERIAL_H
3 
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <string.h>
7 #include <termios.h>
8 #include <unistd.h>
9 #define SERIALDEFAULTDEVICE "/dev/ttyS0"
10 enum SerialBaud {
11  SPABAUD_50 = B50, SPABAUD_110 = B110, SPABAUD_300 = B300, SPABAUD_600 = B600,
12  SPABAUD_1200 = B1200, SPABAUD_2400 = B2400, SPABAUD_4800 = B4800,
13  SPABAUD_9600 = B9600, SPABAUD_19200 = B19200,
14  SPABAUD_38400 = B38400, SPABAUD_57600 = B57600, SPABAUD_115200 = B115200 };
18  SPASTOPBITS_1 = 0, SPASTOPBITS_2 = CSTOPB };
20  SPAPARITY_NONE = 0, SPAPARITY_ODD = PARODD | PARENB, SPAPARITY_EVEN = PARENB };
23  SPAPROTOCOL_XON_XOFF = IXOFF | IXON };
24 
25 struct serial
26 {
27  struct termios Termios;
28  int Stream;
29 };
30 
31 
32 static enum SerialParity SerialGetParity(const char *buf, int *ressize)
33 {
34  int r = 0;
36  if(!strncasecmp(buf, "none", 4))
37  { r = 4; p = SPAPARITY_NONE; }
38  else if(!strncasecmp(buf, "no", 2))
39  { r = 2; p = SPAPARITY_NONE; }
40  else if(!strncasecmp(buf, "odd", 3))
41  { r = 3; p = SPAPARITY_ODD; }
42  else if(!strncasecmp(buf, "even", 4))
43  { r = 4; p = SPAPARITY_EVEN; }
44  else if(*buf == 'N' || *buf == 'n')
45  { r = 1; p = SPAPARITY_NONE; }
46  else if(*buf == 'O' || *buf == 'o')
47  { r = 1; p = SPAPARITY_ODD; }
48  else if(*buf == 'E' || *buf == 'e')
49  { r = 1; p = SPAPARITY_EVEN; }
50  if(ressize) *ressize = r;
51  return p;
52 }
53 
54 static enum SerialProtocol SerialGetProtocol(const char *buf, int *ressize)
55 {
56  int r = 0;
57  enum SerialProtocol Protocol = SPAPROTOCOL_NONE;
58  /* try some possible forms for input, be as gentle as possible */
59  if(!strncasecmp("xonxoff",buf,7)){r = 7; Protocol=SPAPROTOCOL_XON_XOFF;}
60  else if(!strncasecmp("xon_xoff",buf,8)){r = 8; Protocol=SPAPROTOCOL_XON_XOFF;}
61  else if(!strncasecmp("xon-xoff",buf,8)){r = 8; Protocol=SPAPROTOCOL_XON_XOFF;}
62  else if(!strncasecmp("xon xoff",buf,8)){r = 8; Protocol=SPAPROTOCOL_XON_XOFF;}
63  else if(!strncasecmp("xoff",buf,4)){r = 4; Protocol=SPAPROTOCOL_XON_XOFF;}
64  else if(!strncasecmp("xon",buf,3)){r = 3; Protocol=SPAPROTOCOL_XON_XOFF;}
65  else if(*buf == 'x' || *buf == 'X'){r = 1; Protocol=SPAPROTOCOL_XON_XOFF;}
66  else if(!strncasecmp("rtscts",buf,6)){r = 6; Protocol=SPAPROTOCOL_RTS_CTS;}
67  else if(!strncasecmp("rts_cts",buf,7)){r = 7; Protocol=SPAPROTOCOL_RTS_CTS;}
68  else if(!strncasecmp("rts-cts",buf,7)){r = 7; Protocol=SPAPROTOCOL_RTS_CTS;}
69  else if(!strncasecmp("rts cts",buf,7)){r = 7; Protocol=SPAPROTOCOL_RTS_CTS;}
70  else if(!strncasecmp("rts",buf,3)){r = 3; Protocol=SPAPROTOCOL_RTS_CTS;}
71  else if(!strncasecmp("cts",buf,3)){r = 3; Protocol=SPAPROTOCOL_RTS_CTS;}
72  else if(*buf == 'r' || *buf == 'R' || *buf == 'c' || *buf == 'C')
73  {r = 1; Protocol=SPAPROTOCOL_RTS_CTS;}
74  else if(!strncasecmp("none",buf,4)){r = 4; Protocol=SPAPROTOCOL_NONE;}
75  else if(!strncasecmp("no",buf,2)){r = 2; Protocol=SPAPROTOCOL_NONE;}
76  else if(*buf == 'n' || *buf == 'N'){r = 1; Protocol=SPAPROTOCOL_NONE;}
77  if(ressize) *ressize = r;
78  return Protocol;
79 }
80 
81 
82 void SerialFree(struct serial *sn);
83 int SerialWrite(struct serial *sn, const char *buffer, size_t size);
84 int SerialRead(struct serial *sn, char *buffer, size_t size);
85 const char * SerialInit(struct serial *sn,
86 const char *Device, enum SerialBaud Baud, enum SerialStopbits StopBits,
87  enum SerialProtocol Protocol, enum SerialParity Parity,
88  enum SerialDatabits DataBits, int dowrite
89  #ifdef __GNUC__
90  __attribute__((__unused__))
91  #endif /* __GNUC__ */
92 );
93 
94 #endif
95 
struct termios Termios
Definition: serial.h:27
int SerialRead(struct serial *sn, char *buffer, size_t size)
Definition: serial.cpp:73
static enum SerialProtocol SerialGetProtocol(const char *buf, int *ressize)
Definition: serial.h:54
const char * SerialInit(struct serial *sn, const char *Device, enum SerialBaud Baud, enum SerialStopbits StopBits, enum SerialProtocol Protocol, enum SerialParity Parity, enum SerialDatabits DataBits, int dowrite)
Definition: serial.cpp:43
SerialDatabits
Definition: serial.h:15
Definition: serial.h:25
void SerialFree(struct serial *sn)
Definition: serial.cpp:32
SerialProtocol
Definition: serial.h:21
SerialBaud
Definition: serial.h:10
SerialParity
Definition: serial.h:19
SerialStopbits
Definition: serial.h:17
int SerialWrite(struct serial *sn, const char *buffer, size_t size)
Definition: serial.cpp:86
static enum SerialParity SerialGetParity(const char *buf, int *ressize)
Definition: serial.h:32
int Stream
Definition: serial.h:28


dgps_ros
Author(s):
autogenerated on Wed Jan 20 2021 03:38:34