-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcom.cpp
726 lines (661 loc) · 20.5 KB
/
com.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#include "com.h"
namespace com{
void USART::USART_OPEN() {
this->fd = open(this->file_name, O_RDWR | O_NOCTTY | O_NDELAY);
#if IF_THROW_EXCEPTION
if(this->fd == -1){
this->if_success = false;
COUT_RED_START
printf("Error! in Opening %s \n", this->file_name);
COUT_COLOR_END
} else{
#if IF_PRINT_INFORMATION
COUT_GREEN_START
printf("%s Opened Successfully \n", this->file_name);
COUT_COLOR_END
#endif
}
#endif
}
void USART::USART_CLOSE() {
if(fd != -1){
close(this->fd);
#if IF_PRINT_INFORMATION
COUT_BLUE_START
printf(" +----------------------------------+\n");
printf(" | Serial Port Close |\n");
printf(" +----------------------------------+\n");
COUT_COLOR_END
#endif
}
}
void USART::USART_INFORMATION() {
assert(this->if_success);
#if IF_PRINT_INFORMATION
COUT_YELLOW_START
switch (this->speed) {
case B115200:
printf("BaudRate = 115200 \n");
break;
case B19200:
printf("BaudRate = 19200 \n");
break;
case B9600:
printf("BaudRate = 9600 \n");
break;
case B4800:
printf("BaudRate = 4800 \n");
break;
case B2400:
printf("BaudRate = 2400 \n");
break;
case B1200:
printf("BaudRate = 1200 \n");
break;
case B300:
printf("BaudRate = 300 \n");
break;
}
printf("StopBits = %d \n", this->stopbits);
switch (this->parity) {
case 0:
printf("Parity = none \n");
break;
case 1:
printf("Parity = odd \n");
break;
case 2:
printf("Parity = even \n");
break;
case 3:
printf("Parity = space \n");
break;
}
if(this->receivable){
printf("Receive Data\n");
}else{
printf("Refuse Data\n");
}
COUT_COLOR_END
#endif
}
void USART::USART_SET() {
if(!this->if_success){
return;
}
/* Error Checking */
if (tcgetattr(fd, &this->options)){
this->if_success = false;
#if IF_PRINT_INFORMATION
COUT_RED_START
printf("Error in SetupsSerial !\n");
COUT_COLOR_END
#endif
}
if(!this->if_success){
return;
}
/* 设置串口输入波特率和输出波特率 */
cfsetispeed(&this->options, this->speed);
cfsetospeed(&this->options, this->speed);
/* 修改控制模式,保证程序不会占用串口 */
options.c_cflag |= CLOCAL;
/* 修改控制模式,使得能够从串口中读取输入数据 */
options.c_cflag |= CREAD;
/* 设置数据流控制 */
switch (this->flow_ctrl) {
/* 不使用数据流控制 */
case 0:
this->options.c_cflag &= ~CRTSCTS;
break;
/* 使用硬件数据流控制 */
case 1:
this->options.c_cflag |= CRTSCTS;
break;
/* 使用软件数据流控制 */
case 2:
this->options.c_cflag |= IXON | IXOFF | IXANY;
break;
}
/* 设置数据位,屏蔽其他标志位 */
this->options.c_cflag &= ~CSIZE;
switch (this->databits) {
case 5:
this->options.c_cflag |= CS5;
break;
case 6:
this->options.c_cflag |= CS6;
break;
case 7:
this->options.c_cflag |= CS7;
break;
case 8:
this->options.c_cflag |= CS8;
break;
}
/* 设置校验位 */
switch (this->parity) {
/* 无奇偶校验位 */
case 0:
this->options.c_cflag &= ~PARENB;
this->options.c_iflag &= ~INPCK;
break;
/* 设置为奇校验 */
case 1:
this->options.c_cflag |= (PARODD | PARENB);
this->options.c_iflag |= INPCK;
break;
/* 设置为偶校验 */
case 2:
this->options.c_cflag != PARENB;
this->options.c_cflag &= ~PARODD;
this->options.c_iflag |= INPCK;
break;
/* 设置为空格 */
case 3:
this->options.c_cflag &= ~PARENB;
this->options.c_cflag &= ~CSTOPB;
break;
}
/* 设置停止位 */
switch (this->stopbits) {
case 1:
this->options.c_cflag &= ~CSTOPB;
break;
case 2:
this->options.c_cflag |= CSTOPB;
break;
}
/* 设置操作模式 */
this->options.c_oflag &= ~OPOST;
this->options.c_oflag &= ~(ONLCR | OCRNL);
this->options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
this->options.c_iflag &= ~(ICRNL | INLCR);
this->options.c_iflag &= ~(IXON | IXOFF | IXANY);
/* 另一种配置 */
// this->options.c_cflag |= CLOCAL | CREAD;
// this->options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
// this->options.c_oflag &= ~OPOST;
// this->options.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
/* 设置等待时间和最小接收字符 */
/* 读取一个字符等待1*(1/10)s */
this->options.c_cc[VTIME] = 1;
/* 读取字符的最少个数为1 */
this->options.c_cc[VMIN] = 1;
/* 如果发生数据溢出,接收数据,但是不再读取 刷新收到的数据但是不读 */
tcflush(this->fd,TCIFLUSH);
/* 激活配置 */
if(tcsetattr(this->fd, TCSANOW, &this->options) != 0){
this->if_success = false;
#if IF_THROW_EXCEPTION
COUT_RED_START
printf("com set error!\n");
COUT_COLOR_END
#endif
}
if(!this->if_success){
return;
}
this->USART_SET_RECEIVE();
}
void USART::USART_INIT() {
#if IF_PRINT_INFORMATION
COUT_BLUE_START
printf("Serial Port Init\n");
COUT_COLOR_END
#endif
this->USART_OPEN();
this->USART_SET();
#if IF_PRINT_INFORMATION
if(this->if_success){
this->USART_INFORMATION();
}
#endif
}
void USART::USART_SEND(std::string str) {
const char* write_buffer = str.c_str();
const unsigned char* u_write_buffer = (const unsigned char*)write_buffer;
write(this->fd, write_buffer, strlen(write_buffer));
#if IF_PRINT_INFORMATION
COUT_YELLOW_START
printf("send data: %s", write_buffer);
COUT_COLOR_END
#endif
}
void USART::USART_SEND(unsigned char u_write_buffer[]) {
std::string str(reinterpret_cast<char*>(u_write_buffer));
write(this->fd, u_write_buffer, str.length());
#if IF_PRINT_INFORMATION
COUT_YELLOW_START
printf("send data: %s", u_write_buffer);
COUT_COLOR_END
#endif
}
void USART::USART_SEND(char write_buffer[]){
const unsigned char* u_write_buffer = (const unsigned char*)write_buffer;
int l = strlen(write_buffer);
write(this->fd, write_buffer, strlen(write_buffer));
#if IF_PRINT_INFORMATION
COUT_YELLOW_START
printf("send data: %s", write_buffer);
COUT_COLOR_END
#endif
}
void USART::USART_SET_ReceiveCallback_Normal(Normal_ReceiveCallback normal_receiveCallback) {
this->normal_receiveCallback = normal_receiveCallback;
}
void USART::USART_SET_ReceiveCallback_Frame(Frame_ReceiveCallback frame_ReceiveCallback) {
this->frame_ReceiveCallback = frame_ReceiveCallback;
}
void USART::USART_SET_SPEED(int set_speed) {
bool is_legal = false;
for(size_t i = 0; i < sizeof(this->speed_arr)/sizeof(this->speed_arr[0]); i++){
if(this->speed_arr[i] == set_speed){
is_legal = true;
}
}
if(is_legal){
this->speed = set_speed;
#if IF_PRINT_INFORMATION
COUT_GREEN_START
printf("set speed success!\n");
COUT_COLOR_END
#endif
} else{
#if IF_THROW_EXCEPTION
COUT_RED_START
printf("illegal variable!\n");
COUT_COLOR_END
#endif
}
}
void USART::USART_SET_FLOW_CTRL(int set_flow_ctrl) {
bool is_legal = false;
for (size_t i = 0; i < sizeof(this->flow_ctrl_arr) / sizeof(this->flow_ctrl_arr[0]); i++) {
if (this->flow_ctrl_arr[i] == set_flow_ctrl) {
is_legal = true;
}
}
if (is_legal) {
this->flow_ctrl = set_flow_ctrl;
#if IF_PRINT_INFORMATION
COUT_GREEN_START
printf("set flow ctrl success!\n");
COUT_COLOR_END
#endif
} else {
#if IF_THROW_EXCEPTION
COUT_RED_START
printf("illegal variable!\n");
COUT_COLOR_END
#endif
}
}
void USART::USART_SET_DATABITS(int set_databits) {
bool is_legal = false;
for (size_t i = 0; i < sizeof(this->databits_arr) / sizeof(this->databits_arr[0]); i++) {
if (this->databits_arr[i] == set_databits) {
is_legal = true;
}
}
if (is_legal) {
this->databits = set_databits;
#if IF_PRINT_INFORMATION
COUT_GREEN_START
printf("set databits success!\n");
COUT_COLOR_END
#endif
} else {
#if IF_THROW_EXCEPTION
COUT_RED_START
printf("illegal variable!\n");
COUT_COLOR_END
#endif
}
}
void USART::USART_SET_PARITY(int set_parity) {
bool is_legal = false;
for (size_t i = 0; i < sizeof(this->parity_arr) / sizeof(this->parity_arr[0]); i++) {
if (this->parity_arr[i] == set_parity) {
is_legal = true;
}
}
if (is_legal) {
this->parity = set_parity;
#if IF_PRINT_INFORMATION
COUT_GREEN_START
printf("set parity success!\n");
COUT_COLOR_END
#endif
} else {
#if IF_THROW_EXCEPTION
COUT_RED_START
printf("illegal variable!\n");
COUT_COLOR_END
#endif
}
}
void USART::USART_SET_STOPBITS(int set_stopbits) {
bool is_legal = false;
for (size_t i = 0; i < sizeof(this->stopbits_arr) / sizeof(this->stopbits_arr[0]); i++) {
if (this->stopbits_arr[i] == set_stopbits) {
is_legal = true;
break;
}
}
if (is_legal) {
this->stopbits = set_stopbits;
#if IF_PRINT_INFORMATION
COUT_GREEN_START
printf("set stopbits success!\n");
COUT_COLOR_END
#endif
} else {
#if IF_THROW_EXCEPTION
COUT_RED_START
printf("illegal variable!\n");
COUT_COLOR_END
#endif
}
}
void FRAME::FRAME_ADD_ELEMENT(int type) {
switch(type){
case is_char:
this->format.emplace(this->send_data_size, is_char);
this->send_data_size += 1;
break;
case is_int:
this->format.emplace(this->send_data_size, is_int);
this->send_data_size += 1;
break;
case is_float:
this->format.emplace(this->send_data_size, is_float);
this->send_data_size += 4;
break;
case is_double:
this->format.emplace(this->send_data_size, is_double);
this->send_data_size += 8;
break;
default:
#if IF_THROW_EXCEPTION
COUT_RED_START
printf("illegal variable type!\n");
COUT_COLOR_END
#endif
return;
}
this->send_data_part += 1;
}
void FRAME::FRAME_PRINT_ELEMENTS() {
COUT_YELLOW_START
for(auto iter = this->format.begin();iter != this->format.end(); ++iter){
switch (iter->second) {
case is_char:
std::cout << iter->first << '~' << iter->first << ' ' << "is char variable" << std::endl;
break;
case is_int:
std::cout << iter->first << '~' << iter->first << ' ' << "is int variable" << std::endl;
break;
case is_float:
std::cout << iter->first << '~' << iter->first + 3 << ' ' << "is float variable" << std::endl;
break;
case is_double:
std::cout << iter->first << '~' << iter->first + 7 << ' ' << "is double variable" << std::endl;
break;
}
}
COUT_COLOR_END
}
void FRAME::FRAME_ADD_DATA(char data) {
auto iter = this->format.begin() ;
for(size_t i = 0;i < this->write_index;i++){
iter++;
}
if(iter->second != is_char){
#if IF_PRINT_INFORMATION
COUT_RED_START
printf("illegal char variable!\n");
COUT_COLOR_END
#endif
return;
} else{
this->write_buffer[iter->first] = data;
}
this->write_index++;
if(this->write_index == this->send_data_part){
this->write_buffer[this->send_data_size] = '\0';
this->write_index = 0;
}
}
void FRAME::FRAME_ADD_DATA(std::string data) {
auto iter = this->format.begin();
for(size_t i = 0;i < this->write_index;i++){
iter++;
}
if(iter->second != is_char){
#if IF_PRINT_INFORMATION
COUT_RED_START
printf("illegal char variable variable!\n");
COUT_COLOR_END
#endif
return;
} else{
this->write_buffer[iter->first] = data[0];
}
this->write_index++;
}
void FRAME::FRAME_ADD_DATA(int data) {
auto iter = this->format.begin() ;
for(size_t i = 0;i < this->write_index;i++){
iter++;
}
if(iter->second != is_int){
#if IF_PRINT_INFORMATION
COUT_RED_START
printf("illegal int variable!\n");
COUT_COLOR_END
#endif
return;
} else{
char s = std::to_string(data)[0];
this->write_buffer[iter->first] = s;
}
this->write_index++;
if(this->write_index == this->send_data_part){
this->write_buffer[this->send_data_size] = '\0';
this->write_index = 0;
}
}
void FRAME::FRAME_ADD_DATA(float data) {
auto iter = this->format.begin() ;
for(size_t i = 0;i < this->write_index;i++){
iter++;
}
if(iter->second != is_float){
#if IF_PRINT_INFORMATION
COUT_RED_START
printf("illegal float variable!\n");
COUT_COLOR_END
#endif
return;
} else{
char *p = (char *)(&data);
this->write_buffer[iter->first] = *(p);
this->write_buffer[iter->first + 1] = *(p + 1);
this->write_buffer[iter->first + 2] = *(p + 2);
this->write_buffer[iter->first + 3] = *(p + 3);
}
this->write_index++;
if(this->write_index == this->send_data_part){
this->write_buffer[this->send_data_size] = '\0';
this->write_index = 0;
}
}
void FRAME::FRAME_ADD_DATA(double data) {
auto iter = this->format.begin() ;
for(size_t i = 0;i < this->write_index;i++){
iter++;
}
if(iter->second != is_double){
#if IF_PRINT_INFORMATION
COUT_RED_START
printf("illegal double variable!\n");
COUT_COLOR_END
#endif
return;
} else{
char *p = (char *)(&data);
this->write_buffer[iter->first] = *(p);
this->write_buffer[iter->first + 1] = *(p + 1);
this->write_buffer[iter->first + 2] = *(p + 2);
this->write_buffer[iter->first + 3] = *(p + 3);
this->write_buffer[iter->first + 4] = *(p + 4);
this->write_buffer[iter->first + 5] = *(p + 5);
this->write_buffer[iter->first + 6] = *(p + 6);
this->write_buffer[iter->first + 7] = *(p + 7);
}
this->write_index++;
if(this->write_index == this->send_data_part){
this->write_buffer[this->send_data_size] = '\0';
this->write_index = 0;
}
}
void FRAME::FRAME_SHOW_DATA() {
COUT_YELLOW_START
for(auto iter = this->format.begin();iter != this->format.end(); ++iter){
switch (iter->second) {
case is_char:
std::cout << this->write_buffer[iter->first] << ' ';
break;
case is_int:
std::cout << this->write_buffer[iter->first] << ' ';
break;
case is_float:
std::cout << *(float *)(&this->write_buffer[iter->first]) << ' ';
break;
case is_double:
std::cout << *(double *)(&this->write_buffer[iter->first]) << ' ';
break;
}
}
std::cout << std::endl;
COUT_COLOR_END
}
void USART::USART_SEND_FRAME(char *write_buff, int send_size, int send_times) {
for(size_t i = 0; i < send_times;i++){
write(this->fd, write_buff, send_size);
}
}
void USART::USART_GET_FRAME(FRAME &frame) {
this->receive_frame = frame;
}
void USART::USART_SET_COM_NAME(std::string str) {
this->file_name = str.c_str();
}
void USART::USART_SET_IF_RECEIVE(int if_receive) {
if(if_receive){
this->receivable = true;
} else{
this->receivable = false;
}
}
void FRAME::FRAME_SEND_DATA(USART &usart, unsigned int times) {
usart.USART_SEND_FRAME(this->write_buffer, this->send_data_size + 1,times);
#if IF_PRINT_INFORMATION
this->FRAME_SHOW_DATA();
#endif
}
void USART::USART_SET_RECEIVE() {
if(this->receivable){
if(this->if_use_frame_mode){
std::thread([&]{
char* receiveData = new char[this->receive_Maxlength];
int receivedLength = 0;
int selectResult = -1;
while (this->receivable){
memset(receiveData,0,this->receive_Maxlength);
/* block util data received */
receivedLength = read(this->fd, receiveData, this->receive_Maxlength);
if(receivedLength > 0){
if(nullptr != this->frame_ReceiveCallback){
/* 数据对齐 */
// while(*receiveData != '\0'){
// receiveData++;
// }
// receiveData++;
int l = strlen(receiveData);
#if IF_PRINT_INFORMATION
COUT_CYAN_START
for(auto iter = this->receive_frame.format.begin();iter != this->receive_frame.format.end(); ++iter){
switch (iter->second) {
case is_char:
std::cout << receiveData[iter->first] << ' ';
break;
case is_int:
std::cout << receiveData[iter->first] << ' ';
break;
case is_float:
std::cout << *(float *)(&receiveData[iter->first]) << ' ';
break;
case is_double:
std::cout << *(double *)(&receiveData[iter->first]) << ' ';
break;
}
}
std::cout << std::endl;
COUT_COLOR_END
#endif
this->frame_ReceiveCallback(receiveData, receivedLength, this->receive_frame);
}
}
receivedLength = 0;
}
delete[] receiveData;
receiveData = nullptr;
}).detach();
} else{
std::thread([&]{
char* receiveData = new char[this->receive_Maxlength];
int receivedLength = 0;
int selectResult = -1;
while (this->receivable){
memset(receiveData,0,this->receive_Maxlength);
/* block util data received */
receivedLength = read(this->fd, receiveData, this->receive_Maxlength);
if(receivedLength > 0){
if(nullptr != this->normal_receiveCallback){
this->normal_receiveCallback(receiveData, receivedLength);
}
}
receivedLength = 0;
}
delete[] receiveData;
receiveData = nullptr;
}).detach();
this->if_success = 1;
}
}
}
void USART::USART_SET_RECEIVER_MODE(bool if_use_frame_mode) {
this->if_use_frame_mode = if_use_frame_mode;
}
void RECEIVE_DATA::RECEIVE_DATA_PUSH_VALUE(int data) {
this->receive_data[this->receive_data_size] = (void *)(&data);
this->receive_data_size++;
}
void RECEIVE_DATA::RECEIVE_DATA_PUSH_VALUE(char data) {
this->receive_data[this->receive_data_size] = (void *)(&data);
this->receive_data_size++;
}
void RECEIVE_DATA::RECEIVE_DATA_PUSH_VALUE(float data) {
this->receive_data[this->receive_data_size] = (void *)(&data);
this->receive_data_size++;
}
void RECEIVE_DATA::RECEIVE_DATA_PUSH_VALUE(double data) {
this->receive_data[this->receive_data_size] = (void *)(&data);
this->receive_data_size++;
}
void *RECEIVE_DATA::RECEIVE_DATA_GET_VAlUE(int index) {
return this->receive_data[index];
}
}