-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial.cpp
More file actions
162 lines (148 loc) · 4.38 KB
/
serial.cpp
File metadata and controls
162 lines (148 loc) · 4.38 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
#include "serial.hpp"
MySerial::MySerial() {
}
MySerial::~MySerial() {
}
int MySerial::set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop) //串口设置
{
struct termios newtio, oldtio;
if (tcgetattr(fd, &oldtio) != 0) {
perror("SetupSerial 1");
return -1;
}
bzero(&newtio, sizeof(newtio));
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch (nBits) {
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
default:
fprintf(stderr, "Unsupported data size.\n");
return -1;
}
/*设置奇偶校验位*/
switch (nEvent) {
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E':
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N':
newtio.c_cflag &= ~PARENB;
break;
default:
fprintf(stderr, "Unsupported parity.\n");
return -1;
}
switch (nSpeed) {
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
switch (nStop) //设置停止位
{
case 1:
newtio.c_cflag &= ~CSTOPB;
break;
case 2:
newtio.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr, "Unsupported stopbits.\n");
return -1;
}
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(fd, TCIFLUSH);
if ((tcsetattr(fd, TCSANOW, &newtio)) != 0) {
perror("com set error");
return -1;
}
return 0;
}
int MySerial::open_port(int comport) //通过参数,打开相应的串口
{
int fd;
if (comport == 1) {
//O_RDWR读写模式 O_NOCTTY如果路径名指向终端设备,不要把这个设备用作控制终端 O_NDELAY表示不关心DCD信号所处的状态(端口的另一端是否激活或者停止)
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if (-1 == fd) {
perror("Can't Open Serial /dev/ttyS0 Port");
return (-1);
}
} else if (comport == 2) {
fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if (-1 == fd) {
perror("Can't Open Serial /dev/ttyS1 Port");
return (-1);
}
} else if (comport == 3) {
fd = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if (-1 == fd) {
perror("Can't Open Serial /dev/ttyS2 Port");
return (-1);
}
}
// if (fcntl(fd, F_SETFL, 0) < 0)
// perror("fcntl failed!");
// else
// printf("fcntl=%d\n", fcntl(fd, F_SETFL, 0));
if (isatty(STDIN_FILENO) == 0)
perror("standard input is not a terminal device");
else
printf("isartty success!\n");
printf("fd-open=%d\n", fd);
return fd;
}
int MySerial::nwrite(int serialfd, const unsigned char *data, int datalength) //写串口信息
{
int len = 0, total_len = 0;
// for (total_len = 0 ; total_len < datalength;)
// {
len = 0;
//len = write(serialfd, &data[total_len], datalength - total_len);
for (int i = 0; i < datalength; i++) {
len += write(serialfd, &data[i], 1);
//usleep(1000);
}
// if (len > 0)
// total_len += len;
// }
return (total_len);
}
int MySerial::nread(int fd, void *data, int datalength) //读取串口信息
{
int readlen = 0;
readlen = static_cast<int>(read(fd, data, datalength));
// if (readlen> 0) {
// char *q= static_cast<char *>(data);
// printf("current condition is %d\n", int(*q));
// }
return readlen;
}