serial.cpp
Go to the documentation of this file.
1 /*
2  Serial port access for NTRIP client for POSIX.
3  $Id$
4  Copyright (C) 2008 by Dirk Stöcker <soft@dstoecker.de>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  or read http://www.gnu.org/licenses/gpl.txt
20 */
21 
22 /* system includes */
23 #include <stdio.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <termios.h>
29 #include <unistd.h>
30 #include "serial.h"
31 
32 void SerialFree(struct serial *sn)
33 {
34  if(sn->Stream)
35  {
36  /* reset old settings */
37  tcsetattr(sn->Stream, TCSANOW, &sn->Termios);
38  close(sn->Stream);
39  sn->Stream = 0;
40  }
41 }
42 
43 const char * SerialInit(struct serial *sn,
44 const char *Device, enum SerialBaud Baud, enum SerialStopbits StopBits,
45 enum SerialProtocol Protocol, enum SerialParity Parity,
46 enum SerialDatabits DataBits, int dowrite
47 #ifdef __GNUC__
48 __attribute__((__unused__))
49 #endif /* __GNUC__ */
50 )
51 {
52  struct termios newtermios;
53 
54  if((sn->Stream = open(Device, O_RDWR | O_NOCTTY | O_NONBLOCK)) <= 0)
55  return "could not open serial port";
56  tcgetattr(sn->Stream, &sn->Termios);
57 
58  memset(&newtermios, 0, sizeof(struct termios));
59  newtermios.c_cflag = Baud | StopBits | Parity | DataBits
60  | CLOCAL | CREAD;
61  if(Protocol == SPAPROTOCOL_RTS_CTS)
62  newtermios.c_cflag |= CRTSCTS;
63  else
64  newtermios.c_cflag |= Protocol;
65  newtermios.c_cc[VMIN] = 1;
66  tcflush(sn->Stream, TCIOFLUSH);
67  tcsetattr(sn->Stream, TCSANOW, &newtermios);
68  tcflush(sn->Stream, TCIOFLUSH);
69  fcntl(sn->Stream, F_SETFL, O_NONBLOCK);
70  return 0;
71 }
72 
73 int SerialRead(struct serial *sn, char *buffer, size_t size)
74 {
75  int j = read(sn->Stream, buffer, size);
76  if(j < 0)
77  {
78  if(errno == EAGAIN)
79  return 0;
80  else
81  return j;
82  }
83  return j;
84 }
85 
86 int SerialWrite(struct serial *sn, const char *buffer, size_t size)
87 {
88  int j = write(sn->Stream, buffer, size);
89  if(j < 0)
90  {
91  if(errno == EAGAIN)
92  return 0;
93  else
94  return j;
95  }
96  return j;
97 }
98 
99 
struct termios Termios
Definition: serial.h:27
void SerialFree(struct serial *sn)
Definition: serial.cpp:32
const char * SerialInit(struct serial *sn, const char *Device, enum SerialBaud Baud, enum SerialStopbits StopBits, enum SerialProtocol Protocol, enum SerialParity Parity, enum SerialDatabits DataBits, int dowrite)
Definition: serial.cpp:43
SerialDatabits
Definition: serial.h:15
int SerialRead(struct serial *sn, char *buffer, size_t size)
Definition: serial.cpp:73
Definition: serial.h:25
int SerialWrite(struct serial *sn, const char *buffer, size_t size)
Definition: serial.cpp:86
SerialProtocol
Definition: serial.h:21
SerialBaud
Definition: serial.h:10
SerialParity
Definition: serial.h:19
SerialStopbits
Definition: serial.h:17
int Stream
Definition: serial.h:28


dgps_ros
Author(s):
autogenerated on Wed Jan 20 2021 03:38:34