Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ModbusMaster.h>
- //#include <Convert.h>
- //Creation of class object
- //Convert convert;
- #define RXD2 16
- #define TXD2 17
- //#define RXD1 4
- //#define TXD1 2
- #define Fk 1
- #define maxSerial Serial1
- #define MAX485_DE 14
- #define MAX485_RE_NEG 14
- //int c;
- //float cc;
- // instantiate ModbusMaster object
- ModbusMaster node;
- void preTransmission()
- {
- digitalWrite(MAX485_RE_NEG, 1);
- digitalWrite(MAX485_DE, 1);
- }
- void postTransmission()
- {
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- }
- void setup()
- {
- pinMode(MAX485_RE_NEG, OUTPUT);
- pinMode(MAX485_DE, OUTPUT);
- // Init in receive mode
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- maxSerial.begin(9600, SERIAL_8N1, RXD2, TXD2);
- // Modbus communication runs at 115200 baud
- Serial.begin(9600);
- node.begin(1, maxSerial);
- node.preTransmission(preTransmission);
- node.postTransmission(postTransmission);
- }
- bool state = true;
- void loop()
- {
- uint8_t result;
- uint16_t data[6];
- result = node.readHoldingRegisters(0, 9);
- if (result == node.ku8MBSuccess)
- {
- Serial.println(node.getResponseBuffer(0));
- Serial.println(node.getResponseBuffer(1));
- Serial.println(node.getResponseBuffer(2));
- Serial.println(node.getResponseBuffer(3));
- Serial.println(node.getResponseBuffer(4));
- Serial.println(node.getResponseBuffer(5));
- Serial.println(node.getResponseBuffer(6));
- Serial.println("--------------------------------");
- }
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement