RuiViana

ModBus_Master_Teste

Feb 11th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #include <SimpleModbusMaster.h>
  2. int flag;
  3. #define baud 9600
  4. #define timeout 1000
  5. #define polling 200 // the scan rate
  6. #define retry_count 10
  7.  
  8. #define TxEnablePin 2
  9.  
  10. #define LED 9
  11. #define LUZ 13
  12.  
  13. #define TOTAL_NO_OF_REGISTERS 3
  14.  
  15. enum
  16. {
  17. PACKET1,
  18. PACKET2,
  19. PACKET3,
  20.  
  21. TOTAL_NO_OF_PACKETS // leave this last entry
  22. };
  23.  
  24. Packet packets[TOTAL_NO_OF_PACKETS];
  25.  
  26. unsigned int regs[TOTAL_NO_OF_REGISTERS];
  27.  
  28. void setup()
  29. { // 1 int Addr 0001
  30. Serial.begin(9600); // 1 int data 0001
  31. // 2 start addr Reg[2]
  32. // modbus_construct(&packets[PACKET1], 2, READ_HOLDING_REGISTERS, 0, 1, 0);
  33. // modbus_construct(&packets[PACKET2], 2, READ_HOLDING_REGISTERS, 1, 1, 2);
  34. // modbus_construct(&packets[PACKET3], 1, READ_HOLDING_REGISTERS, 0, 1, 0);
  35.  
  36. modbus_construct(&packets[PACKET1], 2, READ_HOLDING_REGISTERS, 0, 1, 0);
  37. modbus_construct(&packets[PACKET2], 2, READ_HOLDING_REGISTERS, 1, 1, 2);
  38. modbus_construct(&packets[PACKET3], 2, READ_HOLDING_REGISTERS, 2, 1, 4);
  39.  
  40. modbus_configure(&Serial1, baud, SERIAL_8N2, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
  41. pinMode(LED, OUTPUT);
  42. pinMode(LUZ, OUTPUT);
  43. }
  44. void loop()
  45. {
  46. modbus_update();
  47. unsigned long temp = (unsigned long)regs[0] << 16 | regs[1];
  48. Serial.println(" ");
  49. Serial.println("Regs");
  50. Serial.println(regs[0]);
  51. Serial.println(regs[2]);
  52. Serial.println(regs[4]);
  53. }
Add Comment
Please, Sign In to add comment