serial.cc
Go to the documentation of this file.
1 
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <sys/time.h>
22 #include <unistd.h>
23 
24 #include "serial.h"
25 #include <string.h>
26 #include <errno.h>
27 
29  if (tty_fd_slot != -1) {
32  }
33 }
35  tty_fd_slot = -1;
36  strcpy(device_name,"");
37 }
38 
39 
40 /******************************************************************************
41  ***
42  *** Name: open_serial
43  ***
44  *** Purpose: oeffnet ein serielles Device
45  ***
46  *** Parameters: devicename (z.B. "/dev/ttyS4") und flags (z.B. CS8|B9600 )
47  ***
48  *** Returns: filedescriptor, bzw. -1 im Fehlerfall
49  ***
50  ******************************************************************************
51  */
52 
53 int SerialDevice::open_serial (const char *p_tty_name)
54 {
55  struct termio tty_set;
56  int tty_fd = -1;
57 
58  if ( (tty_fd = open(p_tty_name, O_RDWR | O_NOCTTY | O_NDELAY)) < 0 )//NONBLOCK
59  {
60  printf("ERROR: Couldn't open port %s!\n", p_tty_name);
61  perror(NULL);
62  return (-1);
63  }
64 
65  if (ioctl(tty_fd, TCGETA, &tty_set ) == -1 )
66  {
67  printf("ERROR: ioctl() failed to read settings!\n");
68  perror(NULL);
69  return (-1);
70  }
71 
72  tty_set.c_cflag = (B115200 | CS8 | CLOCAL | CREAD);
73  tty_set.c_iflag = IXOFF;
74  tty_set.c_lflag = 0;
75  tty_set.c_oflag = 0;
76  tty_set.c_cc[VTIME] = 20;
77  tty_set.c_cc[VMIN] = 0;
78 
79  if (ioctl(tty_fd, TCSETAW, &tty_set ) == -1 )
80  {
81  printf("ERROR: ioctl() failed to set settings!\n");
82  perror(NULL);
83  return (-1);
84  }
85 
86  tty_fd_slot = tty_fd;
87  strcpy(device_name,p_tty_name);
88  return (tty_fd);
89 }
90 
91 /******************************************************************************
92  ***
93  *** Name: change_baud_serial
94  ***
95  *** Purpose: stellt die Geschwindigkeit der seriellen Schnittstelle um
96  ***
97  *** Parameters: filedescriptor und neue Geschwindigkeit (z.B. B38400)
98  ***
99  *** Returns: STATUS_OK, bzw. STATUS_ERROR im Fehlerfall
100  ***
101  ******************************************************************************
102  */
103 
104 long SerialDevice::change_baud_serial (int tty_fd, speed_t speed)
105  {
106  struct termio tty_set;
107 
108  if (ioctl(tty_fd, TCGETA, &tty_set ) == -1 )
109  {
110  printf("ERROR: ioctl() failed to read settings!\n");
111  perror(NULL);
112  return (STATUS_ERROR);
113  }
114 
115  tty_set.c_cflag &= ~CBAUD;
116  tty_set.c_cflag |= speed;
117 
118  if (ioctl(tty_fd, TCSETAW, &tty_set ) == -1 )
119  {
120  printf("ERROR: ioctl() failed to set settings!\n");
121  perror(NULL);
122  return (STATUS_ERROR);
123  }
124 
125  return (STATUS_OK);
126  }
127 
128 /******************************************************************************
129  ***
130  *** Name: write_serial
131  ***
132  *** Purpose: Schickt Daten an die serielle Schnittstelle
133  ***
134  *** Parameters: filedescriptor, Pointer auf Speicher, Zahl der zu versendenden bytes
135  ***
136  *** Returns: STATUS_OK, bzw. STATUS_ERROR im Fehlerfall
137  ***
138  ******************************************************************************
139  */
140 
141 long SerialDevice::write_serial(int tty_fd, unsigned char *p_buffer, long nb_byte)
142  {
143  int bytes_out;
144 
145  //printf("write serial tty:%d msg:%s nb:%d\n", tty_fd, p_buffer, nb_byte);
146 
147  if (( bytes_out = write(tty_fd, p_buffer, nb_byte)) < nb_byte)
148  {
149  printf("ERROR: write() failed!\n");
150  perror(NULL);
151  return (STATUS_ERROR);
152  };
153 
154  return (STATUS_OK);
155  }
156 
157 /******************************************************************************
158  ***
159  *** Name: read_serial
160  ***
161  *** Purpose: Liest Daten von der seriellen Schnittstelle. Liegen gerade
162  *** keine Daten an, wird eine 20tel Sekunde darauf gewartet!
163  ***
164  *** Parameters: filedescriptor, Pointer auf Speicherbereich, maximale
165  *** Zahl der zu lesenden bytes
166  ***
167  *** Returns: Zahl der gelesenen bytes
168  ***
169  ******************************************************************************
170  */
171 
172 long SerialDevice::read_serial(int tty_fd, unsigned char *p_buffer, long nb_bytes_max)
173  {
174  struct timeval tz;
175  fd_set fds;
176  int tries = 0;
177  int qu_new = 0;
178  int bytes_read = 0;
179  int ready = STATUS_ERROR;
180 
181 
182  /* read max nb_bytes_max bytes */
183 
184  while (tries < 2 && !ready)
185  {
186  tz.tv_sec = 1;
187  tz.tv_usec = 0;
188 
189  FD_ZERO(&fds);
190  FD_SET(tty_fd, &fds);
191  if (select(FD_SETSIZE, &fds, 0, 0, &tz) > 0)
192  {
193  qu_new = read(tty_fd, &p_buffer[bytes_read], (nb_bytes_max - bytes_read));
194  if (qu_new > 0)
195  bytes_read += qu_new;
196  else
197  tries++;
198  ready = (bytes_read == nb_bytes_max);
199  }
200  else
201  tries++;
202  }
203 
204  //printf("read serial tty:%d msg:%s nb:%d\n", tty_fd, p_buffer, bytes_read);
205 
206  return(bytes_read);
207  }
208 
209 /******************************************************************************
210  ***
211  *** Name: close_serial
212  ***
213  *** Purpose: schliesst das serielle device
214  ***
215  *** Parameter: filedescriptor
216  ***
217  ******************************************************************************
218  */
219 
220 void SerialDevice::close_serial (int tty_fd)
221  {
222  close(tty_fd);
223  }
224 
225 /******************************************************************************
226  ***
227  *** Name: wait_for_serial
228  ***
229  *** Purpose: Wartet maximal eine vorgegebene Zeit auf das Eintreffen von
230  *** Daten an der seriellen Schnittstelle
231  ***
232  *** Parameters: filedescriptor, maximale Wartezeit
233  ***
234  *** Returns: STATUS_OK wenn Daten anliegen, bzw. STATUS_ERROR wenn nicht
235  ***
236  ******************************************************************************
237  */
238 
239 long SerialDevice::wait_for_serial(int tty_fd, long max_time_secs)
240  {
241  struct timeval tz;
242  fd_set fds;
243 
244  while (max_time_secs--)
245  {
246  tz.tv_sec = 1;
247  tz.tv_usec = 0;
248 
249  FD_ZERO (&fds);
250  FD_SET(tty_fd, &fds);
251 
252  if (select(FD_SETSIZE, &fds, 0, 0, &tz) > 0)
253  return (STATUS_OK);
254  }
255 
256  return (STATUS_ERROR);
257  }
258 
259 
260 /******************************************************************************
261  ***
262  *** Name: empty_serial
263  ***
264  *** Purpose: Loescht den Eingangspuffer der seriellen Schnittstelle
265  ***
266  *** Caveats: Noch nicht getestet... (see manpage zu termios)
267  ***
268  *** Parameters: filedescriptor
269  ***
270  *** Returns: STATUS_OK, bzw. STATUS_ERROR im Fehlerfall
271  ***
272  ******************************************************************************
273  */
274 
276  {
277  if (-1 == tcflush (tty_fd, TCIFLUSH))
278  return (STATUS_ERROR);
279  else
280  return (STATUS_OK);
281  }
282 
283 
284 
285 
286 
287 
288 
289 
290 
291 
292 
293 
294 
295 
296 
297 
298 
long empty_serial(int tty_fd)
Definition: serial.cc:275
SerialDevice()
Definition: serial.cc:34
long write_serial(int tty_fd, unsigned char *p_buffer, long nb_byte)
Definition: serial.cc:141
int tty_fd_slot
Definition: serial.h:44
void close_serial(int tty_fd)
Definition: serial.cc:220
char device_name[255]
Definition: serial.h:45
long change_baud_serial(int tty_fd, speed_t speed)
Definition: serial.cc:104
~SerialDevice()
Definition: serial.cc:28
long wait_for_serial(int tty_fd, long max_time_secs)
Definition: serial.cc:239
#define STATUS_ERROR
Definition: serial.h:39
#define STATUS_OK
Definition: serial.h:38
int open_serial(const char *p_tty_name)
Definition: serial.cc:53
long read_serial(int tty_fd, unsigned char *p_buffer, long nb_bytes_max)
Definition: serial.cc:172


asr_cyberglove_lib
Author(s): Heller Florian, Meißner Pascal, Nguyen Trung, Yi Xie
autogenerated on Mon Jun 10 2019 12:40:38