Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ModbusMaster.h>
- #define MAX485_DE 3
- #define MAX485_RE_NEG 2
- // 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);
- // Modbus communication runs at 115200 baud
- Serial.begin(115200);
- // Modbus slave ID 1
- node.begin(1, Serial);
- // Callbacks allow us to configure the RS485 transceiver correctly
- node.preTransmission(preTransmission);
- node.postTransmission(postTransmission);
- }
- bool state = true;
- void loop()
- {
- uint8_t result;
- uint16_t data[6];
- // Toggle the coil at address 0x0002 (Manual Load Control)
- result = node.writeSingleCoil(0x0002, state);
- state = !state;
- // Read 16 registers starting at 0x3100)
- result = node.readInputRegisters(0x3100, 16);
- if (result == node.ku8MBSuccess)
- {
- Serial.print("Vbatt: ");
- Serial.println(node.getResponseBuffer(0x04)/100.0f);
- Serial.print("Vload: ");
- Serial.println(node.getResponseBuffer(0xC0)/100.0f);
- Serial.print("Pload: ");
- Serial.println((node.getResponseBuffer(0x0D) +
- node.getResponseBuffer(0x0E) << 16)/100.0f);
- }
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement