mikroavr

modbus_4-20mA

Feb 16th, 2021 (edited)
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <ModbusMaster.h>
  3. ModbusMaster node;
  4. static uint8_t SlaveAddr = 1;
  5.  
  6. #define RXD1 33
  7. #define TXD1 25
  8.  
  9. unsigned cur_time, old_time;
  10.  
  11. void setup() {
  12.   // put your setup code here, to run once:
  13.   Serial.begin(115200);
  14.   Serial1.begin(9600, SERIAL_8N1, RXD1, TXD1);
  15.   node.begin(SlaveAddr, 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.     baca_rs485(15);
  23.     old_time = cur_time;
  24.   }
  25. }
  26.  
  27. void baca_rs485(uint8_t wkt) {
  28.   uint8_t result;
  29.   uint32_t tempdouble;
  30.   result = node.readHoldingRegisters(171, 2);// address start ( list start from 172), get two register
  31.   if (result == node.ku8MBSuccess)
  32.   {
  33.     Serial.println("succes1");
  34.     tempdouble = node.getResponseBuffer(0);
  35.     Serial.println(tempdouble);
  36.     tempdouble = node.getResponseBuffer(1);
  37.     Serial.println(tempdouble);
  38.   }
  39.   else {
  40.     Serial.println("failed1");
  41.   }
  42.   Serial.println();
  43.   Serial.println("-----------------------------------------");
  44. }
Add Comment
Please, Sign In to add comment