linux_serial.cpp
Go to the documentation of this file.
1 
47 #if !defined(LINUX_SERIAL_H) && !defined(win_x86)
48 #define LINUX_SERIAL_H
49 #include "roch_base/core/serial.h" /* Std. function protos */
50 #include <stdio.h> /* Standard input/output definitions */
51 #include <string.h> /* String function definitions */
52 #include <unistd.h> /* UNIX standard function definitions */
53 #include <fcntl.h> /* File control definitions */
54 #include <errno.h> /* Error number definitions */
55 #include <termios.h> /* POSIX terminal control definitions */
56 #include <stdlib.h> /* Malloc */
57 #include <assert.h>
58 
59 namespace sawyer{
60 
61  int roch_driver::OpenSerial(void **handle, const char *port_name)
62 {
63 
64  int fd; /* File descriptor for the port */
65 
66  fd = open(port_name, O_RDWR | O_NOCTTY | O_NDELAY);
67  if (fd == -1)
68  {
69  fprintf(stderr, "Unable to open %s\n\r", port_name);
70  return -3;
71  }
72 
73  // Verify it is a serial port
74  if (!isatty(fd))
75  {
76  close(fd);
77  fprintf(stderr, "%s is not a serial port\n", port_name);
78  return -3;
79  }
80 
81  *handle = (int *) malloc(sizeof(int));
82  **(int **) handle = fd;
83  return fd;
84 }
85 
86 int roch_driver::SetupSerial(void *handle)
87 {
88  struct termios options;
89 
90  // Get the current options for the port...
91  tcgetattr(*(int *) handle, &options);
92 
93  // 8 bits, 1 stop, no parity
94  options.c_cflag = 0;
95  options.c_cflag |= CS8; // 8-bit input
96 
97  // Enable the receiver and set local mode...
98  options.c_cflag |= (CLOCAL | CREAD);
99 
100  // Set the baud rates to 115200...
101  cfsetispeed(&options, B115200);
102  cfsetospeed(&options, B115200);
103 
104  // No input processing
105  options.c_iflag = 0;
106 
107  // No output processing
108  options.c_oflag = 0;
109 
110  // No line processing
111  options.c_lflag = 0;
112 
113  // read timeout
114  options.c_cc[VMIN] = 0; // non-blocking
115  options.c_cc[VTIME] = 1; // always return after 0.1 seconds
116 
117  // Set the new options for the port...
118  tcsetattr(*(int *) handle, TCSAFLUSH, &options);
119 
120  return 0;
121 }
122 
123 int roch_driver::WriteData(void *handle, const char *buffer, int length)
124 {
125 
126  int n = write(*(int *) handle, buffer, length);
127 
128  if (n < 0)
129  {
130  fprintf(stderr, "Error in serial write\r\n");
131  return -1;
132  }
133  //serial port output monitor
134 
135 #ifdef TX_DEBUG
136  printf("TX:");
137  int i;
138  for (i=0; i<length; ++i) printf(" %x", (unsigned char)(buffer[i]));
139  printf("\r\n");
140 
141 #endif
142  return n;
143 }
144 
145 
146 int roch_driver::ReadData(void *handle, char *buffer, int length)
147 {
148  int bytesRead = read(*(int *) handle, buffer, length);
149 
150  if (bytesRead <= 0)
151  {
152  return 0;
153  }
154 
155  // serial port input monitor
156 #ifdef RX_DEBUG1
157  printf("RX:");
158  int i;
159  for (i=0; i<bytesRead; ++i) printf(" %x", (unsigned char)buffer[i]);
160  printf("\r\n");
161 #endif
162  return bytesRead;
163 }
164 
165 int roch_driver::CloseSerial(void *handle)
166 {
167  if (NULL == handle)
168  {
169  return 0;
170  }
171  close(*(int *) handle);
172  free(handle);
173  return 0;
174 }
175 
176 };
177 
178 #endif // LINUX_SERIAL
int SetupSerial(void *handle)
int OpenSerial(void **handle, const char *port_name)
int CloseSerial(void *handle)
int WriteData(void *handle, const char *buffer, int length)
int ReadData(void *handle, char *buffer, int length)


roch_base
Author(s): Mike Purvis , Paul Bovbel , Chen
autogenerated on Mon Jun 10 2019 14:41:13