-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.c
76 lines (57 loc) · 1.7 KB
/
serial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* serial.c
General serial device handling, not instrument specific
*/
#include "serial.h"
struct termios old_tio;
struct termios tio;
/* Setup RS232 connection parameters and return file descriptor */
void initialize_serial(int baudrate, int data, int parity, int stopbit, int hardware_fc)
{
memset(&tio,0,sizeof(tio));
/* Ignore bytes with parity errors and map CR to NL */
/*tio.c_iflag = IGNPAR | ICRNL; */
tio.c_iflag = IGNBRK | IGNPAR | ICRNL;
/* Raw output */
tio.c_oflag = 0;
/* CS8 = 8N1 (8 bit, no parity, 1 stop bit)
CLOCAL = local connection, no modem control
CREAD = enable receiving characters
CRTSCTS = enable hardware flow control */
tio.c_cflag = CS8 | CREAD | CLOCAL | CRTSCTS;
tio.c_lflag = 0;
/* Blocking read until 1 character arrives */
tio.c_cc[VMIN] = 0;
/* Inter-character timer unused */
tio.c_cc[VTIME] = 0;
cfsetospeed(&tio,baudrate);
cfsetispeed(&tio,baudrate);
}
int open_serial (char *device)
{
int fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd == -1)
{
printf("open_port(): Unable to open %s\n", device);
return -1;
} else {
fcntl(fd, F_SETFL, 0);
printf("Connection to %s open.\n", device);
/* Save current serial port settings */
tcgetattr(fd,&old_tio);
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&tio);
return fd;
}
}
/* read bytes from serial connection with file descriptor fd. */
/* return output as pointer to buffer */
int read_serial(int fd, char *buffer, size_t size)
{
int n;
if ((n = read(fd, buffer, size)) < 0)
return -1;
else {
buffer[n] = 0;
return n;
}
}