Advertisement
mikroavr

URM14_RS485

Feb 2nd, 2022 (edited)
1,945
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ModbusMaster.h>
  2. //#include <Convert.h>
  3.  
  4. //Creation of class object
  5. //Convert convert;
  6. #define RXD2 16
  7. #define TXD2 17
  8. //#define RXD1 4
  9. //#define TXD1 2
  10. #define Fk 1
  11. #define maxSerial Serial1
  12.  
  13. #define MAX485_DE      14
  14. #define MAX485_RE_NEG  14
  15. //int c;
  16. //float cc;
  17. // instantiate ModbusMaster object
  18. ModbusMaster node;
  19.  
  20. void preTransmission()
  21. {
  22.   digitalWrite(MAX485_RE_NEG, 1);
  23.   digitalWrite(MAX485_DE, 1);
  24. }
  25.  
  26. void postTransmission()
  27. {
  28.   digitalWrite(MAX485_RE_NEG, 0);
  29.   digitalWrite(MAX485_DE, 0);
  30. }
  31.  
  32. void setup()
  33. {
  34.   pinMode(MAX485_RE_NEG, OUTPUT);
  35.   pinMode(MAX485_DE, OUTPUT);
  36.   // Init in receive mode
  37.   digitalWrite(MAX485_RE_NEG, 0);
  38.   digitalWrite(MAX485_DE, 0);
  39.   maxSerial.begin(9600, SERIAL_8N1, RXD2, TXD2);
  40.   // Modbus communication runs at 115200 baud
  41.   Serial.begin(9600);
  42.   node.begin(1, maxSerial);
  43.   node.preTransmission(preTransmission);
  44.   node.postTransmission(postTransmission);
  45. }
  46.  
  47. bool state = true;
  48.  
  49. void loop()
  50. {
  51.   uint8_t result;
  52.   uint16_t data[6];
  53.   result = node.readHoldingRegisters(0, 9);
  54.   if (result == node.ku8MBSuccess)
  55.   {
  56.     Serial.println(node.getResponseBuffer(0));
  57.     Serial.println(node.getResponseBuffer(1));
  58.     Serial.println(node.getResponseBuffer(2));
  59.     Serial.println(node.getResponseBuffer(3));
  60.     Serial.println(node.getResponseBuffer(4));
  61.     Serial.println(node.getResponseBuffer(5));
  62.     Serial.println(node.getResponseBuffer(6));
  63.     Serial.println("--------------------------------");
  64.   }
  65.   delay(1000);
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement