====== 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 ^ | [[:moacon:rtu_readcoils:|RTU_readCoils]] | 1, 2 | | [[:moacon:rtu_readregs:|RTU_readRegs]] | 3 | | [[:moacon:rtu_readinregs:|RTU_readInRegs]] | 4 | | [[:moacon:rtu_writecoil:|RTU_writeCoil]] | 5 | | [[:moacon:rtu_writereg:|RTU_writeReg]] | 6 | | [[:moacon:rtu_writecoils:|RTU_writeCoils]] | 15 | Use [[:moacon:opencom:|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 [[:moacon:getcrc:|getCrc]] function can be used to compute a Modbus RTU CRC if the needed. [[moacon:index|MOACON - Modular Programmable Automation Controller (PAC)]]