Advertisement
mikroavr

read_modbus

Dec 12th, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ModbusMaster.h>
  2. ModbusMaster node;
  3.  
  4. #define MAX485_RXD 16
  5. #define MAX485_TXD 17
  6. const byte addr_pm = 1;
  7.  
  8. unsigned long cur_time, old_time;
  9. float van, vbn, vcn;
  10.  
  11. void setup() {
  12.   // put your setup code here, to run once:
  13.   Serial.begin(115200);
  14.   Serial1.begin(9600, SERIAL_8N1, MAX485_RXD, MAX485_TXD);
  15.   node.begin(addr_pm, Serial1);
  16. }
  17.  
  18. void loop() {
  19.   // put your main code here, to run repeatedly:
  20.   cur_time = millis();
  21.   if (cur_time - old_time >= 1000) {
  22.     uint8_t result;
  23.     uint32_t tempdouble;
  24.  
  25.     result = node.readHoldingRegisters(3027, 6);
  26.     if (result == node.ku8MBSuccess)
  27.     {
  28.       tempdouble = node.getResponseBuffer(1);
  29.       tempdouble |= node.getResponseBuffer(0) << 16;
  30.       van = *(float*)&tempdouble;
  31.  
  32.       tempdouble = node.getResponseBuffer(3);
  33.       tempdouble |= node.getResponseBuffer(2) << 16;
  34.       vbn = *(float*)&tempdouble;
  35.  
  36.       tempdouble = node.getResponseBuffer(5);
  37.       tempdouble |= node.getResponseBuffer(4) << 16;
  38.       vcn = *(float*)&tempdouble;
  39.  
  40.       Serial.print("van: "); Serial.print(van); Serial.print(", ");
  41.       Serial.print("vbn: "); Serial.print(vbn); Serial.print(", ");
  42.       Serial.print("vcn: "); Serial.print(vcn);
  43.       Serial.println();
  44.     } else {
  45.       Serial.println("failed read");
  46.     }
  47.  
  48.     old_time = millis();
  49.   }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement