Commit 807c20f08d9da2eaa42c9effd77d4a6838783002
1 parent
2b985f81
Parity setting is now a single char ('N', 'E' or 'O')
Showing
5 changed files
with
28 additions
and
21 deletions
NEWS
| @@ -12,6 +12,7 @@ libmodbus 2.1.1 (2010-XX-XX) | @@ -12,6 +12,7 @@ libmodbus 2.1.1 (2010-XX-XX) | ||
| 12 | Original patch by Sisyph (eric-paul). | 12 | Original patch by Sisyph (eric-paul). |
| 13 | - Fix #591142 - Slave id check should be disabled in TCP connection | 13 | - Fix #591142 - Slave id check should be disabled in TCP connection |
| 14 | Reported by aladdinwu. | 14 | Reported by aladdinwu. |
| 15 | +- Parity setting is now a single char ('N', 'E' or 'O') | ||
| 15 | 16 | ||
| 16 | libmodbus 2.1.0 (2010-03-24) | 17 | libmodbus 2.1.0 (2010-03-24) |
| 17 | ============================ | 18 | ============================ |
src/modbus.c
| @@ -1480,27 +1480,31 @@ void init_common(modbus_param_t *mb_param) | @@ -1480,27 +1480,31 @@ void init_common(modbus_param_t *mb_param) | ||
| 1480 | /* Initializes the modbus_param_t structure for RTU | 1480 | /* Initializes the modbus_param_t structure for RTU |
| 1481 | - device: "/dev/ttyS0" | 1481 | - device: "/dev/ttyS0" |
| 1482 | - baud: 9600, 19200, 57600, 115200, etc | 1482 | - baud: 9600, 19200, 57600, 115200, etc |
| 1483 | - - parity: "even", "odd" or "none" | 1483 | + - parity: 'N' stands for None, 'E' for Even and 'O' for odd |
| 1484 | - data_bits: 5, 6, 7, 8 | 1484 | - data_bits: 5, 6, 7, 8 |
| 1485 | - stop_bits: 1, 2 | 1485 | - stop_bits: 1, 2 |
| 1486 | - slave: slave number | 1486 | - slave: slave number |
| 1487 | */ | 1487 | */ |
| 1488 | int modbus_init_rtu(modbus_param_t *mb_param, const char *device, | 1488 | int modbus_init_rtu(modbus_param_t *mb_param, const char *device, |
| 1489 | - int baud, const char *parity, int data_bit, | 1489 | + int baud, char parity, int data_bit, |
| 1490 | int stop_bit, int slave) | 1490 | int stop_bit, int slave) |
| 1491 | { | 1491 | { |
| 1492 | memset(mb_param, 0, sizeof(modbus_param_t)); | 1492 | memset(mb_param, 0, sizeof(modbus_param_t)); |
| 1493 | + init_common(mb_param); | ||
| 1494 | + | ||
| 1493 | strcpy(mb_param->device, device); | 1495 | strcpy(mb_param->device, device); |
| 1494 | mb_param->baud = baud; | 1496 | mb_param->baud = baud; |
| 1495 | - strcpy(mb_param->parity, parity); | 1497 | + if (parity == 'N' || parity == 'E' || parity == 'O') { |
| 1498 | + mb_param->parity = parity; | ||
| 1499 | + } else { | ||
| 1500 | + errno = EINVAL; | ||
| 1501 | + return -1; | ||
| 1502 | + } | ||
| 1496 | mb_param->debug = FALSE; | 1503 | mb_param->debug = FALSE; |
| 1497 | mb_param->data_bit = data_bit; | 1504 | mb_param->data_bit = data_bit; |
| 1498 | mb_param->stop_bit = stop_bit; | 1505 | mb_param->stop_bit = stop_bit; |
| 1499 | mb_param->type_com = RTU; | 1506 | mb_param->type_com = RTU; |
| 1500 | mb_param->error_recovery = FALSE; | 1507 | mb_param->error_recovery = FALSE; |
| 1501 | - | ||
| 1502 | - init_common(mb_param); | ||
| 1503 | - | ||
| 1504 | return modbus_set_slave(mb_param, slave); | 1508 | return modbus_set_slave(mb_param, slave); |
| 1505 | } | 1509 | } |
| 1506 | 1510 | ||
| @@ -1516,6 +1520,8 @@ int modbus_init_rtu(modbus_param_t *mb_param, const char *device, | @@ -1516,6 +1520,8 @@ int modbus_init_rtu(modbus_param_t *mb_param, const char *device, | ||
| 1516 | int modbus_init_tcp(modbus_param_t *mb_param, const char *ip, int port) | 1520 | int modbus_init_tcp(modbus_param_t *mb_param, const char *ip, int port) |
| 1517 | { | 1521 | { |
| 1518 | memset(mb_param, 0, sizeof(modbus_param_t)); | 1522 | memset(mb_param, 0, sizeof(modbus_param_t)); |
| 1523 | + init_common(mb_param); | ||
| 1524 | + | ||
| 1519 | strncpy(mb_param->ip, ip, sizeof(char)*16); | 1525 | strncpy(mb_param->ip, ip, sizeof(char)*16); |
| 1520 | mb_param->port = port; | 1526 | mb_param->port = port; |
| 1521 | mb_param->type_com = TCP; | 1527 | mb_param->type_com = TCP; |
| @@ -1523,8 +1529,6 @@ int modbus_init_tcp(modbus_param_t *mb_param, const char *ip, int port) | @@ -1523,8 +1529,6 @@ int modbus_init_tcp(modbus_param_t *mb_param, const char *ip, int port) | ||
| 1523 | /* Can be changed after to reach remote serial Modbus device */ | 1529 | /* Can be changed after to reach remote serial Modbus device */ |
| 1524 | mb_param->slave = 0xFF; | 1530 | mb_param->slave = 0xFF; |
| 1525 | 1531 | ||
| 1526 | - init_common(mb_param); | ||
| 1527 | - | ||
| 1528 | return 0; | 1532 | return 0; |
| 1529 | } | 1533 | } |
| 1530 | 1534 | ||
| @@ -1600,8 +1604,9 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) | @@ -1600,8 +1604,9 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) | ||
| 1600 | speed_t speed; | 1604 | speed_t speed; |
| 1601 | 1605 | ||
| 1602 | if (mb_param->debug) { | 1606 | if (mb_param->debug) { |
| 1603 | - printf("Opening %s at %d bauds (%s)\n", | ||
| 1604 | - mb_param->device, mb_param->baud, mb_param->parity); | 1607 | + printf("Opening %s at %d bauds (%c, %d, %d)\n", |
| 1608 | + mb_param->device, mb_param->baud, mb_param->parity, | ||
| 1609 | + mb_param->data_bit, mb_param->stop_bit); | ||
| 1605 | } | 1610 | } |
| 1606 | 1611 | ||
| 1607 | /* The O_NOCTTY flag tells UNIX that this program doesn't want | 1612 | /* The O_NOCTTY flag tells UNIX that this program doesn't want |
| @@ -1710,13 +1715,15 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) | @@ -1710,13 +1715,15 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) | ||
| 1710 | 1715 | ||
| 1711 | /* PARENB Enable parity bit | 1716 | /* PARENB Enable parity bit |
| 1712 | PARODD Use odd parity instead of even */ | 1717 | PARODD Use odd parity instead of even */ |
| 1713 | - if (strncmp(mb_param->parity, "none", 4) == 0) { | 1718 | + if (mb_param->parity == 'N') { |
| 1719 | + /* None */ | ||
| 1714 | tios.c_cflag &=~ PARENB; | 1720 | tios.c_cflag &=~ PARENB; |
| 1715 | - } else if (strncmp(mb_param->parity, "even", 4) == 0) { | 1721 | + } else if (mb_param->parity == 'E') { |
| 1722 | + /* Even */ | ||
| 1716 | tios.c_cflag |= PARENB; | 1723 | tios.c_cflag |= PARENB; |
| 1717 | tios.c_cflag &=~ PARODD; | 1724 | tios.c_cflag &=~ PARODD; |
| 1718 | } else { | 1725 | } else { |
| 1719 | - /* odd */ | 1726 | + /* Odd */ |
| 1720 | tios.c_cflag |= PARENB; | 1727 | tios.c_cflag |= PARENB; |
| 1721 | tios.c_cflag |= PARODD; | 1728 | tios.c_cflag |= PARODD; |
| 1722 | } | 1729 | } |
| @@ -1778,7 +1785,8 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) | @@ -1778,7 +1785,8 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) | ||
| 1778 | IUCLC Map uppercase to lowercase | 1785 | IUCLC Map uppercase to lowercase |
| 1779 | IMAXBEL Echo BEL on input line too long | 1786 | IMAXBEL Echo BEL on input line too long |
| 1780 | */ | 1787 | */ |
| 1781 | - if (strncmp(mb_param->parity, "none", 4) == 0) { | 1788 | + if (mb_param->parity == 'N') { |
| 1789 | + /* None */ | ||
| 1782 | tios.c_iflag &= ~INPCK; | 1790 | tios.c_iflag &= ~INPCK; |
| 1783 | } else { | 1791 | } else { |
| 1784 | tios.c_iflag |= INPCK; | 1792 | tios.c_iflag |= INPCK; |
src/modbus.h.in
| @@ -227,8 +227,8 @@ typedef struct { | @@ -227,8 +227,8 @@ typedef struct { | ||
| 227 | uint8_t data_bit; | 227 | uint8_t data_bit; |
| 228 | /* Stop bit */ | 228 | /* Stop bit */ |
| 229 | uint8_t stop_bit; | 229 | uint8_t stop_bit; |
| 230 | - /* Parity: "even", "odd", "none" */ | ||
| 231 | - char parity[5]; | 230 | + /* Parity: 'N', 'O', 'E' */ |
| 231 | + char parity; | ||
| 232 | /* In error_treat with TCP, do a reconnect or just dump the error */ | 232 | /* In error_treat with TCP, do a reconnect or just dump the error */ |
| 233 | uint8_t error_recovery; | 233 | uint8_t error_recovery; |
| 234 | /* IP address */ | 234 | /* IP address */ |
| @@ -251,7 +251,7 @@ typedef struct { | @@ -251,7 +251,7 @@ typedef struct { | ||
| 251 | } modbus_mapping_t; | 251 | } modbus_mapping_t; |
| 252 | 252 | ||
| 253 | int modbus_init_rtu(modbus_param_t *mb_param, const char *device, | 253 | int modbus_init_rtu(modbus_param_t *mb_param, const char *device, |
| 254 | - int baud, const char *parity, int data_bit, | 254 | + int baud, char parity, int data_bit, |
| 255 | int stop_bit, int slave); | 255 | int stop_bit, int slave); |
| 256 | int modbus_init_tcp(modbus_param_t *mb_param, const char *ip_address, int port); | 256 | int modbus_init_tcp(modbus_param_t *mb_param, const char *ip_address, int port); |
| 257 | int modbus_set_slave(modbus_param_t *mb_param, int slave); | 257 | int modbus_set_slave(modbus_param_t *mb_param, int slave); |
tests/random-test-master.c
| @@ -58,9 +58,8 @@ int main(void) | @@ -58,9 +58,8 @@ int main(void) | ||
| 58 | uint16_t *tab_rp_registers; | 58 | uint16_t *tab_rp_registers; |
| 59 | modbus_param_t mb_param; | 59 | modbus_param_t mb_param; |
| 60 | 60 | ||
| 61 | - /* RTU parity : none, even, odd */ | ||
| 62 | /* | 61 | /* |
| 63 | - modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1, MY_ID); | 62 | + modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, 'N', 8, 1, MY_ID); |
| 64 | */ | 63 | */ |
| 65 | 64 | ||
| 66 | /* TCP */ | 65 | /* TCP */ |
tests/unit-test-master.c
| @@ -39,9 +39,8 @@ int main(void) | @@ -39,9 +39,8 @@ int main(void) | ||
| 39 | struct timeval timeout_begin_old; | 39 | struct timeval timeout_begin_old; |
| 40 | struct timeval timeout_begin_new; | 40 | struct timeval timeout_begin_new; |
| 41 | 41 | ||
| 42 | - /* RTU parity : none, even, odd */ | ||
| 43 | /* | 42 | /* |
| 44 | - modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1, | 43 | + modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, 'N', 8, 1, |
| 45 | CLIENT_ID); | 44 | CLIENT_ID); |
| 46 | */ | 45 | */ |
| 47 | 46 |