Professional Documents
Culture Documents
lcd.begin();
lcd.clear();
lcd.setCursor(1, 1);
lcd.print("hello Industruino!");
lcd.setCursor(1, 3);
lcd.print("Modbus RTU Master");
analogWrite(26, 100); // LCD backlight
Indio.digitalMode(1, OUTPUT);
Indio.digitalMode(2, OUTPUT);
Indio.digitalMode(3, OUTPUT);
Indio.digitalMode(4, OUTPUT);
// Initialize each packet: packet, slave-id, function, start of slave index, number of regs, start of
master index
// set 4 registers
// read 4 registers
modbus_construct(&packets[PACKET1], SlaveID, PRESET_MULTIPLE_REGISTERS, 0, 4,
0);
modbus_construct(&packets[PACKET2], SlaveID, READ_HOLDING_REGISTERS, 4, 4, 4);
// Initialize the Modbus Finite State Machine
modbus_configure(&Serial1, baud, SERIAL_8N2, timeout, polling, retry_count, TxEnablePin,
packets, TOTAL_NO_OF_PACKETS, regs);
// Serial1 = INDUSTRUINO RS485
}
void loop()
{
// unsigned long moment = millis();
modbus_update(); // send Master request to Slave, as defined above
// Serial.println(millis()-moment);
// frequency is limited by polling parameter
// the library manual suggests not using long delays but an interval instead
// the below section updates a counter after an interval,
// and switches ON one LED of 8: 4 on the Master and 4 on the Slave
// first switch all OFF
// then if counter points to first half, switch ON one on Master
// if counter points to second half, switch ON one on Slave
lcd.setCursor(1, 6);
if (regs[6]) lcd.print("Switch 7: ON "); // print status of switch on Slave
else lcd.print("Switch 7: OFF");
lcd.setCursor(1, 7);
if (regs[7]) lcd.print("Switch 8: ON "); // print status of switch on Slave
else lcd.print("Switch 8: OFF");
}
/* Note:
The use of the enum instruction is not needed. You could set a maximum allowable
size for holdinRegs[] by defining HOLDING_REGS_SIZE using a constant and then access
holdingRegs[] by "Index" addressing.
I.e.
holdingRegs[0] = analogRead(A0);
analogWrite(LED, holdingRegs[1]/4);
*/
}