Modbus86 Master

Modbus86 Master 能使 86Duino 模擬成 Modbus Master ,對通道上的 Slave 節點傳送封包。

範例


啟用 RS485 並使用 Modbus RTU 作為通訊方式的 Modbus Master 範例

#include <Modbus86.h>

ModbusMaster bus;
ModbusMasterNode node1;
ModbusMasterNode node2;

int led = 0;
uint32_t value = 0;

void setup()
{
    Serial485.begin(115200);
    
    /* Modbus RTU Mode via RS485. */
    bus.begin(MODBUS_RTU, Serial485);
    
    /* Slave node 1 with ID 11. */
    node1.begin(11, bus);
    /* Slave node 2 with ID 12. */
    node2.begin(12, bus);
}

void loop()
{
    uint16_t reg[2];
    
    /* Write 1 coil to address 0 of the slave with ID 11. */
    node1.writeSingleCoil(0, led);
    
    /* Write 2 word to holding registers address 16 of
	   the slave with ID 11. */
    reg[0] = value & 0xFFFF;
    reg[1] = (value >> 16) & 0xFFFF;
    node1.setTransmitBuffer(0, reg[0]);
    node1.setTransmitBuffer(1, reg[1]);
    node1.writeMultipleRegisters(16, 2);

    /* Read 2 word from holding registers address 16 of
	   the slave with ID 11. */
    node1.readHoldingRegisters(16, 2);
    Serial.print("From Node 1 Holding Register: ");
    value = node1.getResponseBuffer(0)
		| (node1.getResponseBuffer(1) << 16);
    Serial.println(value);

    /* Read 1 word from input registers address 2 of
	   the slave with ID 11. */
    node1.readInputRegisters(2, 1);
    Serial.print("              Input Register: ");
    Serial.print(node1.getResponseBuffer(0));
    Serial.println();
    
    /* Write 1 coil to address 0 of the slave with ID 12. */
    node2.writeSingleCoil(0, led);
    
    /* Write 2 word to holding registers address 16 of
	   the slave with ID 12. */
    reg[0] = value & 0xFFFF;
    reg[1] = (value >> 16) & 0xFFFF;
    node2.setTransmitBuffer(0, reg[0]);
    node2.setTransmitBuffer(1, reg[1]);
    node2.writeMultipleRegisters(16, 2);

    /* Read 2 word from holding registers address 16
	   of the slave with ID 12. */
    node2.readHoldingRegisters(16, 2);
    Serial.print("From Node 2 Holding Register: ");
    value = node2.getResponseBuffer(0)
		| (node2.getResponseBuffer(1) << 16);
    Serial.println(value);

    /* Read 1 word from input registers address 2 of
	   the slave with ID 12. */
    node2.readInputRegisters(2, 1);
    Serial.print("              Input Register: ");
    Serial.print(node2.getResponseBuffer(0));
    Serial.println();
    
    led = !led;
    value++;
    
    delay(1000);
}


啟用 Ethernet 並使用 Modbus TCP 作為通訊方式的 Modbus Master 範例

#include <Ethernet.h>
#include <Modbus86.h>

ModbusMaster bus;
ModbusMasterNode node;

byte mac[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
IPAddress localIp(192,168,1,101);
IPAddress serverIp(192,168,1,102);

int led = 0;
uint32_t value = 0;

void setup()
{
    Ethernet.begin(mac, localIp);
    
    /* Modbus TCP Mode via Ethernet. */
    bus.begin(MODBUS_TCP, serverIp);
    
    /* Slave node initialize. */
    node.begin(11, bus);
}

void loop()
{
    uint16_t reg[2];
    
    /* Write 1 coil to address 0 of the slave with ID 11. */
    node.writeSingleCoil(0, led);
    
    /* Write 2 word to holding registers address 16 of
	   the slave with ID 11. */
    reg[0] = value & 0xFFFF;
    reg[1] = (value >> 16) & 0xFFFF;
    node.setTransmitBuffer(0, reg[0]);
    node.setTransmitBuffer(1, reg[1]);
    node.writeMultipleRegisters(16, 2);

    /* Read 2 word from holding registers address 16 of
	   the slave with ID 11. */
    node.readHoldingRegisters(16, 2);
    Serial.print("From Node 1 Holding Register: ");
    value = node.getResponseBuffer(0)
		| (node.getResponseBuffer(1) << 16);
    Serial.println(value);

    /* Read 1 word from input registers address 2 of
	   the slave with ID 11. */
    node.readInputRegisters(2, 1);
    Serial.print("              Input Register: ");
    Serial.print(node.getResponseBuffer(0));
    Serial.println();
    
    led = !led;
    value++;
    
    delay(1000);
}

函式庫參考主頁面

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.