Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <modbus.h>
- modbus_t *ctx;
- uint16_t tab_reg[64];
- int rc;
- int i;
- int main(void) {
- ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
- if (ctx == NULL) {
- fprintf(stderr, "Unable to create the libmodbus context\n");
- return -1;
- }
- rc = modbus_read_registers(ctx, 2, 3, tab_reg);
- if (rc == -1) {
- fprintf(stderr, "%s\n", modbus_strerror(errno));
- return -1;
- }
- for (i=0; i < rc; i++) {
- printf("reg[%d]=%d (0x%X)\n", i, tab_reg[i], tab_reg[i]);
- }
- modbus_close(ctx);
- modbus_free(ctx);
- }
- # apt-get install libmodbus5 libmodbus-dev
- $ gcc test.c -o test
- test.c:1:20: fatal error: modbus.h: File not found
- #include <modbus.h>
- ^
- compilation terminated.
- $ gcc -I /usr/include/modbus -o test test.c
- test.c:9:1: warning: data definition has no type or storage class [enabled by default]
- ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
- ^
- test.c:9:1: error: conflicting types for ‘ctx’
- test.c:2:11: note: previous declaration of ‘ctx’ was here
- modbus_t *ctx;
- ^
- test.c:9:7: warning: initialization makes integer from pointer without a cast [enabled by default]
- ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
- ^
- test.c:9:1: error: initializer element is not constant
- ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
- ^
- test.c:11:1: error: expected identifier or ‘(’ before ‘if’
- if (ctx == NULL) {
- ^
- test.c:17:1: warning: data definition has no type or storage class [enabled by default]
- rc = modbus_read_registers(ctx, 2, 3, tab_reg);
- ^
- test.c:17:1: warning: passing argument 1 of ‘modbus_read_registers’ makes pointer from integer without a cast [enabled by default]
- In file included from test.c:1:0:
- /usr/include/modbus/modbus.h:170:5: note: expected ‘struct modbus_t *’ but argument is of type ‘int’
- int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
- ^
- test.c:17:1: error: initializer element is not constant
- rc = modbus_read_registers(ctx, 2, 3, tab_reg);
- ^
- test.c:19:1: error: expected identifier or ‘(’ before ‘if’
- if (rc == -1) {
- ^
- test.c:24:1: error: expected identifier or ‘(’ before ‘for’
- for (i=0; i < rc; i++) {
- ^
- test.c:24:13: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
- for (i=0; i < rc; i++) {
- ^
- test.c:24:20: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘++’ token
- for (i=0; i < rc; i++) {
- ^
- test.c:28:1: warning: data definition has no type or storage class [enabled by default]
- modbus_close(ctx);
- ^
- test.c:28:1: warning: parameter names (without types) in function declaration [enabled by default]
- test.c:28:1: error: conflicting types for ‘modbus_close’
- In file included from test.c:1:0:
- /usr/include/modbus/modbus.h:159:6: note: previous declaration of ‘modbus_close’ was here
- void modbus_close(modbus_t *ctx);
- ^
- test.c:29:1: warning: data definition has no type or storage class [enabled by default]
- modbus_free(ctx);
- ^
- test.c:29:1: warning: parameter names (without types) in function declaration [enabled by default]
- test.c:29:1: error: conflicting types for ‘modbus_free’
- In file included from test.c:1:0:
- /usr/include/modbus/modbus.h:161:6: note: previous declaration of ‘modbus_free’ was here
- void modbus_free(modbus_t *ctx);
- ^
- $ lsb_release -a
- No LSB modules are available.
- Distributor ID: LinuxMint
- Description: Linux Mint 16 Petra
- Release: 16
- Codename: petra
- $
Advertisement
Add Comment
Please, Sign In to add comment