Commit ef7ae05367932ed923058decac0de71a3e1b4456
1 parent
cb692215
Fix setting baud rate for Linux (partial revert of fa20798)
Showing
1 changed file
with
129 additions
and
4 deletions
src/modbus-rtu.c
| @@ -12,9 +12,8 @@ | @@ -12,9 +12,8 @@ | ||
| 12 | #ifndef _MSC_VER | 12 | #ifndef _MSC_VER |
| 13 | #include <unistd.h> | 13 | #include <unistd.h> |
| 14 | #endif | 14 | #endif |
| 15 | -#include <assert.h> | ||
| 16 | - | ||
| 17 | #include "modbus-private.h" | 15 | #include "modbus-private.h" |
| 16 | +#include <assert.h> | ||
| 18 | 17 | ||
| 19 | #include "modbus-rtu-private.h" | 18 | #include "modbus-rtu-private.h" |
| 20 | #include "modbus-rtu.h" | 19 | #include "modbus-rtu.h" |
| @@ -505,11 +504,125 @@ static int _modbus_rtu_connect(modbus_t *ctx) | @@ -505,11 +504,125 @@ static int _modbus_rtu_connect(modbus_t *ctx) | ||
| 505 | return 0; | 504 | return 0; |
| 506 | } | 505 | } |
| 507 | #else | 506 | #else |
| 507 | + | ||
| 508 | +static speed_t _get_termios_speed(int baud, int debug) | ||
| 509 | +{ | ||
| 510 | + speed_t speed; | ||
| 511 | + | ||
| 512 | + switch (baud) { | ||
| 513 | + case 110: | ||
| 514 | + speed = B110; | ||
| 515 | + break; | ||
| 516 | + case 300: | ||
| 517 | + speed = B300; | ||
| 518 | + break; | ||
| 519 | + case 600: | ||
| 520 | + speed = B600; | ||
| 521 | + break; | ||
| 522 | + case 1200: | ||
| 523 | + speed = B1200; | ||
| 524 | + break; | ||
| 525 | + case 2400: | ||
| 526 | + speed = B2400; | ||
| 527 | + break; | ||
| 528 | + case 4800: | ||
| 529 | + speed = B4800; | ||
| 530 | + break; | ||
| 531 | + case 9600: | ||
| 532 | + speed = B9600; | ||
| 533 | + break; | ||
| 534 | + case 19200: | ||
| 535 | + speed = B19200; | ||
| 536 | + break; | ||
| 537 | + case 38400: | ||
| 538 | + speed = B38400; | ||
| 539 | + break; | ||
| 540 | +#ifdef B57600 | ||
| 541 | + case 57600: | ||
| 542 | + speed = B57600; | ||
| 543 | + break; | ||
| 544 | +#endif | ||
| 545 | +#ifdef B115200 | ||
| 546 | + case 115200: | ||
| 547 | + speed = B115200; | ||
| 548 | + break; | ||
| 549 | +#endif | ||
| 550 | +#ifdef B230400 | ||
| 551 | + case 230400: | ||
| 552 | + speed = B230400; | ||
| 553 | + break; | ||
| 554 | +#endif | ||
| 555 | +#ifdef B460800 | ||
| 556 | + case 460800: | ||
| 557 | + speed = B460800; | ||
| 558 | + break; | ||
| 559 | +#endif | ||
| 560 | +#ifdef B500000 | ||
| 561 | + case 500000: | ||
| 562 | + speed = B500000; | ||
| 563 | + break; | ||
| 564 | +#endif | ||
| 565 | +#ifdef B576000 | ||
| 566 | + case 576000: | ||
| 567 | + speed = B576000; | ||
| 568 | + break; | ||
| 569 | +#endif | ||
| 570 | +#ifdef B921600 | ||
| 571 | + case 921600: | ||
| 572 | + speed = B921600; | ||
| 573 | + break; | ||
| 574 | +#endif | ||
| 575 | +#ifdef B1000000 | ||
| 576 | + case 1000000: | ||
| 577 | + speed = B1000000; | ||
| 578 | + break; | ||
| 579 | +#endif | ||
| 580 | +#ifdef B1152000 | ||
| 581 | + case 1152000: | ||
| 582 | + speed = B1152000; | ||
| 583 | + break; | ||
| 584 | +#endif | ||
| 585 | +#ifdef B1500000 | ||
| 586 | + case 1500000: | ||
| 587 | + speed = B1500000; | ||
| 588 | + break; | ||
| 589 | +#endif | ||
| 590 | +#ifdef B2500000 | ||
| 591 | + case 2500000: | ||
| 592 | + speed = B2500000; | ||
| 593 | + break; | ||
| 594 | +#endif | ||
| 595 | +#ifdef B3000000 | ||
| 596 | + case 3000000: | ||
| 597 | + speed = B3000000; | ||
| 598 | + break; | ||
| 599 | +#endif | ||
| 600 | +#ifdef B3500000 | ||
| 601 | + case 3500000: | ||
| 602 | + speed = B3500000; | ||
| 603 | + break; | ||
| 604 | +#endif | ||
| 605 | +#ifdef B4000000 | ||
| 606 | + case 4000000: | ||
| 607 | + speed = B4000000; | ||
| 608 | + break; | ||
| 609 | +#endif | ||
| 610 | + default: | ||
| 611 | + speed = B9600; | ||
| 612 | + if (debug) { | ||
| 613 | + fprintf(stderr, "WARNING Unknown baud rate %d (B9600 used)\n", baud); | ||
| 614 | + } | ||
| 615 | + } | ||
| 616 | + | ||
| 617 | + return speed; | ||
| 618 | +} | ||
| 619 | + | ||
| 508 | /* POSIX */ | 620 | /* POSIX */ |
| 509 | static int _modbus_rtu_connect(modbus_t *ctx) | 621 | static int _modbus_rtu_connect(modbus_t *ctx) |
| 510 | { | 622 | { |
| 511 | struct termios tios; | 623 | struct termios tios; |
| 512 | int flags; | 624 | int flags; |
| 625 | + speed_t speed; | ||
| 513 | modbus_rtu_t *ctx_rtu = ctx->backend_data; | 626 | modbus_rtu_t *ctx_rtu = ctx->backend_data; |
| 514 | 627 | ||
| 515 | if (ctx->debug) { | 628 | if (ctx->debug) { |
| @@ -554,8 +667,20 @@ static int _modbus_rtu_connect(modbus_t *ctx) | @@ -554,8 +667,20 @@ static int _modbus_rtu_connect(modbus_t *ctx) | ||
| 554 | */ | 667 | */ |
| 555 | 668 | ||
| 556 | /* Set the baud rate */ | 669 | /* Set the baud rate */ |
| 557 | - if ((cfsetispeed(&tios, ctx_rtu->baud) < 0) || | ||
| 558 | - (cfsetospeed(&tios, ctx_rtu->baud) < 0)) { | 670 | + |
| 671 | + /* | ||
| 672 | + On MacOS, constants of baud rates are equal to the integer in argument but | ||
| 673 | + that's not the case under Linux so we have to find the corresponding | ||
| 674 | + constant. Until the code is upgraded to termios2, the list of possible | ||
| 675 | + values is limited (no 14400 for example). | ||
| 676 | + */ | ||
| 677 | + if (9600 == B9600) { | ||
| 678 | + speed = ctx_rtu->baud; | ||
| 679 | + } else { | ||
| 680 | + speed = _get_termios_speed(ctx_rtu->baud, ctx->debug); | ||
| 681 | + } | ||
| 682 | + | ||
| 683 | + if ((cfsetispeed(&tios, speed) < 0) || (cfsetospeed(&tios, speed) < 0)) { | ||
| 559 | close(ctx->s); | 684 | close(ctx->s); |
| 560 | ctx->s = -1; | 685 | ctx->s = -1; |
| 561 | return -1; | 686 | return -1; |