Advertisement
mikroavr

Untitled

Mar 21st, 2021
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ModbusMaster.h>
  2. ModbusMaster node;
  3. static uint8_t pzemSlaveAddr = 3;
  4. #define RXD1 32
  5. #define TXD1 25
  6. unsigned cur_time, old_time;
  7.  
  8. #define MAX485_RE_NEG 33
  9. #define MAX485_DE 33
  10.  
  11. void preTransmission()
  12. {
  13.   digitalWrite(MAX485_RE_NEG, 1);
  14.   digitalWrite(MAX485_DE, 1);
  15. }
  16.  
  17. void postTransmission()
  18. {
  19.   digitalWrite(MAX485_RE_NEG, 0);
  20.   digitalWrite(MAX485_DE, 0);
  21. }
  22.  
  23.  
  24. void setup() {
  25.   // put your setup code here, to run once:
  26.   digitalWrite(MAX485_RE_NEG, 0);
  27.   digitalWrite(MAX485_DE, 0);
  28.   Serial.begin(115200);
  29.   Serial1.begin(9600, SERIAL_8N1, RXD1, TXD1);
  30.   node.begin(pzemSlaveAddr, Serial1);
  31.   node.preTransmission(preTransmission);
  32.   node.postTransmission(postTransmission);
  33.  
  34. }
  35.  
  36. void loop() {
  37.   // put your main code here, to run repeatedly:
  38.   cur_time = millis();
  39.   if (cur_time - old_time >= 3000) {
  40.     uint8_t result;
  41.     uint32_t tempdouble;
  42.     result = node.readHoldingRegisters(171, 2);
  43.     if (result == node.ku8MBSuccess)
  44.     {
  45.       Serial.println("success read modbus1");
  46.     }
  47.     else {
  48.       Serial.println("failed read modbus1");
  49.     }
  50.   }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement