/
modbus_rtu.c
executable file
·1036 lines (696 loc) · 22.5 KB
/
modbus_rtu.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
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* modbus_rtu.c
These library of functions are designed to enable a program send and
receive data from a device that communicates using the Modbus protocol.
Copyright (C) 2000 Philip Costigan P.C. SCADA LINK PTY. LTD.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, US
*/
#include <fcntl.h> /* File control definitions */
#include <stdio.h> /* Standard input/output */
#include <string.h>
#include <stdlib.h>
#include <termio.h> /* POSIX terminal control definitions */
#include <sys/time.h> /* Time structures for select() */
#include <unistd.h> /* POSIX Symbolic Constants */
#include <errno.h> /* Error definitions */
#include "modbus_rtu.h"
#define DEBUG /* uncomment to see the data sent and received */
// #define DEBUG_CHITO /* mas comentarios para encontrar el error en recepcion */
int char_interval_timeout;
enum {
FALSE = 0,
TRUE
};
/*************************************************************************
modbus_query( packet, length)
Function to add a checksum to the end of a packet.
Please note that the packet array must be at least 2 fields longer than
string_length.
**************************************************************************/
void modbus_query(unsigned char *packet, size_t string_length)
{
int temp_crc;
/* declaration */
unsigned int crc(unsigned char buf[], int start, int cnt);
temp_crc = crc(packet, 0, string_length);
packet[string_length++] = temp_crc >> 8;
packet[string_length++] = temp_crc & 0x00FF;
}
/***********************************************************************
send_query( file_descriptor, query_string, query_length )
Function to send a query out to a modbus slave.
************************************************************************/
int send_query(int ttyfd, unsigned char *query, size_t string_length)
{
int write_stat;
int status;
#ifdef DEBUG
int i;
#endif
modbus_query(query, string_length);
string_length += 2;
#ifdef DEBUG
// Print to stderr the hex value of each character that is about to be
// sent to the modbus slave.
for (i = 0; i < string_length; i++) {
fprintf(stderr, "[%0.2X]", query[i]);
}
fprintf(stderr, " - string length = %d", string_length);
fprintf(stderr, "\n");
#endif
tcflush(ttyfd, TCIOFLUSH); /* flush the input & output streams */
/* configura la linea RTS para transmision */
ioctl(ttyfd, TIOCMGET, &status);
status |= TIOCM_RTS;
ioctl(ttyfd, TIOCMSET, &status);
write_stat = write(ttyfd, query, string_length);
tcflush(ttyfd, TCIFLUSH); /* maybe not neccesary */
return (write_stat);
}
/*********************************************************************
modbus_response( response_data_array, query_array )
Function to the correct response is returned and that the checksum
is correct.
Returns: string_length if OK
0 if failed
Less than 0 for exception errors
Note: All functions used for sending or receiving data via
modbus return these return values.
**********************************************************************/
int modbus_response(unsigned char *data, unsigned char *query, int fd)
{
int response_length;
unsigned short crc_calc = 0;
unsigned short crc_received = 0;
unsigned char recv_crc_hi;
unsigned char recv_crc_lo;
/* local declaration */
int receive_response(unsigned char *received_string, int ttyfd);
unsigned int crc(unsigned char buf[], int start, int cnt);
response_length = receive_response(data, fd);
if (response_length) {
crc_calc = crc(data, 0, response_length - 2);
recv_crc_hi = (unsigned) data[response_length - 2];
recv_crc_lo = (unsigned) data[response_length - 1];
crc_received = data[response_length - 2];
crc_received = (unsigned) crc_received << 8;
crc_received =
crc_received | (unsigned) data[response_length - 1];
/*********** check CRC of response ************/
if (crc_calc != crc_received) {
fprintf(stderr, "crc error received ");
fprintf(stderr, "%0X - ", crc_received);
fprintf(stderr, "crc_calc %0X\n", crc_calc);
response_length = 0;
}
/********** check for exception response *****/
if (response_length && data[1] != query[1]) {
response_length = 0 - data[2];
}
}
/* FIXME: it does not check for the slave id; jpz */
return (response_length);
}
/***********************************************************************
receive_response( array_for_data )
Function to monitor for the reply from the modbus slave.
This function blocks for timeout seconds if there is no reply.
Returns: Total number of characters received.
***********************************************************************/
int receive_response(unsigned char *received_string, int ttyfd)
{
int rxchar = PORT_FAILURE;
int data_avail = FALSE;
int bytes_received = 0;
int read_stat;
int timeout = 1; /* 1 second */
fd_set rfds;
struct timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = 0;
FD_ZERO(&rfds);
FD_SET(ttyfd, &rfds);
#ifdef DEBUG
fprintf(stderr, "Waiting for response.\n");
#endif
/* wait for a response */
data_avail = select(FD_SETSIZE, &rfds, NULL, NULL, &tv);
if (!data_avail) {
bytes_received = 0;
fprintf(stderr, "Comms time out\n");
}
tv.tv_sec = 0;
tv.tv_usec = char_interval_timeout;
FD_ZERO(&rfds);
FD_SET(ttyfd, &rfds);
while (data_avail) {
/* antes de leer un byte, hacer una pausa de "un byte",
* de lo contrario, otro proceso se apodera de la linea RTS */
//esperaRTS(1);
/* if no character at the buffer wait char_interval_timeout */
/* before accepting end of response */
if (select(FD_SETSIZE, &rfds, NULL, NULL, &tv)) {
read_stat = read(ttyfd, &rxchar, 1);
if (read_stat < 0) {
bytes_received = PORT_FAILURE;
data_avail = FALSE;
} else {
rxchar = rxchar & 0xFF;
received_string[bytes_received++] = rxchar;
}
if (bytes_received >= MAX_RESPONSE_LENGTH) {
bytes_received = PORT_FAILURE;
data_avail = FALSE;
}
#ifdef DEBUG
/* display the hex code of each character received */
fprintf(stderr, "<%0.2X>", rxchar);
#endif
} else {
data_avail = FALSE;
}
}
#ifdef DEBUG
fprintf(stderr, "\n");
#endif
/* Al restar 2 a bytes_received, el crc de retorno es calculado a partir de
* 2 bytes menos, lo que resulta en crc_error en lectura. ¡ELIMINADO!
*/
#if 0
if (bytes_received > 2) {
bytes_received -= 2;
}
#endif
#ifdef DEBUG_CHITO
fprintf(stderr, "receive_response: bytes recibidos: %d\n",
bytes_received);
#endif
return (bytes_received);
}
/***********************************************************************
The following functions construct the required query into
a modbus query packet.
***********************************************************************/
#define REQUEST_QUERY_SIZE 6 /* the following packets require */
#define CHECKSUM_SIZE 2 /* 6 unsigned chars for the packet plus */
/* 2 for the checksum. */
void build_request_packet(int slave, int function, int start_addr,
int count, unsigned char *packet)
{
packet[0] = slave, packet[1] = function;
start_addr -= 1;
packet[2] = start_addr >> 8;
packet[3] = start_addr & 0x00ff;
packet[4] = count >> 8;
packet[5] = count & 0x00ff;
}
/************************************************************************
read_IO_status
read_coil_stat_query and read_coil_stat_response interigate
a modbus slave to get coil status. An array of coils shall be
set to TRUE or FALSE according to the response from the slave.
*************************************************************************/
int read_IO_status(int function, int slave, int start_addr, int count,
int *dest, int dest_size, int ttyfd)
{
/* local declaration */
int read_IO_stat_response(int *dest, int dest_size, int coil_count,
unsigned char *query, int fd);
int status;
unsigned char packet[REQUEST_QUERY_SIZE + CHECKSUM_SIZE];
build_request_packet(slave, function, start_addr, count, packet);
if (send_query(ttyfd, packet, REQUEST_QUERY_SIZE) > -1) {
status = read_IO_stat_response(dest, dest_size,
count, packet, ttyfd);
} else {
status = PORT_FAILURE;
}
return (status);
}
/************************************************************************
read_coil_status
reads the boolean status of coils and sets the array elements
in the destination to TRUE or FALSE
*************************************************************************/
int read_coil_status(int slave, int start_addr, int count,
int *dest, int dest_size, int ttyfd)
{
int function = 0x01;
int status;
status = read_IO_status(function, slave, start_addr, count,
dest, dest_size, ttyfd);
return (status);
}
/************************************************************************
read_input_status
same as read_coil_status but reads the slaves input table.
************************************************************************/
int read_input_status(int slave, int start_addr, int count,
int *dest, int dest_size, int ttyfd)
{
int function = 0x02; /* Function: Read Input Status */
int status;
status = read_IO_status(function, slave, start_addr, count,
dest, dest_size, ttyfd);
return (status);
}
/**************************************************************************
read_IO_stat_response
this function does the work of setting array elements to TRUE
or FALSE.
**************************************************************************/
int read_IO_stat_response(int *dest, int dest_size, int coil_count,
unsigned char *query, int fd)
{
unsigned char data[MAX_RESPONSE_LENGTH];
int raw_response_length;
int temp, i, bit, dest_pos = 0;
int coils_processed = 0;
raw_response_length = modbus_response(data, query, fd);
if (raw_response_length > 0) {
for (i = 0; i < (data[2]) && i < dest_size; i++) {
/* shift reg hi_byte to temp */
temp = data[3 + i];
for (bit = 0x01; bit & 0xff &&
coils_processed < coil_count;) {
if (temp & bit) {
dest[dest_pos] = TRUE;
} else {
dest[dest_pos] = FALSE;
}
coils_processed++;
dest_pos++;
bit = bit << 1;
}
}
}
return (raw_response_length);
}
/************************************************************************
read_registers
read the data from a modbus slave and put that data into an array.
************************************************************************/
int read_registers(int function, int slave, int start_addr, int count,
int *dest, int dest_size, int ttyfd)
{
/* local declaration */
int read_reg_response(int *dest, int dest_size,
unsigned char *query, int fd);
int status;
unsigned char packet[REQUEST_QUERY_SIZE + CHECKSUM_SIZE];
build_request_packet(slave, function, start_addr, count, packet);
if (send_query(ttyfd, packet, REQUEST_QUERY_SIZE) > -1) {
status = read_reg_response(dest, dest_size, packet, ttyfd);
} else {
status = PORT_FAILURE;
}
return (status);
}
/************************************************************************
read_holding_registers
Read the holding registers in a slave and put the data into
an array.
*************************************************************************/
int read_holding_registers(int slave, int start_addr, int count,
int *dest, int dest_size, int ttyfd)
{
int function = 0x03; /* Function: Read Holding Registers */
int status;
if (count > MAX_READ_REGS) {
count = MAX_READ_REGS;
#ifdef DEBUG
fprintf(stderr, "Too many registers requested.\n");
#endif
}
status = read_registers(function, slave, start_addr, count,
dest, dest_size, ttyfd);
return (status);
}
/************************************************************************
read_input_registers
Read the inputg registers in a slave and put the data into
an array.
*************************************************************************/
int read_input_registers(int slave, int start_addr, int count,
int *dest, int dest_size, int ttyfd)
{
int function = 0x04; /* Function: Read Input Reqisters */
int status;
if (count > MAX_INPUT_REGS) {
count = MAX_INPUT_REGS;
#ifdef DEBUG
fprintf(stderr, "Too many input registers requested.\n");
#endif
}
status = read_registers(function, slave, start_addr, count,
dest, dest_size, ttyfd);
return (status);
}
/************************************************************************
read_reg_response
reads the response data from a slave and puts the data into an
array.
************************************************************************/
int read_reg_response(int *dest, int dest_size, unsigned char *query,
int fd)
{
unsigned char data[MAX_RESPONSE_LENGTH];
int raw_response_length;
int temp, i;
raw_response_length = modbus_response(data, query, fd);
if (raw_response_length > 0)
raw_response_length -= 2;
if (raw_response_length > 0) {
for (i = 0;
i < (data[2] * 2) && i < (raw_response_length / 2);
i++) {
/* shift reg hi_byte to temp */
temp = data[3 + i * 2] << 8;
/* OR with lo_byte */
temp = temp | data[4 + i * 2];
dest[i] = temp;
}
}
return (raw_response_length);
}
/***********************************************************************
preset_response
Gets the raw data from the input stream.
***********************************************************************/
int preset_response(unsigned char *query, int fd)
{
unsigned char data[MAX_RESPONSE_LENGTH];
int raw_response_length;
raw_response_length = modbus_response(data, query, fd);
return (raw_response_length);
}
/*************************************************************************
set_single
sends a value to a register in a slave.
**************************************************************************/
int set_single(int function, int slave, int addr, int value, int fd)
{
int status;
unsigned char packet[REQUEST_QUERY_SIZE];
packet[0] = slave;
packet[1] = function;
addr -= 1;
packet[2] = addr >> 8;
packet[3] = addr & 0x00FF;
packet[4] = value >> 8;
packet[5] = value & 0x00FF;
if (send_query(fd, packet, 6) > -1) {
status = preset_response(packet, fd);
} else {
status = PORT_FAILURE;
}
return (status);
}
/*************************************************************************
force_single_coil
turn on or off a single coil on the slave device
*************************************************************************/
int force_single_coil(int slave, int coil_addr, int state, int fd)
{
int function = 0x05;
int status;
if (state)
state = 0xFF00;
status = set_single(function, slave, coil_addr, state, fd);
return (status);
}
/*************************************************************************
preset_single_register
sets a value in one holding register in the slave device
*************************************************************************/
int preset_single_register(int slave, int reg_addr, int value, int fd)
{
int function = 0x06;
int status;
status = set_single(function, slave, reg_addr, value, fd);
return (status);
}
/************************************************************************
set_multiple_coils
Takes an array of ints and sets or resets the coils on a slave
appropriatly.
*************************************************************************/
#define PRESET_QUERY_SIZE 210
int set_multiple_coils(int slave, int start_addr, int coil_count,
int *data, int fd)
{
int byte_count;
int i, bit, packet_size = 6;
int coil_check = 0;
int data_array_pos = 0;
int status;
unsigned char packet[PRESET_QUERY_SIZE];
if (coil_count > MAX_WRITE_COILS) {
coil_count = MAX_WRITE_COILS;
#ifdef DEBUG
fprintf(stderr, "Writing to too many coils.\n");
#endif
}
packet[0] = slave;
packet[1] = 0x0F;
start_addr -= 1;
packet[2] = start_addr >> 8;
packet[3] = start_addr & 0x00FF;
packet[4] = coil_count >> 8;
packet[5] = coil_count & 0x00FF;
byte_count = (coil_count / 8) + 1;
packet[6] = byte_count;
bit = 0x01;
for (i = 0; i < byte_count; i++) {
packet[++packet_size] = 0;
while (bit & 0xFF && coil_check++ < coil_count) {
if (data[data_array_pos++]) {
packet[packet_size] |= bit;
} else {
packet[packet_size] &= ~bit;
}
bit = bit << 1;
}
bit = 0x01;
}
if (send_query(fd, packet, ++packet_size) > -1) {
status = preset_response(packet, fd);
} else {
status = PORT_FAILURE;
}
return (status);
}
/*************************************************************************
preset_multiple_registers
copy the values in an array to an array on the slave.
***************************************************************************/
int preset_multiple_registers(int slave, int start_addr,
int reg_count, int *data, int fd)
{
int byte_count, i, packet_size = 6;
int status;
unsigned char packet[PRESET_QUERY_SIZE];
if (reg_count > MAX_WRITE_REGS) {
reg_count = MAX_WRITE_REGS;
#ifdef DEBUG
fprintf(stderr,
"Trying to write to too many registers.\n");
#endif
}
packet[0] = slave;
packet[1] = 0x10;
start_addr -= 1;
packet[2] = start_addr >> 8;
packet[3] = start_addr & 0x00FF;
packet[4] = reg_count >> 8;
packet[5] = reg_count & 0x00FF;
byte_count = reg_count * 2;
packet[6] = byte_count;
for (i = 0; i < reg_count; i++) {
packet[++packet_size] = data[i] >> 8;
packet[++packet_size] = data[i] & 0x00FF;
}
if (send_query(fd, packet, ++packet_size) > -1) {
status = preset_response(packet, fd);
} else {
status = PORT_FAILURE;
}
return (status);
}
/****************************************************************************
***************************** [ BEGIN: crc ] ******************************
*****************************************************************************
INPUTS:
buf -> Array containing message to be sent to controller. start -> Start of loop in crc counter, usually 0.
cnt -> Amount of bytes in message being sent to controller/
OUTPUTS:
temp -> Returns crc byte for message.
COMMENTS:
This routine calculates the crc high and low byte of a message.
Note that this crc is only used for Modbus, not Modbus+ etc.
****************************************************************************/
unsigned int crc(unsigned char *buf, int start, int cnt)
{
int i, j;
unsigned temp, temp2, flag;
temp = 0xFFFF;
#ifdef DEBUG_CHITO
fprintf(stderr, "crc: string length %d\n", cnt);
#endif
for (i = start; i < cnt; i++) {
temp = temp ^ buf[i];
for (j = 1; j <= 8; j++) {
flag = temp & 0x0001;
temp = temp >> 1;
if (flag)
temp = temp ^ 0xA001;
}
}
/* Reverse byte order. */
temp2 = temp >> 8;
temp = (temp << 8) | temp2;
temp &= 0xFFFF;
#ifdef DEBUG_CHITO
fprintf(stderr, "crc: temp crc %0.2X\n", temp);
#endif
return (temp);
}
/************************************************************************
set_up_comms
This function sets up a serial port for RTU communications to
modbus.
**************************************************************************/
int set_up_comms(char *device, int baud_i, char *parity)
{
int ttyfd;
struct termios settings;
int k, n, status; // jpz
speed_t baud_rate;
#ifdef DEBUG
fprintf(stderr, "opening %s\n", device);
#endif
switch (baud_i) {
case 110:
baud_rate = B110;
char_interval_timeout = TO_B110;
break;
case 300:
baud_rate = B300;
char_interval_timeout = TO_B300;
break;
case 600:
baud_rate = B600;
char_interval_timeout = TO_B600;
break;
case 1200:
baud_rate = B1200;
char_interval_timeout = TO_B1200;
break;
case 2400:
baud_rate = B2400;
char_interval_timeout = TO_B2400;
break;
case 4800:
baud_rate = B4800;
char_interval_timeout = TO_B4800;
break;
case 9600:
case 0:
baud_rate = B9600;
char_interval_timeout = TO_B9600;
break;
case 19200:
baud_rate = B19200;
char_interval_timeout = TO_B19200;
break;
case 38400:
baud_rate = B38400;
char_interval_timeout = TO_B38400;
break;
case 57600:
baud_rate = B57600;
char_interval_timeout = TO_B57600;
break;
case 115200:
baud_rate = B115200;
char_interval_timeout = TO_B115200;
break;
default:
baud_rate = B9600;
char_interval_timeout = TO_B9600;
fprintf(stderr, "Unknown baud rate %d for %s.", baud_i,
device);
}
if ((ttyfd = open(device, O_RDWR)) < 0) {
fprintf(stderr, "Error opening device %s. ", device);
fprintf(stderr, "Error no. %d \n", errno);
exit(1); /* stop the program. This maybe should */
/* be a bit kinder but will do for now. */
}
#ifdef DEBUG
fprintf(stderr, "%s open\n", device);
#endif
/* read your man page for the meaning of all this. # man termios */
/* Its a bit to involved to comment here */
cfsetispeed(&settings, baud_rate); /* Set the baud rate */
cfsetospeed(&settings, baud_rate);
settings.c_line = 0;
settings.c_iflag |= IGNBRK;
settings.c_iflag |= IGNPAR;
settings.c_iflag &= ~PARMRK;
settings.c_iflag &= ~INPCK;
settings.c_iflag &= ~ISTRIP;
settings.c_iflag &= ~INLCR;
settings.c_iflag &= ~IGNCR;
settings.c_iflag &= ~ICRNL;
settings.c_iflag &= ~IUCLC;
settings.c_iflag &= ~IXON;
settings.c_iflag |= IXANY;
settings.c_iflag &= ~IXOFF;
settings.c_iflag &= ~IMAXBEL;
settings.c_oflag |= OPOST;
settings.c_oflag &= ~OLCUC;
settings.c_oflag &= ~ONLCR;
settings.c_oflag &= ~OCRNL;
settings.c_oflag |= ONOCR;
settings.c_oflag &= ~ONLRET;
settings.c_oflag &= ~OFILL;
settings.c_oflag &= ~OFDEL;