implement serial port config in BlinkenOutputWin
Stefan Schuermans

Stefan Schuermans commited on 2019-11-10 13:54:57
Showing 1 changed files, with 89 additions and 21 deletions.

... ...
@@ -12,40 +12,103 @@
12 12
 
13 13
 #include <BlinkenLib/BlinkenLib.h>
14 14
 
15
+struct s_serial_settings {
16
+  char *str; // pointer into argv[]
17
+  DWORD BaudRate;
18
+  DWORD fParity : 1;
19
+  BYTE ByteSize;
20
+  BYTE Parity;
21
+  BYTE StopBits;
22
+};
23
+
15 24
 // get serial settings from text
16
-static int serial_settings_parse(char *str)
25
+static int serial_settings_parse(char *str,
26
+                                 struct s_serial_settings *serial_settings)
17 27
 {
18 28
   int baud, data, stop;
19 29
   char parity;
20 30
 
31
+  serial_settings->str = str;
32
+
21 33
   // split and parse settings string
22 34
   if (sscanf(str, "%i,%c,%i,%i", &baud, &parity, &data, &stop) != 4)
23 35
     return 0;
24 36
 
25 37
   // baud rate
26
-  if (baud != 300 &&
27
-      baud != 600 &&
28
-      baud != 1200 &&
29
-      baud != 2400 &&
30
-      baud != 4800 &&
31
-      baud != 9600 &&
32
-      baud != 19200 && baud != 38400 && baud != 57600 && baud != 115200) {
38
+  switch (baud) {
39
+    case 300:
40
+      serial_settings->BaudRate = CBR_300;
41
+      break;
42
+    case 600:
43
+      serial_settings->BaudRate = CBR_600;
44
+      break;
45
+    case 1200:
46
+      serial_settings->BaudRate = CBR_1200;
47
+      break;
48
+    case 2400:
49
+      serial_settings->BaudRate = CBR_2400;
50
+      break;
51
+    case 4800:
52
+      serial_settings->BaudRate = CBR_4800;
53
+      break;
54
+    case 9600:
55
+      serial_settings->BaudRate = CBR_9600;
56
+      break;
57
+    case 19200:
58
+      serial_settings->BaudRate = CBR_19200;
59
+      break;
60
+    case 57600:
61
+      serial_settings->BaudRate = CBR_57600;
62
+      break;
63
+    case 115200:
64
+      serial_settings->BaudRate = CBR_115200;
65
+      break;
66
+    default:
33 67
       printf("illegal baudrate: %d\n", baud);
34 68
       return 0;
35 69
   }
36 70
   // parity
37
-  if (parity != 'n' && parity != 'N' &&
38
-      parity != 'e' && parity != 'E' && parity != 'o' && parity != 'O') {
71
+  switch (parity) {
72
+    case 'n':
73
+    case 'N':
74
+      serial_settings->fParity = 0;
75
+      serial_settings->Parity = NOPARITY;
76
+      break;
77
+    case 'e':
78
+    case 'E':
79
+      serial_settings->fParity = 1;
80
+      serial_settings->Parity = EVENPARITY;
81
+      break;
82
+    case 'o':
83
+    case 'O':
84
+      serial_settings->fParity = 1;
85
+      serial_settings->Parity = ODDPARITY;
86
+      break;
87
+    default:
39 88
       printf("invalid parity: %c\n", parity);
40 89
       return 0;
41 90
   }
42 91
   // data bits
43
-  if (data != 5 && data != 6 && data != 7 && data != 8) {
92
+  switch (data) {
93
+    case 5:
94
+    case 6:
95
+    case 7:
96
+    case 8:
97
+      serial_settings->ByteSize = data;
98
+      break;
99
+    default:
44 100
       printf("illegal number of data bits: %d\n", data);
45 101
       return 0;
46 102
   }
47 103
   // stop bits
48
-  if (stop != 1 && stop == 2) {
104
+  switch (stop) {
105
+    case 1:
106
+      serial_settings->StopBits = ONESTOPBIT;
107
+      break;
108
+    case 2:
109
+      serial_settings->StopBits = TWOSTOPBITS;
110
+      break;
111
+    default:
49 112
       printf("illegal number of stop bits: %d\n", stop);
50 113
       return 0;
51 114
   }
... ...
@@ -54,7 +117,7 @@ static int serial_settings_parse(char *str)
54 117
 }
55 118
 
56 119
 // set serial settings for fd
57
-static int serial_settings_set(HANDLE hDev, char *settings)
120
+static int serial_settings_set(HANDLE hDev, struct s_serial_settings *settings)
58 121
 {
59 122
   DCB PortDcb;
60 123
   COMMTIMEOUTS CommTimeouts;
... ...
@@ -68,7 +131,11 @@ static int serial_settings_set(HANDLE hDev, char *settings)
68 131
     printf("error building comm state: error %lu\n", GetLastError());
69 132
     return 0;
70 133
   }
71
-  (void) settings; // TODO: set PortDcb fields
134
+  PortDcb.BaudRate = settings->BaudRate;
135
+  PortDcb.fParity = settings->fParity;
136
+  PortDcb.ByteSize = settings->ByteSize;
137
+  PortDcb.Parity = settings->Parity;
138
+  PortDcb.StopBits = settings->StopBits;
72 139
   if (!SetCommState(hDev, &PortDcb)) {
73 140
     printf("error setting device comm state: error %lu\n", GetLastError());
74 141
     return 0;
... ...
@@ -208,7 +275,8 @@ static int recv_and_out(SOCKET udpSocket, HANDLE hDev,
208 275
 // returns error code (not for device-errors, 0 for success)
209 276
 static int open_and_output(SOCKET udpSocket, char *device,
210 277
                            int *p_device_output_active,
211
-                           int serial_settings_change, char *serial_settings,
278
+                           int serial_settings_change,
279
+                           struct s_serial_settings *serial_settings,
212 280
                            unsigned int format_change,
213 281
                            unsigned int format_height,
214 282
                            unsigned int format_width,
... ...
@@ -231,7 +299,7 @@ static int open_and_output(SOCKET udpSocket, char *device,
231 299
   if (serial_settings_change) {
232 300
     if (!serial_settings_set(hDev, serial_settings)) {
233 301
       if (*p_device_output_active)
234
-        printf("could not set serial port to \"%s\"\n", serial_settings);
302
+        printf("could not set serial port to \"%s\"\n", serial_settings->str);
235 303
       CloseHandle(hDev);
236 304
       return 0;
237 305
     }
... ...
@@ -252,7 +320,8 @@ static int open_and_output(SOCKET udpSocket, char *device,
252 320
 // returns error code (not for device-errors, 0 for success)
253 321
 static int open_and_output_loop(SOCKET udpSocket, char *device,
254 322
                                 int serial_settings_change,
255
-                                char *serial_settings, int reopen_device,
323
+                                struct s_serial_settings *serial_settings,
324
+                                int reopen_device,
256 325
                                 unsigned int reopen_device_ms,
257 326
                                 unsigned int format_change,
258 327
                                 unsigned int format_height,
... ...
@@ -305,7 +374,7 @@ int main(int argCnt, char **args)
305 374
   WSADATA WsaData;
306 375
   int i, bound;
307 376
   SOCKET udpSocket;
308
-  char *serial_settings;
377
+  struct s_serial_settings serial_settings = {};
309 378
   etBlinkenProto proto;
310 379
   unsigned int format_change, format_height, format_width, format_channels,
311 380
       format_colors;
... ...
@@ -468,9 +537,8 @@ int main(int argCnt, char **args)
468 537
     else if (strcmp(args[i], "-s") == 0) {
469 538
       if (i + 1 < argCnt) {
470 539
         i++;
471
-        if (serial_settings_parse(args[i])) {
540
+        if (serial_settings_parse(args[i], &serial_settings)) {
472 541
           serial_settings_change = 1;
473
-          serial_settings = args[i];
474 542
         } else
475 543
           printf("invalid serial settings \"%s\"\n", args[i]);
476 544
       } else
... ...
@@ -513,7 +581,7 @@ int main(int argCnt, char **args)
513 581
   }
514 582
   // open device and output frames in a loop
515 583
   open_and_output_loop(udpSocket, device,
516
-                       serial_settings_change, serial_settings,
584
+                       serial_settings_change, &serial_settings,
517 585
                        reopen_device, reopen_device_ms,
518 586
                        format_change, format_height, format_width,
519 587
                        format_channels, format_colors, proto);
520 588