2 This file is part of AutoQuad.
4 AutoQuad is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 AutoQuad is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with AutoQuad. If not, see <http://www.gnu.org/licenses/>.
16 Copyright © 2011 Bill Nesbitt
29 #include <sys/select.h>
32 serialStruct_t
*initSerial(const char *port
, unsigned int baud
, char ctsRts
) {
34 struct termios options
;
37 s
= (serialStruct_t
*)calloc(1, sizeof(serialStruct_t
));
39 s
->fd
= open(port
, O_RDWR
| O_NOCTTY
| O_NDELAY
);
46 fcntl(s
->fd
, F_SETFL
, 0); // clear all flags on descriptor, enable direct I/O
48 // bzero(&options, sizeof(options));
49 // memset(&options, 0, sizeof(options));
50 tcgetattr(s
->fd
, &options
);
82 options
.c_cflag
= brate
;
84 cfsetispeed(&options
, baud
);
85 cfsetospeed(&options
, baud
);
88 options
.c_cflag
|= CRTSCTS
| CS8
| CLOCAL
| CREAD
;
89 options
.c_iflag
= IGNPAR
;
90 options
.c_iflag
&= (~(IXON
|IXOFF
|IXANY
)); // turn off software flow control
93 /* set input mode (non-canonical, no echo,...) */
96 options
.c_cc
[VTIME
] = 0; /* inter-character timer unused */
97 options
.c_cc
[VMIN
] = 1; /* blocking read until 1 chars received */
100 options
.c_cflag
|= CCTS_OFLOW
;
104 options
.c_cflag
&= ~(CRTSCTS
); // turn off hardware flow control
106 // set the new port options
107 tcsetattr(s
->fd
, TCSANOW
, &options
);
112 void serialFree(serialStruct_t
*s
) {
120 void serialNoParity(serialStruct_t
*s
) {
121 struct termios options
;
123 tcgetattr(s
->fd
, &options
); // read serial port options
125 options
.c_cflag
&= ~(PARENB
| CSTOPB
);
127 tcsetattr(s
->fd
, TCSANOW
, &options
);
130 void serialEvenParity(serialStruct_t
*s
) {
131 struct termios options
;
133 tcgetattr(s
->fd
, &options
); // read serial port options
135 options
.c_cflag
|= (PARENB
);
136 options
.c_cflag
&= ~(PARODD
| CSTOPB
);
138 tcsetattr(s
->fd
, TCSANOW
, &options
);
141 void serialWriteChar(serialStruct_t
*s
, unsigned char c
) {
144 ret
= write(s
->fd
, &c
, 1);
147 void serialWrite(serialStruct_t
*s
, const char *str
, unsigned int len
) {
150 ret
= write(s
->fd
, str
, len
);
153 unsigned char serialAvailable(serialStruct_t
*s
) {
155 struct timeval timeout
;
158 FD_SET(s
->fd
, &fdSet
);
159 memset((char *)&timeout
, 0, sizeof(timeout
));
161 if (select(s
->fd
+1, &fdSet
, 0, 0, &timeout
) == 1)
167 void serialFlush(serialStruct_t
*s
) {
168 while (serialAvailable(s
))
172 unsigned char serialRead(serialStruct_t
*s
) {
176 ret
= read(s
->fd
, &c
, 1);