Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <modbus.h>
  4. #include <errno.h>
  5. int main()
  6. {
  7. struct timeval old_response_timeout;
  8. struct timeval response_timeout;
  9. modbus_t *ctx = NULL;
  10. int rc = 0;
  11. uint16_t tab_reg[64];
  12. int i = 0;
  13. int slave = 0;
  14. int connected = 0;
  15. int serial = 0;
  16. ctx = modbus_new_rtu("/dev/ttyS1", 9600, 'E', 8, 1);
  17. if (ctx == NULL) {
  18. fprintf(stderr, "Unable to create the libmodbus contextn");
  19. return -1;
  20. }
  21. modbus_set_debug(ctx, 1);
  22. modbus_get_response_timeout(ctx, &old_response_timeout);
  23. response_timeout.tv_sec = 10;
  24. response_timeout.tv_usec = 0;
  25. modbus_set_response_timeout(ctx, &response_timeout);
  26. modbus_set_byte_timeout(ctx, &response_timeout);
  27.  
  28. slave = modbus_set_slave(ctx,247);
  29.  
  30. if(slave == -1)
  31. printf("Didn't connect to slave/n");
  32.  
  33. connected = modbus_connect(ctx);
  34.  
  35. if(connected == -1)
  36. printf("Connection failedn");
  37. if(connected == 0)
  38. printf("connectedn");
  39. //serial = modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485);
  40. //if(serial == -1)
  41. // printf("Didn't set serial mode/n:%sn",modbus_strerror(errno));
  42. rc = modbus_read_registers(ctx,0x27,2,tab_reg);
  43.  
  44. if (rc == -1) {
  45. fprintf(stderr, "%sn", modbus_strerror(errno));
  46. return -1;
  47. }
  48.  
  49. for(i=0;i<rc;i++)
  50. printf("degrees %dn", tab_reg[i]);
  51.  
  52. return 0;
  53. }
  54.  
  55. rc = modbus_read_registers(ctx,0x27,2,tab_reg);
  56.  
  57. if (rc == -1) {
  58. fprintf(stderr, "%sn", modbus_strerror(errno));
  59. return -1;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement