Advertisement
Guest User

Modbus arduino

a guest
Feb 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1.  
  2.  
  3. #include <ModbusMaster.h>
  4.  
  5.  
  6. // instantiate ModbusMaster object
  7. ModbusMaster node;
  8.  
  9.  
  10. void setup()
  11. {
  12. // use Serial (port 0); initialize Modbus communication baud rate
  13. Serial.begin(9600);
  14.  
  15. // communicate with Modbus slave ID 2 over Serial (port 0)
  16. node.begin(2, Serial);
  17. }
  18.  
  19.  
  20. void loop()
  21. {
  22. static uint32_t i;
  23. uint8_t j, result;
  24. uint16_t data[6];
  25.  
  26. i++;
  27.  
  28. // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
  29. node.setTransmitBuffer(0, lowWord(i));
  30.  
  31. // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
  32. node.setTransmitBuffer(1, highWord(i));
  33.  
  34. // slave: write TX buffer to (2) 16-bit registers starting at register 0
  35. result = node.writeMultipleRegisters(0, 2);
  36.  
  37. // slave: read (6) 16-bit registers starting at register 2 to RX buffer
  38. result = node.readInputRegisters(1000, 2);
  39.  
  40. // do something with data if read is successful
  41. if (result == node.ku8MBSuccess)
  42. {
  43. for (j = 0; j < 6; j++)
  44. {
  45. data[j] = node.getResponseBuffer(j);
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement