Creating a CUWIN Modbus RTU Master with NModbus
NModbus is a free and open source implementation of the Modbus protocol for the .Net Framework and .Net Compact Framework. It is currently hosted at https://code.google.com/p/nmodbus/ but there are also other forks on Github. This blog post will describe how to get started using NModbus with the CUWIN.
After downloading and extracting the NModbus binaries from the repository, there will be two folders inside the "bin" folder: "net" and "netcf". "net" is for the .Net Framework, and "netcf" is for the .Net Compact Framework. The CUWIN is a .Net Compact Framework device, so to use NModbus in a CUWIN project, simply add a reference to the "Modbus.dll" file in the "netcf" folder.
Add a using declaration to the source file to contain the Modbus Master code.
using Modbus.Device;
Initialize a new ModbusSerialMaster:
// Open COM1 with a baud rate of 115200
_serialPort = new SerialPort("COM1", 115200);
_serialPort.Open();
// Create a new Modbus RTU Master using _serialPort as the communication channel
_modbusMaster = ModbusSerialMaster.CreateRtu(_serialPort);
_modbusMaster.Transport.ReadTimeout = 500;
_modbusMaster.Transport.WriteTimeout = 500;
_modbusMaster.Transport.Retries = 0;
Then call any of the given methods to query a Modbus slave:
bool[] ReadCoils(byte slaveAddress, ushort startAddress, ushort numberOfPoints);
ushort[] ReadHoldingRegisters(byte slaveAddress, ushort startAddress,
ushort numberOfPoints);
ushort[] ReadInputRegisters(byte slaveAddress, ushort startAddress,
ushort numberOfPoints);
bool[] ReadInputs(byte slaveAddress, ushort startAddress, ushort numberOfPoints);
ushort[] ReadWriteMultipleRegisters(byte slaveAddress, ushort startReadAddress,
ushort numberOfPointsToRead, ushort startWriteAddress, ushort[] writeData);
void WriteMultipleCoils(byte slaveAddress, ushort startAddress, bool[] data);
void WriteMultipleRegisters(byte slaveAddress, ushort startAddress, ushort[] data);
void WriteSingleCoil(byte slaveAddress, ushort coilAddress, bool value);
void WriteSingleRegister(byte slaveAddress, ushort registerAddress, ushort value);
The following sample project demonstrates creating a Modbus Master using the CUWIN CWV that monitors and controls a Modbus slave using a CUBLOC CB280 and the CUBLOC Study Board 2. The CB280's digital output on pin P0 is connected to the Study Board's LED0 on the, and an analog input is connected to the Study Board's knob potentiometer, AD1.
The Analog Input "Read" button will query the CB280 for the value read from its analog input and display it on the form. Turning the knob pot AD1 on the Study Board will change the value read from the analog input.
The Digital Output "Read" button will query the CB280 for the state of the CB280's digital output on pin P0. The Digital Output "On" and "Off" buttons will turn P0 on or off respectively thus turning the Study Board's LED0 on or off.
Recent Posts
-
We are preparing a new industrial modular Arduino.
This product provides hardware to create industrial applications using the Arduino development envir …Feb 29th 2024 -
Announcing the CPi-S070WR Industrial Panel PC built around the Raspberry Pi Compute Module 4S
We are pleased to announce the next generation of our very popular Raspberry Pi Panel PCs: The Co …Jun 15th 2023 -
Announcing the CPi-C070WR4C Industrial Panel PC built around the Raspberry Pi Compute Module 4
We are pleased to announce the next generation of our very popular Raspberry Pi Panel PCs: The C …Jan 21st 2023