Przeglądaj źródła

Parity setting is now a single char ('N', 'E' or 'O')

Stéphane Raimbault 14 lat temu
rodzic
commit
807c20f08d
5 zmienionych plików z 28 dodań i 21 usunięć
  1. 1 0
      NEWS
  2. 22 14
      src/modbus.c
  3. 3 3
      src/modbus.h.in
  4. 1 2
      tests/random-test-master.c
  5. 1 2
      tests/unit-test-master.c

+ 1 - 0
NEWS

@@ -12,6 +12,7 @@ libmodbus 2.1.1 (2010-XX-XX)
   Original patch by Sisyph (eric-paul).
 - Fix #591142 - Slave id check should be disabled in TCP connection
   Reported by aladdinwu.
+- Parity setting is now a single char ('N', 'E' or 'O')
 
 libmodbus 2.1.0 (2010-03-24)
 ============================

+ 22 - 14
src/modbus.c

@@ -1480,27 +1480,31 @@ void init_common(modbus_param_t *mb_param)
 /* Initializes the modbus_param_t structure for RTU
    - device: "/dev/ttyS0"
    - baud:   9600, 19200, 57600, 115200, etc
-   - parity: "even", "odd" or "none"
+   - parity: 'N' stands for None, 'E' for Even and 'O' for odd
    - data_bits: 5, 6, 7, 8
    - stop_bits: 1, 2
    - slave: slave number
 */
 int modbus_init_rtu(modbus_param_t *mb_param, const char *device,
-                     int baud, const char *parity, int data_bit,
+                     int baud, char parity, int data_bit,
                      int stop_bit, int slave)
 {
         memset(mb_param, 0, sizeof(modbus_param_t));
+        init_common(mb_param);
+
         strcpy(mb_param->device, device);
         mb_param->baud = baud;
-        strcpy(mb_param->parity, parity);
+        if (parity == 'N' || parity == 'E' || parity == 'O') {
+                mb_param->parity = parity;
+        } else {
+                errno = EINVAL;
+                return -1;
+        }
         mb_param->debug = FALSE;
         mb_param->data_bit = data_bit;
         mb_param->stop_bit = stop_bit;
         mb_param->type_com = RTU;
         mb_param->error_recovery = FALSE;
-
-        init_common(mb_param);
-
         return modbus_set_slave(mb_param, slave);
 }
 
@@ -1516,6 +1520,8 @@ int modbus_init_rtu(modbus_param_t *mb_param, const char *device,
 int modbus_init_tcp(modbus_param_t *mb_param, const char *ip, int port)
 {
         memset(mb_param, 0, sizeof(modbus_param_t));
+        init_common(mb_param);
+
         strncpy(mb_param->ip, ip, sizeof(char)*16);
         mb_param->port = port;
         mb_param->type_com = TCP;
@@ -1523,8 +1529,6 @@ int modbus_init_tcp(modbus_param_t *mb_param, const char *ip, int port)
         /* Can be changed after to reach remote serial Modbus device */
         mb_param->slave = 0xFF;
 
-        init_common(mb_param);
-
         return 0;
 }
 
@@ -1600,8 +1604,9 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
         speed_t speed;
 
         if (mb_param->debug) {
-                printf("Opening %s at %d bauds (%s)\n",
-                       mb_param->device, mb_param->baud, mb_param->parity);
+                printf("Opening %s at %d bauds (%c, %d, %d)\n",
+                       mb_param->device, mb_param->baud, mb_param->parity,
+                       mb_param->data_bit, mb_param->stop_bit);
         }
 
         /* 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)
 
         /* PARENB       Enable parity bit
            PARODD       Use odd parity instead of even */
-        if (strncmp(mb_param->parity, "none", 4) == 0) {
+        if (mb_param->parity == 'N') {
+                /* None */
                 tios.c_cflag &=~ PARENB;
-        } else if (strncmp(mb_param->parity, "even", 4) == 0) {
+        } else if (mb_param->parity == 'E') {
+                /* Even */
                 tios.c_cflag |= PARENB;
                 tios.c_cflag &=~ PARODD;
         } else {
-                /* odd */
+                /* Odd */
                 tios.c_cflag |= PARENB;
                 tios.c_cflag |= PARODD;
         }
@@ -1778,7 +1785,8 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
            IUCLC        Map uppercase to lowercase
            IMAXBEL      Echo BEL on input line too long
         */
-        if (strncmp(mb_param->parity, "none", 4) == 0) {
+        if (mb_param->parity == 'N') {
+                /* None */
                 tios.c_iflag &= ~INPCK;
         } else {
                 tios.c_iflag |= INPCK;

+ 3 - 3
src/modbus.h.in

@@ -227,8 +227,8 @@ typedef struct {
         uint8_t data_bit;
         /* Stop bit */
         uint8_t stop_bit;
-        /* Parity: "even", "odd", "none" */
-        char parity[5];
+        /* Parity: 'N', 'O', 'E' */
+        char parity;
         /* In error_treat with TCP, do a reconnect or just dump the error */
         uint8_t error_recovery;
         /* IP address */
@@ -251,7 +251,7 @@ typedef struct {
 } modbus_mapping_t;
 
 int modbus_init_rtu(modbus_param_t *mb_param, const char *device,
-                    int baud, const char *parity, int data_bit,
+                    int baud, char parity, int data_bit,
                     int stop_bit, int slave);
 int modbus_init_tcp(modbus_param_t *mb_param, const char *ip_address, int port);
 int modbus_set_slave(modbus_param_t *mb_param, int slave);

+ 1 - 2
tests/random-test-master.c

@@ -58,9 +58,8 @@ int main(void)
         uint16_t *tab_rp_registers;
         modbus_param_t mb_param;
 
-        /* RTU parity : none, even, odd */
         /*
-          modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1, MY_ID);
+          modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, 'N', 8, 1, MY_ID);
         */
 
         /* TCP */

+ 1 - 2
tests/unit-test-master.c

@@ -39,9 +39,8 @@ int main(void)
         struct timeval timeout_begin_old;
         struct timeval timeout_begin_new;
 
-        /* RTU parity : none, even, odd */
         /*
-        modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1,
+        modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, 'N', 8, 1,
                         CLIENT_ID);
         */