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


husky_base
Author(s): Mike Purvis , Paul Bovbel
autogenerated on Fri Oct 2 2020 03:40:07