mikroavr

npk_modbus

Feb 25th, 2021
549
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(20, 3);// address start ( list start from 1), get six 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.     tempdouble = node.getResponseBuffer(2);
  39.     Serial.println(tempdouble);
  40.   }
  41.   else {
  42.     Serial.println("failed1");
  43.   }
  44.   Serial.println();
  45.   Serial.println("-----------------------------------------");
  46. }
RAW Paste Data