rs232.c
Go to the documentation of this file.
1 /*
2 ***************************************************************************
3 *
4 * Author: Teunis van Beelen
5 *
6 * Copyright (C) 2005, 2006, 2007, 2008, 2009 Teunis van Beelen
7 *
8 * teuniz@gmail.com
9 *
10 ***************************************************************************
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation version 2 of the License.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 ***************************************************************************
26 *
27 * This version of GPL is at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
28 *
29 ***************************************************************************
30 */
31 
32 
33 
34 #include "rs232.h"
35 
36 
37 
38 #ifdef __linux__ /* Linux */
39 
40 
41 int Cport,
42  error;
43 
44 struct termios new_port_settings,
45  old_port_settings;
46 
47 int OpenComport(char *comport, int baudrate)
48 {
49  int baudr;
50 
51  switch(baudrate)
52  {
53  case 50 : baudr = B50;
54  break;
55  case 75 : baudr = B75;
56  break;
57  case 110 : baudr = B110;
58  break;
59  case 134 : baudr = B134;
60  break;
61  case 150 : baudr = B150;
62  break;
63  case 200 : baudr = B200;
64  break;
65  case 300 : baudr = B300;
66  break;
67  case 600 : baudr = B600;
68  break;
69  case 1200 : baudr = B1200;
70  break;
71  case 1800 : baudr = B1800;
72  break;
73  case 2400 : baudr = B2400;
74  break;
75  case 4800 : baudr = B4800;
76  break;
77  case 9600 : baudr = B9600;
78  break;
79  case 19200 : baudr = B19200;
80  break;
81  case 38400 : baudr = B38400;
82  break;
83  case 57600 : baudr = B57600;
84  break;
85  case 115200 : baudr = B115200;
86  break;
87  case 230400 : baudr = B230400;
88  break;
89  case 460800 : baudr = B460800;
90  break;
91  case 500000 : baudr = B500000;
92  break;
93  case 576000 : baudr = B576000;
94  break;
95  case 921600 : baudr = B921600;
96  break;
97  case 1000000 : baudr = B1000000;
98  break;
99  default : printf("invalid baudrate\n");
100  return(1);
101  break;
102  }
103 
104  Cport = open(comport, O_RDWR | O_NOCTTY | O_NDELAY);
105  if(Cport==-1)
106  {
107  perror("unable to open comport ");
108  return(1);
109  }
110 
111  error = tcgetattr(Cport, &old_port_settings);
112  if(error==-1)
113  {
114  close(Cport);
115  perror("unable to read portsettings ");
116  return(1);
117  }
118  memset(&new_port_settings, 0, sizeof(new_port_settings)); /* clear the new struct */
119 
120  new_port_settings.c_cflag = baudr | CS8 | CLOCAL | CREAD;
121  new_port_settings.c_iflag = IGNPAR;
122  new_port_settings.c_oflag = 0;
123  new_port_settings.c_lflag = 0;
124  new_port_settings.c_cc[VMIN] = 0; /* block untill n bytes are received */
125  new_port_settings.c_cc[VTIME] = 0; /* block untill a timer expires (n * 100 mSec.) */
126  error = tcsetattr(Cport, TCSANOW, &new_port_settings);
127  if(error==-1)
128  {
129  close(Cport);
130  perror("unable to adjust portsettings ");
131  return(1);
132  }
133 
134  return(0);
135 }
136 
137 
138 int PollComport(unsigned char *buf, int size)
139 {
140  int n;
141 
142 #ifndef __STRICT_ANSI__ /* __STRICT_ANSI__ is defined when the -ansi option is used for gcc */
143  if(size>SSIZE_MAX) size = (int)SSIZE_MAX; /* SSIZE_MAX is defined in limits.h */
144 #else
145  if(size>4096) size = 4096;
146 #endif
147 
148  n = read(Cport, buf, size);
149 
150  return(n);
151 }
152 
153 
154 int SendByte(unsigned char byte)
155 {
156  int n;
157 
158  n = write(Cport, &byte, 1);
159  if(n<0) return(1);
160 
161  return(0);
162 }
163 
164 
165 int SendBuf(unsigned char *buf, int size)
166 {
167  return(write(Cport, buf, size));
168 }
169 
170 
171 void CloseComport()
172 {
173  close(Cport);
174  tcsetattr(Cport, TCSANOW, &old_port_settings);
175 }
176 
177 /*
178 Constant Description
179 TIOCM_LE DSR (data set ready/line enable)
180 TIOCM_DTR DTR (data terminal ready)
181 TIOCM_RTS RTS (request to send)
182 TIOCM_ST Secondary TXD (transmit)
183 TIOCM_SR Secondary RXD (receive)
184 TIOCM_CTS CTS (clear to send)
185 TIOCM_CAR DCD (data carrier detect)
186 TIOCM_CD Synonym for TIOCM_CAR
187 TIOCM_RNG RNG (ring)
188 TIOCM_RI Synonym for TIOCM_RNG
189 TIOCM_DSR DSR (data set ready)
190 */
191 
192 int IsCTSEnabled(int comport_number)
193 {
194  int status;
195 
196  status = ioctl(Cport, TIOCMGET, &status);
197 
198  if(status&TIOCM_CTS) return(1);
199  else return(0);
200 }
201 
202 
203 #else /* windows */
204 
205 
206 HANDLE Cport;
207 
208 char baudr[64];
209 
210 
211 int OpenComport(char *comport, int baudrate)
212 {
213  DCB port_settings;
214  COMMTIMEOUTS Cptimeouts;
215 
216  char comport_path[32];
217  sprintf(comport_path, "\\\\.\\%s", comport);
218 
219  switch(baudrate)
220  {
221  case 110 : strcpy(baudr, "baud=110 data=8 parity=N stop=1");
222  break;
223  case 300 : strcpy(baudr, "baud=300 data=8 parity=N stop=1");
224  break;
225  case 600 : strcpy(baudr, "baud=600 data=8 parity=N stop=1");
226  break;
227  case 1200 : strcpy(baudr, "baud=1200 data=8 parity=N stop=1");
228  break;
229  case 2400 : strcpy(baudr, "baud=2400 data=8 parity=N stop=1");
230  break;
231  case 4800 : strcpy(baudr, "baud=4800 data=8 parity=N stop=1");
232  break;
233  case 9600 : strcpy(baudr, "baud=9600 data=8 parity=N stop=1");
234  break;
235  case 19200 : strcpy(baudr, "baud=19200 data=8 parity=N stop=1");
236  break;
237  case 38400 : strcpy(baudr, "baud=38400 data=8 parity=N stop=1");
238  break;
239  case 57600 : strcpy(baudr, "baud=57600 data=8 parity=N stop=1");
240  break;
241  case 115200 : strcpy(baudr, "baud=115200 data=8 parity=N stop=1");
242  break;
243  case 128000 : strcpy(baudr, "baud=128000 data=8 parity=N stop=1");
244  break;
245  case 230400 : strcpy(baudr, "baud=230400 data=8 parity=N stop=1");
246  break;
247  case 256000 : strcpy(baudr, "baud=256000 data=8 parity=N stop=1");
248  break;
249  case 460800 : strcpy(baudr, "baud=460800 data=8 parity=N stop=1");
250  break;
251  case 500000 : strcpy(baudr, "baud=500000 data=8 parity=N stop=1");
252  break;
253  case 921600 : strcpy(baudr, "baud=921600 data=8 parity=N stop=1");
254  break;
255  case 1000000 : strcpy(baudr, "baud=1000000 data=8 parity=N stop=1");
256  break;
257  default : printf("invalid baudrate\n");
258  return(1);
259  break;
260  }
261 
262  Cport = CreateFileA(comport_path,
263  GENERIC_READ|GENERIC_WRITE,
264  0, /* no share */
265  NULL, /* no security */
266  OPEN_EXISTING,
267  0, /* no threads */
268  NULL); /* no templates */
269 
270  if(Cport==INVALID_HANDLE_VALUE)
271  {
272  printf("unable to open comport\n");
273  return(1);
274  }
275 
276 
277  memset(&port_settings, 0, sizeof(port_settings)); /* clear the new struct */
278  port_settings.DCBlength = sizeof(port_settings);
279 
280  if(!BuildCommDCBA(baudr, &port_settings))
281  {
282  printf("unable to set comport dcb settings\n");
283  CloseHandle(Cport);
284  return(1);
285  }
286 
287  if(!SetCommState(Cport, &port_settings))
288  {
289  printf("unable to set comport cfg settings\n");
290  CloseHandle(Cport);
291  return(1);
292  }
293 
294  Cptimeouts.ReadIntervalTimeout = MAXDWORD;
295  Cptimeouts.ReadTotalTimeoutMultiplier = 0;
296  Cptimeouts.ReadTotalTimeoutConstant = 0;
297  Cptimeouts.WriteTotalTimeoutMultiplier = 0;
298  Cptimeouts.WriteTotalTimeoutConstant = 0;
299 
300  if(!SetCommTimeouts(Cport, &Cptimeouts))
301  {
302  printf("unable to set comport time-out settings\n");
303  CloseHandle(Cport);
304  return(1);
305  }
306 
307  return(0);
308 }
309 
310 
311 int PollComport(unsigned char *buf, int size)
312 {
313  int n;
314 
315  if(size>4096) size = 4096;
316 
317 /* added the void pointer cast, otherwise gcc will complain about */
318 /* "warning: dereferencing type-punned pointer will break strict aliasing rules" */
319 
320  ReadFile(Cport, buf, size, (LPDWORD)((void *)&n), NULL);
321 
322  return(n);
323 }
324 
325 
326 int SendByte(unsigned char byte)
327 {
328  int n;
329 
330  WriteFile(Cport, &byte, 1, (LPDWORD)((void *)&n), NULL);
331 
332  if(n<0) return(1);
333 
334  return(0);
335 }
336 
337 
338 int SendBuf(unsigned char *buf, int size)
339 {
340  int n;
341 
342  if(WriteFile(Cport, buf, size, (LPDWORD)((void *)&n), NULL))
343  {
344  return(n);
345  }
346 
347  return(-1);
348 }
349 
350 
352 {
353  CloseHandle(Cport);
354 }
355 
356 
358 {
359  int status;
360 
361  GetCommModemStatus(Cport, (LPDWORD)((void *)&status));
362 
363  if(status&MS_CTS_ON) return(1);
364  else return(0);
365 }
366 
367 
368 #endif
369 
char baudr[64]
Definition: rs232.c:208
HANDLE Cport
Definition: rs232.c:206
int OpenComport(char *comport, int baudrate)
Definition: rs232.c:211
int PollComport(unsigned char *buf, int size)
Definition: rs232.c:311
int SendBuf(unsigned char *buf, int size)
Definition: rs232.c:338
int SendByte(unsigned char byte)
Definition: rs232.c:326
int IsCTSEnabled()
Definition: rs232.c:357
void CloseComport()
Definition: rs232.c:351


advanced_navigation_driver
Author(s):
autogenerated on Thu Jun 6 2019 19:13:08