Robot Agent  1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
serialport.c
Go to the documentation of this file.
1 
11 /* -- Includes -- */
12 /* system libraries */
13 #include <stdio.h> /* Standard input/output definitions */
14 #include <string.h> /* String function definitions */
15 #include <stdlib.h>
16 #include <unistd.h> /* UNIX standard function definitions */
17 #include <fcntl.h> /* File control definitions */
18 #include <errno.h> /* Error number definitions */
19 #include <termios.h> /* POSIX terminal control definitions */
20 /* project libraries */
21 #include "serialport.h"
22 #include "def.h"
23 
24 /* -- Defines -- */
25 
26 /* -- Functions -- */
27 
33 serialport_t *serialport_open(const char *name)
34 {
35  serialport_t *sps = (serialport_t *) malloc(sizeof(serialport_t)); // Serial port structure
36 
37  strcpy(sps->name, name); // Set name
38  sps->descriptor = open(sps->name, O_RDWR | O_NOCTTY | O_NDELAY);
39 
40  if (sps->descriptor == s_ERROR)
41  {
42  // sCould not open the port.
43  printf("serialport_open: Unable to open port - \"%s\".\n", sps->name);
44  }
45  else
46  {
47  fcntl(sps->descriptor, F_SETFL, 0);
48  }
49 
50  return sps;
51 }
52 
59 {
60  int res; // Result
61 
62  res = close(sps->descriptor);
63 
64  if (res == s_ERROR)
65  {
66  //Could not close the port.
67  printf("serialport_close: Unable to close port - \"%s\".\n", sps->name);
68  }
69 
70  // Free memory
71  free(sps);
72 
73  return res;
74 }
75 
82 int serialport_config(serialport_t *sps, unsigned int baud)
83 {
84  int res; // Result
85  struct termios options;
86  speed_t baudrate;
87 
88  // Get the current options for the serial port
89  tcgetattr(sps->descriptor, &options);
90 
91  // Update baudrate in structure
92  sps->baudrate = baud;
93 
94  // Set the baud rate
95  switch (sps->baudrate) {
96  case 2400 :
97  baudrate = B2400;
98  break;
99  case 4800 :
100  baudrate = B4800;
101  break;
102  case 9600 :
103  baudrate = B9600;
104  break;
105  case 19200 :
106  baudrate = B19200;
107  break;
108  case 38400 :
109  baudrate = B38400;
110  break;
111  case 57600 :
112  baudrate = B57600;
113  break;
114  case 115200 :
115  baudrate = B115200;
116  break;
117  default:
118  printf("serialport_config: Invalid baud rate. Baud rate set to 9600.\n");
119  baudrate = B9600;
120  break;
121  }
122  cfsetispeed(&options, baudrate);
123  cfsetospeed(&options, baudrate);
124 
125  //Enable the receiver and set local mode
126  options.c_cflag |= (CLOCAL | CREAD);
127 
128  // Set 8 data bits / no parity bit / 1 stop bit
129  options.c_cflag &= ~PARENB;
130  options.c_cflag &= ~CSTOPB;
131  options.c_cflag &= ~CSIZE;
132  options.c_cflag |= CS8;
133 
134  // Local options
135  //options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
136  options.c_lflag = 0;
137 
138  // Input options
139  //options.c_iflag &= ~(ICRNL);
140  options.c_iflag = 0;
141 
142  // Output options
143  //options.c_oflag &= ~OPOST;
144  options.c_oflag = 0;
145 
146  // Set the new options for the serial port
147  res = tcsetattr(sps->descriptor, TCSANOW, &options);
148 
149  // Error message
150  if(res == s_ERROR)
151  {
152  printf("serialport_config: Could not apply new configuration.\n");
153  }
154 
155  return res;
156 }
157 
158 
166 int serialport_write(serialport_t *sps, unsigned char *data, unsigned int bytes)
167 {
168  // Write data
169  return write(sps->descriptor, data, bytes);
170 }
171 
178 int serialport_byte(serialport_t *sps, unsigned char byte)
179 {
180  // Write one byte
181  return write(sps->descriptor, &byte, 1);
182 }
183 
190 {
191  // Loacal variables
192  char buffer[1000];
193 
194  // Uselessly read input buffer
195  return read(sps->descriptor, &buffer, sizeof(buffer));
196 }
197 
205 /*int serialport_read(serialport_t *sps, char *data, int bytes)
206 {
207  char *bufptr; // Current char in buffer
208  int nbytes, bytes_left; // Number of bytes read
209 
210  bytes_left = bytes;
211 
212  // Read characters into buffer until we get a CR or NL
213  bufptr = data;
214  while ((nbytes = (int)read(sps->descriptor, bufptr, 1)) > 0)
215  {
216  bufptr += nbytes;
217  bytes_left -= nbytes;
218  if (bytes_left <= 0)
219  break;
220  }
221 
222  // Terminate the string with nul
223  *bufptr = '\0';
224 
225  return 0;
226 }*/
227 
228 
int serialport_flush_input(serialport_t *sps)
Definition: serialport.c:189
unsigned int baudrate
Definition: serialport.h:27
serialport_t * serialport_open(const char *name)
Definition: serialport.c:33
int serialport_write(serialport_t *sps, unsigned char *data, unsigned int bytes)
Definition: serialport.c:166
Serial Port structure.
Definition: serialport.h:23
int serialport_config(serialport_t *sps, unsigned int baud)
Definition: serialport.c:82
int serialport_close(serialport_t *sps)
Definition: serialport.c:58
#define s_ERROR
Definition: def.h:65
int serialport_byte(serialport_t *sps, unsigned char byte)
Definition: serialport.c:178