-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserial.c
More file actions
executable file
·164 lines (152 loc) · 3.58 KB
/
serial.c
File metadata and controls
executable file
·164 lines (152 loc) · 3.58 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include "datatype.h"
#include "serial.h"
extern WINDOW *console;
static int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,
B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,};
static int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300,
115200, 38400, 19200, 9600, 4800, 2400, 1200, 300,};
static void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for (i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
if (speed == name_arr[i]) {
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0) {
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}
static int set_mode(int fd,int vmin,int vtime)
{
struct termios options;
if (tcgetattr(fd, &options) != 0) {
perror("SetupSerial 2");
return -1;
}
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = vtime;
options.c_cc[VMIN] = vmin;
options.c_lflag &= ~(ICANON | ECHO | ECHOE |ECHONL | IEXTEN | ISIG);
options.c_oflag &= ~OPOST;
options.c_iflag &= ~(IXON | IXOFF);
options.c_iflag &= ~(BRKINT | ICRNL | INLCR | IXANY |IGNCR);
options.c_oflag &= ~(ONLCR|OCRNL);
if (tcsetattr(fd,TCSANOW,&options) != 0) {
perror("SetupSerial 4");
return -1;
}
tcflush(fd,TCIFLUSH);
return 0;
}
static int set_parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if (tcgetattr(fd, &options) != 0) {
perror("SetupSerial 1");
return -1;
}
options.c_cflag &= ~CSIZE;
switch (databits) {
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return -1;
}
switch (parity) {
case 'n':
case 'N':
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case 'e':
case 'E':
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case 'S':
case 's':
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return -1;
}
switch (stopbits) {
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return -1;
}
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
//tcflush(fd,TCIFLUSH);
if (tcsetattr(fd,TCSANOW,&options) != 0) {
perror("SetupSerial 3");
return -1;
}
//tcflush(fd,TCIFLUSH);
return 0;
}
int serial_open(char *port_name, int speed,int vtime,int vmin)
{
int fd = -1;
fd = open(port_name, O_RDWR);
if (fd < 0) {
print_err(console,"Failed to open %s\n", port_name);
return -1;
}
set_speed(fd, speed);
if (set_parity(fd, 8, 1,'N') < 0) {
print_err(console,"Set Parity Error\n");
return -1;
}
if(set_mode(fd,vmin,vtime)<0){
print_err(console,"Set mode Error\n");
return -1;
}
return fd;
}
void serial_close(int fd)
{
if(fd > 0)
close(fd);
}
int serial_write(int fd, unsigned char *buf, int buf_size)
{
return write(fd, buf, buf_size);
}