Commit 5db0c344469de32535b3a968c28dd8633e8149ba

Authored by Stéphane Raimbault
1 parent bb8dd781

Coding conventions on newly added win32 code

Showing 1 changed file with 112 additions and 100 deletions
src/modbus-rtu.c
@@ -166,30 +166,38 @@ int _modbus_rtu_send_msg_pre(uint8_t *req, int req_length) @@ -166,30 +166,38 @@ int _modbus_rtu_send_msg_pre(uint8_t *req, int req_length)
166 166
167 static void win32_ser_init(struct win32_ser *ws) { 167 static void win32_ser_init(struct win32_ser *ws) {
168 /* Clear everything */ 168 /* Clear everything */
169 - memset(ws,0x00,sizeof(struct win32_ser)); 169 + memset(ws, 0x00, sizeof(struct win32_ser));
  170 +
170 /* Set file handle to invalid */ 171 /* Set file handle to invalid */
171 ws->fd = INVALID_HANDLE_VALUE; 172 ws->fd = INVALID_HANDLE_VALUE;
172 } 173 }
173 174
174 static int win32_ser_select(struct win32_ser *ws, int max_len, struct timeval *tv) { 175 static int win32_ser_select(struct win32_ser *ws, int max_len, struct timeval *tv) {
175 - COMMTIMEOUTS comm_to; unsigned int msec = 0; 176 + COMMTIMEOUTS comm_to;
  177 + unsigned int msec = 0;
  178 +
176 /* Check if some data still in the buffer to be consumed */ 179 /* Check if some data still in the buffer to be consumed */
177 if (ws->n_bytes> 0) { 180 if (ws->n_bytes> 0) {
178 return 1; 181 return 1;
179 } 182 }
  183 +
180 /* Setup timeouts like select() would do */ 184 /* Setup timeouts like select() would do */
181 msec = tv->tv_sec * 1000 + tv->tv_usec / 1000; 185 msec = tv->tv_sec * 1000 + tv->tv_usec / 1000;
182 - if (msec < 1) msec = 1; 186 + if (msec < 1)
  187 + msec = 1;
  188 +
183 comm_to.ReadIntervalTimeout = msec; 189 comm_to.ReadIntervalTimeout = msec;
184 comm_to.ReadTotalTimeoutMultiplier = 0; 190 comm_to.ReadTotalTimeoutMultiplier = 0;
185 comm_to.ReadTotalTimeoutConstant = msec; 191 comm_to.ReadTotalTimeoutConstant = msec;
186 comm_to.WriteTotalTimeoutMultiplier = 0; 192 comm_to.WriteTotalTimeoutMultiplier = 0;
187 comm_to.WriteTotalTimeoutConstant = 1000; 193 comm_to.WriteTotalTimeoutConstant = 1000;
188 - SetCommTimeouts(ws->fd,&comm_to); 194 + SetCommTimeouts(ws->fd, &comm_to);
  195 +
189 /* Read some bytes */ 196 /* Read some bytes */
190 if ((max_len > PY_BUF_SIZE) || (max_len < 0)) { 197 if ((max_len > PY_BUF_SIZE) || (max_len < 0)) {
191 max_len = PY_BUF_SIZE; 198 max_len = PY_BUF_SIZE;
192 } 199 }
  200 +
193 if (ReadFile(ws->fd, &ws->buf, max_len, &ws->n_bytes, NULL)) { 201 if (ReadFile(ws->fd, &ws->buf, max_len, &ws->n_bytes, NULL)) {
194 /* Check if some bytes available */ 202 /* Check if some bytes available */
195 if (ws->n_bytes > 0) { 203 if (ws->n_bytes > 0) {
@@ -207,14 +215,18 @@ static int win32_ser_select(struct win32_ser *ws, int max_len, struct timeval *t @@ -207,14 +215,18 @@ static int win32_ser_select(struct win32_ser *ws, int max_len, struct timeval *t
207 215
208 static int win32_ser_read(struct win32_ser *ws, uint8_t *p_msg, unsigned int max_len) { 216 static int win32_ser_read(struct win32_ser *ws, uint8_t *p_msg, unsigned int max_len) {
209 unsigned int n = ws->n_bytes; 217 unsigned int n = ws->n_bytes;
  218 +
210 if (max_len < n) { 219 if (max_len < n) {
211 n = max_len; 220 n = max_len;
212 } 221 }
  222 +
213 if (n > 0) { 223 if (n > 0) {
214 - memcpy(p_msg,ws->buf,n); 224 + memcpy(p_msg, ws->buf, n);
215 } 225 }
  226 +
216 ws->n_bytes -= n; 227 ws->n_bytes -= n;
217 - return(n); 228 +
  229 + return n;
218 } 230 }
219 #endif 231 #endif
220 232
@@ -290,12 +302,12 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -290,12 +302,12 @@ static int _modbus_rtu_connect(modbus_t *ctx)
290 302
291 /* ctx_rtu->device should contain a string like "COMxx:" xx being a decimal number */ 303 /* ctx_rtu->device should contain a string like "COMxx:" xx being a decimal number */
292 ctx_rtu->w_ser.fd = CreateFileA(ctx_rtu->device, 304 ctx_rtu->w_ser.fd = CreateFileA(ctx_rtu->device,
293 - GENERIC_READ | GENERIC_WRITE,  
294 - 0,  
295 - NULL,  
296 - OPEN_EXISTING,  
297 - 0,  
298 - NULL); 305 + GENERIC_READ | GENERIC_WRITE,
  306 + 0,
  307 + NULL,
  308 + OPEN_EXISTING,
  309 + 0,
  310 + NULL);
299 311
300 /* Error checking */ 312 /* Error checking */
301 if (ctx_rtu->w_ser.fd == INVALID_HANDLE_VALUE) { 313 if (ctx_rtu->w_ser.fd == INVALID_HANDLE_VALUE) {
@@ -317,102 +329,102 @@ static int _modbus_rtu_connect(modbus_t *ctx) @@ -317,102 +329,102 @@ static int _modbus_rtu_connect(modbus_t *ctx)
317 329
318 /* Speed setting */ 330 /* Speed setting */
319 switch (ctx_rtu->baud) { 331 switch (ctx_rtu->baud) {
320 - case 110:  
321 - dcb.BaudRate = CBR_110;  
322 - break;  
323 - case 300:  
324 - dcb.BaudRate = CBR_300;  
325 - break;  
326 - case 600:  
327 - dcb.BaudRate = CBR_600;  
328 - break;  
329 - case 1200:  
330 - dcb.BaudRate = CBR_1200;  
331 - break;  
332 - case 2400:  
333 - dcb.BaudRate = CBR_2400;  
334 - break;  
335 - case 4800:  
336 - dcb.BaudRate = CBR_4800;  
337 - break;  
338 - case 9600:  
339 - dcb.BaudRate = CBR_9600;  
340 - break;  
341 - case 19200:  
342 - dcb.BaudRate = CBR_19200;  
343 - break;  
344 - case 38400:  
345 - dcb.BaudRate = CBR_38400;  
346 - break;  
347 - case 57600:  
348 - dcb.BaudRate = CBR_57600;  
349 - break;  
350 - case 115200:  
351 - dcb.BaudRate = CBR_115200;  
352 - break;  
353 - default:  
354 - dcb.BaudRate = CBR_9600;  
355 - printf("WARNING Unknown baud rate %d for %s (B9600 used)\n",  
356 - ctx_rtu->baud, ctx_rtu->device);  
357 - } 332 + case 110:
  333 + dcb.BaudRate = CBR_110;
  334 + break;
  335 + case 300:
  336 + dcb.BaudRate = CBR_300;
  337 + break;
  338 + case 600:
  339 + dcb.BaudRate = CBR_600;
  340 + break;
  341 + case 1200:
  342 + dcb.BaudRate = CBR_1200;
  343 + break;
  344 + case 2400:
  345 + dcb.BaudRate = CBR_2400;
  346 + break;
  347 + case 4800:
  348 + dcb.BaudRate = CBR_4800;
  349 + break;
  350 + case 9600:
  351 + dcb.BaudRate = CBR_9600;
  352 + break;
  353 + case 19200:
  354 + dcb.BaudRate = CBR_19200;
  355 + break;
  356 + case 38400:
  357 + dcb.BaudRate = CBR_38400;
  358 + break;
  359 + case 57600:
  360 + dcb.BaudRate = CBR_57600;
  361 + break;
  362 + case 115200:
  363 + dcb.BaudRate = CBR_115200;
  364 + break;
  365 + default:
  366 + dcb.BaudRate = CBR_9600;
  367 + printf("WARNING Unknown baud rate %d for %s (B9600 used)\n",
  368 + ctx_rtu->baud, ctx_rtu->device);
  369 + }
358 370
359 - /* Data bits */  
360 - switch (ctx_rtu->data_bit) {  
361 - case 5:  
362 - dcb.ByteSize = 5;  
363 - break;  
364 - case 6:  
365 - dcb.ByteSize = 6;  
366 - break;  
367 - case 7:  
368 - dcb.ByteSize = 7;  
369 - break;  
370 - case 8:  
371 - default:  
372 - dcb.ByteSize = 8;  
373 - break;  
374 - } 371 + /* Data bits */
  372 + switch (ctx_rtu->data_bit) {
  373 + case 5:
  374 + dcb.ByteSize = 5;
  375 + break;
  376 + case 6:
  377 + dcb.ByteSize = 6;
  378 + break;
  379 + case 7:
  380 + dcb.ByteSize = 7;
  381 + break;
  382 + case 8:
  383 + default:
  384 + dcb.ByteSize = 8;
  385 + break;
  386 + }
375 387
376 - /* Stop bits */  
377 - if (ctx_rtu->stop_bit == 1)  
378 - dcb.StopBits = ONESTOPBIT;  
379 - else /* 2 */  
380 - dcb.StopBits = TWOSTOPBITS;  
381 -  
382 - /* Parity */  
383 - if (ctx_rtu->parity == 'N') {  
384 - dcb.Parity = NOPARITY;  
385 - dcb.fParity = FALSE;  
386 - } else if (ctx_rtu->parity == 'E') {  
387 - dcb.Parity = EVENPARITY;  
388 - dcb.fParity = TRUE;  
389 - } else {  
390 - /* odd */  
391 - dcb.Parity = ODDPARITY;  
392 - dcb.fParity = TRUE;  
393 - } 388 + /* Stop bits */
  389 + if (ctx_rtu->stop_bit == 1)
  390 + dcb.StopBits = ONESTOPBIT;
  391 + else /* 2 */
  392 + dcb.StopBits = TWOSTOPBITS;
394 393
395 - /* Hardware handshaking left as default settings retrieved */ 394 + /* Parity */
  395 + if (ctx_rtu->parity == 'N') {
  396 + dcb.Parity = NOPARITY;
  397 + dcb.fParity = FALSE;
  398 + } else if (ctx_rtu->parity == 'E') {
  399 + dcb.Parity = EVENPARITY;
  400 + dcb.fParity = TRUE;
  401 + } else {
  402 + /* odd */
  403 + dcb.Parity = ODDPARITY;
  404 + dcb.fParity = TRUE;
  405 + }
396 406
397 - /* No software handshaking */  
398 - dcb.fTXContinueOnXoff = TRUE;  
399 - dcb.fOutX = FALSE;  
400 - dcb.fInX = FALSE; 407 + /* Hardware handshaking left as default settings retrieved */
401 408
402 - /* Binary mode (it's the only supported on Windows anyway) */  
403 - dcb.fBinary = TRUE; 409 + /* No software handshaking */
  410 + dcb.fTXContinueOnXoff = TRUE;
  411 + dcb.fOutX = FALSE;
  412 + dcb.fInX = FALSE;
404 413
405 - /* Don't want errors to be blocking */  
406 - dcb.fAbortOnError = FALSE; 414 + /* Binary mode (it's the only supported on Windows anyway) */
  415 + dcb.fBinary = TRUE;
407 416
408 - /* TODO: any other flags !? */ 417 + /* Don't want errors to be blocking */
  418 + dcb.fAbortOnError = FALSE;
409 419
410 - /* Setup port */  
411 - if (!SetCommState(ctx_rtu->w_ser.fd, &dcb)) {  
412 - fprintf(stderr, "ERROR Error setting new configuration (LastError %d)\n",  
413 - (int)GetLastError());  
414 - return -1;  
415 - } 420 + /* TODO: any other flags!? */
  421 +
  422 + /* Setup port */
  423 + if (!SetCommState(ctx_rtu->w_ser.fd, &dcb)) {
  424 + fprintf(stderr, "ERROR Error setting new configuration (LastError %d)\n",
  425 + (int)GetLastError());
  426 + return -1;
  427 + }
416 #else 428 #else
417 /* The O_NOCTTY flag tells UNIX that this program doesn't want 429 /* The O_NOCTTY flag tells UNIX that this program doesn't want
418 to be the "controlling terminal" for that port. If you 430 to be the "controlling terminal" for that port. If you