-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialport.cpp
251 lines (223 loc) · 5.94 KB
/
serialport.cpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <stdio.h> /*Standard Input&Output defintion*/
#include <iostream>
#include <time.h>
#include <cmath>
#include<stdlib.h> /*Standard function libray defintion*/
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<termios.h>
#include<errno.h>
#include<string.h>
#include"serialport.h"
#define FALSE -1
#define TRUE 0
using namespace std;
SerialPort::SerialPort(){
PortName = "/dev/ttyUSB0";
speed = 115200;
flow_ctrl = 0;
databits = 8;
stopbits = 1;
parity = 'N';
data_len = 9;
}
SerialPort::SerialPort(char *pn){
PortName = pn;
speed = 115200;
flow_ctrl = 0;
databits = 8;
stopbits = 1;
parity = 'N';
data_len = 9;
}
SerialPort::~SerialPort(){
}
int SerialPort::UART0_Set(){
int i;
int status;
int speed_arr[] = { B115200, B19200, B9600, B4800, B2400, B1200, B300 };
int name_arr[] = { 115200, 19200, 9600, 4800, 2400, 1200, 300 };
struct termios options;
/*tcgetattr(fd,&options)µÃµœÓëfdÖžÏò¶ÔÏóµÄÏà¹Ø²ÎÊý£¬²¢œ«ËüÃDZ£ŽæÓÚoptions,žÃº¯Êý»¹¿ÉÒÔ²âÊÔÅäÖÃÊÇ·ñÕýÈ·£¬žÃŽ®¿ÚÊÇ·ñ¿ÉÓõȡ£Èôµ÷ÓóɹŠ£¬º¯Êý·µ»ØֵΪ0£¬Èôµ÷ÓÃʧ°Ü£¬º¯Êý·µ»ØֵΪ1.
*/
if (tcgetattr(fd, &options) != 0)
{
perror("SetupSerial 1");
return(FALSE);
}
//ÉèÖÃŽ®¿ÚÊäÈ벚ÌØÂʺÍÊä³ö²šÌØÂÊ
for (i = 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if (speed == name_arr[i])
{
cfsetispeed(&options, speed_arr[i]);
cfsetospeed(&options, speed_arr[i]);
}
}
//ÐޞĿØÖÆģʜ£¬±£Ö€³ÌÐò²»»áÕŒÓÃŽ®¿Ú
options.c_cflag |= CLOCAL;
//ÐޞĿØÖÆģʜ£¬Ê¹µÃÄܹ»ŽÓŽ®¿ÚÖжÁÈ¡ÊäÈëÊýŸÝ
options.c_cflag |= CREAD;
//ÉèÖÃÊýŸÝÁ÷¿ØÖÆ
switch (flow_ctrl)
{
case 0://²»Ê¹ÓÃÁ÷¿ØÖÆ
options.c_cflag &= ~CRTSCTS;
break;
case 1://ʹÓÃÓ²ŒþÁ÷¿ØÖÆ
options.c_cflag |= CRTSCTS;
break;
case 2://ʹÓÃÈíŒþÁ÷¿ØÖÆ
options.c_cflag |= IXON | IXOFF | IXANY;
break;
}
//ÉèÖÃÊýŸÝλ
//ÆÁ±ÎÆäËû±ê֟λ
options.c_cflag &= ~CSIZE;
switch (databits)
{
case 5:
options.c_cflag |= CS5;
break;
case 6:
options.c_cflag |= CS6;
break;
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr, "Unsupported data size\n");
return (FALSE);
}
//ÉèÖÃУÑéλ
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 (FALSE);
}
// ÉèÖÃֹͣλ
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB; break;
case 2:
options.c_cflag |= CSTOPB; break;
default:
fprintf(stderr, "Unsupported stop bits\n");
return (FALSE);
}
//ÐÞžÄÊä³öģʜ£¬ÔÊŒÊýŸÝÊä³ö
options.c_oflag &= ~OPOST;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);//ÎҌӵÄ
//options.c_lflag &= ~(ISIG | ICANON);
//ÉèÖõȎýʱŒäºÍ×îСœÓÊÕ×Ö·û
options.c_cc[VTIME] = 1; /* ¶ÁÈ¡Ò»žö×Ö·ûµÈŽý1*(1/10)s */
options.c_cc[VMIN] = 1; /* ¶ÁÈ¡×Ö·ûµÄ×îÉÙžöÊýΪ1 */
//Èç¹û·¢ÉúÊýŸÝÒç³ö£¬œÓÊÕÊýŸÝ£¬µ«ÊDz»ÔÙ¶ÁÈ¡ Ë¢ÐÂÊÕµœµÄÊýŸÝµ«ÊDz»¶Á
tcflush(fd, TCIFLUSH);
//Œ€»îÅäÖà (œ«ÐޞĺóµÄtermiosÊýŸÝÉèÖõœŽ®¿ÚÖУ©
if (tcsetattr(fd, TCSANOW, &options) != 0)
{
perror("com set error!\n");
return (FALSE);
}
return (TRUE);
}
int SerialPort::UART0_Init(){
int err;
//ÉèÖÃŽ®¿ÚÊýŸÝÖ¡žñÊœ
if (UART0_Set() == FALSE)
{
return FALSE;
}
else
{
return TRUE;
}
}
void SerialPort::Open(){
int open_code;
fd = open(PortName, O_RDWR | O_NOCTTY | O_NDELAY);
if (FALSE == fd || -1 == fd)
{
perror("Can't Open Serial Port");
open_code = FALSE;
}
if (fcntl(fd, F_SETFL, 0) < 0)
{
printf("fcntl failed!\n");
open_code = FALSE;
}
int err;
// cout<<"fd :"<<open_code<<endl;
do
{ err = UART0_Init();
// printf("Set Port Exactly!\n");
} while (FALSE == err || FALSE == open_code);
}
int SerialPort::Recv(char *rcv_buf){
int len, fs_sel;
fd_set fs_read;
struct timeval time;
FD_ZERO(&fs_read);
FD_SET(fd, &fs_read);
time.tv_sec = 1000;
time.tv_usec = 0;
fs_sel = select(fd + 1, &fs_read, NULL, NULL, &time);
// fs_sel = 1;
if (fs_sel)
{
len = read(fd, rcv_buf, data_len);
// printf("I am right!(version1.2) len = %d fs_sel = %d\n", len, fs_sel);
return len;
}
else
{
printf("Sorry,I am wrong!");
return FALSE;
}
}
int SerialPort::Send(char *send_buf)
{
int len = 0;
len = write(fd, send_buf, data_len);
if (len == data_len)
{
return len;
}
else
{
tcflush(fd, TCOFLUSH);
return FALSE;
}
}
void SerialPort::Close(){
close(fd);
}