User Tools

Site Tools

한국어

moacon:modbus_master:index

Modbus RTU Master

The MOACON can also function as a Modbus RTU Master to query a Modbus RTU slave. The following functions are provided and will function at any baud rate above 9600.

Function Name Modus Function Code
RTU_readCoils 1, 2
RTU_readRegs 3
RTU_readInRegs 4
RTU_writeCoil 5
RTU_writeReg 6
RTU_writeCoils 15

Use openCom to open a serial port before calling the functions.

#include "moacon500.h"
void cmain(void)
{
    int i=0,kk;
    u8 res;
 
    static u8 MBcoilBuffer[100];
    static u16 MBregisterBuffer[100];
 
    clcdI2cInit(0);
    clcdPower(1);
    delay(100);
    clcdCsr(0);
 
    openCom(0,57600,C8N1);                          // Open the serial port
    while(1) {
        delay(100);
        res = RTU_writeCoils(0,1,0,0x12);           // Write 1 bit to the Modbus RTU slave
        delay(100);
        res = RTU_readCoils(0,1,MBcoilBuffer,4,12); // Read 12 bits from the Modbus RTU slave
    }
}

The getCrc function can be used to compute a Modbus RTU CRC if the needed.

MOACON - Modular Programmable Automation Controller (PAC)

moacon/modbus_master/index.txt · Last modified: 2022/03/10 00:22 by COMFILE Technology