Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
modbus [2011/11/06 22:01]
jap
modbus [2011/11/07 16:40]
jap
Line 41: Line 41:
 Source code of mbtest.c: Source code of mbtest.c:
  
-  ​#include <​stdio.h>​ +<​file>​ 
-  #include <​modbus.h>​ +#include <​stdio.h>​ 
-   +#include <​modbus.h>​ 
-  #define MB_BITRATE 19200 + 
-  #define MB_DATABITS 8 +#define MB_BITRATE 19200 
-  #define MB_STOPBITS 1 +#define MB_DATABITS 8 
-  #define MB_PARITY '​N'​ +#define MB_STOPBITS 1 
-  #define MB_SLAVE_ADDRESS 1 +#define MB_PARITY '​N'​ 
-   +#define MB_SLAVE_ADDRESS 1 
-  #define MB_REG_BASE ​0 + 
-  #define MB_REG_COUNT ​16 +#define MB_REG_BASE ​6 
-   +#define MB_REG_COUNT ​4 
-  int main ( int argc, char* argv[] )  + 
-  +int main ( int argc, char* argv[] ) 
-    modbus_t *ctx; +
-    uint16_t tab_reg[32];​ +  modbus_t *ctx; 
-    int n, i; +  uint16_t tab_reg[32];​ 
-   +  int n, i; 
-    if ( argc != 2 )+  ​int errno = 0; 
 + 
 +  ​if ( argc != 2 )
     {     {
       fprintf(stderr,​ "​Syntax:​ %s serial_device\n",​ argv[0]);       fprintf(stderr,​ "​Syntax:​ %s serial_device\n",​ argv[0]);
       return -1;       return -1;
     }     }
-  ​ + 
-    ctx = modbus_new_rtu(argv[1],​+  ctx = modbus_new_rtu(argv[1],​
         ​MB_BITRATE,​         ​MB_BITRATE,​
         ​MB_PARITY,​         ​MB_PARITY,​
         ​MB_DATABITS,​         ​MB_DATABITS,​
         ​MB_STOPBITS);​         ​MB_STOPBITS);​
-    ​if (ctx == NULL)  +  ​if (ctx == NULL) {
-    ​{+
     fprintf(stderr, ​     fprintf(stderr, ​
      "​Unable to create the libmodbus context on serial port %s\n",      "​Unable to create the libmodbus context on serial port %s\n",
      argv[1]);      argv[1]);
-    return -1 +    return -1; 
-    +  
-   + 
-    if ( modbus_set_slave(ctx,​ MB_SLAVE_ADDRESS) )+  if ( modbus_set_slave(ctx,​ MB_SLAVE_ADDRESS) )
     {     {
       fprintf(stderr,​       fprintf(stderr,​
        "​Failed to set modbus slave address\n"​);​        "​Failed to set modbus slave address\n"​);​
 +      errno = -1;
       goto fail;       goto fail;
-    } +    }   
-   +
-    if ( modbus_rtu_set_serial_mode(ctx,​ MODBUS_RTU_RS485) ) +
-    { +
-      fprintf(stderr,​ +
-       "​Failed to set modbus mode\n"​);​ +
-      goto fail; +
-    }+
   ​   ​
-    ​if ( modbus_connect(ctx))+  ​if ( modbus_connect(ctx))
     {     {
       fprintf(stderr,​       fprintf(stderr,​
        "​Unable to connect to modbus server"​);​        "​Unable to connect to modbus server"​);​
 +      errno = -1;
       goto fail;       goto fail;
     }     }
-  ​ + 
-    n = modbus_read_registers(ctx,​ MB_REG_BASE,​ MB_REG_COUNT,​ tab_reg); +  n = modbus_read_registers(ctx, ​ 
-    if ( n <= 0 )+     ​MB_REG_BASE, ​ 
 +     ​MB_REG_COUNT, ​ 
 +     ​tab_reg+MB_REG_BASE); 
 +  if ( n <= 0 )
     {     {
       fprintf(stderr,​       fprintf(stderr,​
        "​Unable to read modbus registers\n"​);​        "​Unable to read modbus registers\n"​);​
 +      errno = -1;
       goto fail;       goto fail;
     }     }
   ​   ​
-    ​for ( i=MB_REG_BASE;​ i<​MB_REG_BASE+n;​ i++ )+  ​for ( i=MB_REG_BASE;​ i<​MB_REG_BASE+n;​ i++ )
     {     {
       printf ( "[%d]: %d\n", i, tab_reg[i]);​       printf ( "[%d]: %d\n", i, tab_reg[i]);​
     }     }
-  ​ + 
-  fail: + ​fail:​ 
-    modbus_close(ctx);​ +  modbus_close(ctx);​ 
-    modbus_free(ctx);​ +  modbus_free(ctx);​ 
-   + 
-  return ​0+  return ​errno
-  }+} 
 +</​file>​
  
modbus.txt · Last modified: 2011/11/07 16:41 by jap
Recent changes RSS feed CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki