tuxmartin

libmodbus

Nov 25th, 2014
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #include <modbus.h>
  2. modbus_t *ctx;
  3. uint16_t tab_reg[64];
  4. int rc;
  5. int i;
  6. int main(void) {
  7.  
  8. ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
  9.  
  10. if (ctx == NULL) {
  11. fprintf(stderr, "Unable to create the libmodbus context\n");
  12. return -1;
  13. }
  14. rc = modbus_read_registers(ctx, 2, 3, tab_reg);
  15.  
  16. if (rc == -1) {
  17. fprintf(stderr, "%s\n", modbus_strerror(errno));
  18. return -1;
  19. }
  20.  
  21. for (i=0; i < rc; i++) {
  22. printf("reg[%d]=%d (0x%X)\n", i, tab_reg[i], tab_reg[i]);
  23. }
  24.  
  25. modbus_close(ctx);
  26. modbus_free(ctx);
  27. }
  28.  
  29.  
  30.  
  31. # apt-get install libmodbus5 libmodbus-dev
  32.  
  33. $ gcc test.c -o test
  34. test.c:1:20: fatal error: modbus.h: File not found
  35. #include <modbus.h>
  36. ^
  37. compilation terminated.
  38.  
  39.  
  40.  
  41.  
  42.  
  43. $ gcc -I /usr/include/modbus -o test test.c
  44. test.c:9:1: warning: data definition has no type or storage class [enabled by default]
  45. ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
  46. ^
  47. test.c:9:1: error: conflicting types for ‘ctx’
  48. test.c:2:11: note: previous declaration of ‘ctx’ was here
  49. modbus_t *ctx;
  50. ^
  51. test.c:9:7: warning: initialization makes integer from pointer without a cast [enabled by default]
  52. ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
  53. ^
  54. test.c:9:1: error: initializer element is not constant
  55. ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
  56. ^
  57. test.c:11:1: error: expected identifier or ‘(’ before ‘if’
  58. if (ctx == NULL) {
  59. ^
  60. test.c:17:1: warning: data definition has no type or storage class [enabled by default]
  61. rc = modbus_read_registers(ctx, 2, 3, tab_reg);
  62. ^
  63. test.c:17:1: warning: passing argument 1 of ‘modbus_read_registers’ makes pointer from integer without a cast [enabled by default]
  64. In file included from test.c:1:0:
  65. /usr/include/modbus/modbus.h:170:5: note: expected ‘struct modbus_t *’ but argument is of type ‘int’
  66. int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
  67. ^
  68. test.c:17:1: error: initializer element is not constant
  69. rc = modbus_read_registers(ctx, 2, 3, tab_reg);
  70. ^
  71. test.c:19:1: error: expected identifier or ‘(’ before ‘if’
  72. if (rc == -1) {
  73. ^
  74. test.c:24:1: error: expected identifier or ‘(’ before ‘for’
  75. for (i=0; i < rc; i++) {
  76. ^
  77. test.c:24:13: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
  78. for (i=0; i < rc; i++) {
  79. ^
  80. test.c:24:20: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘++’ token
  81. for (i=0; i < rc; i++) {
  82. ^
  83. test.c:28:1: warning: data definition has no type or storage class [enabled by default]
  84. modbus_close(ctx);
  85. ^
  86. test.c:28:1: warning: parameter names (without types) in function declaration [enabled by default]
  87. test.c:28:1: error: conflicting types for ‘modbus_close’
  88. In file included from test.c:1:0:
  89. /usr/include/modbus/modbus.h:159:6: note: previous declaration of ‘modbus_close’ was here
  90. void modbus_close(modbus_t *ctx);
  91. ^
  92. test.c:29:1: warning: data definition has no type or storage class [enabled by default]
  93. modbus_free(ctx);
  94. ^
  95. test.c:29:1: warning: parameter names (without types) in function declaration [enabled by default]
  96. test.c:29:1: error: conflicting types for ‘modbus_free’
  97. In file included from test.c:1:0:
  98. /usr/include/modbus/modbus.h:161:6: note: previous declaration of ‘modbus_free’ was here
  99. void modbus_free(modbus_t *ctx);
  100. ^
  101.  
  102.  
  103. $ lsb_release -a
  104. No LSB modules are available.
  105. Distributor ID: LinuxMint
  106. Description: Linux Mint 16 Petra
  107. Release: 16
  108. Codename: petra
  109. $
Advertisement
Add Comment
Please, Sign In to add comment