Loading... Please wait...


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);

// Create a new Modbus RTU Master using 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.

Download Source Code

MSB LOGIC released

The MSB series is a CUBLOC-based industrial controller.Originally, the MSB series could only be programmed with CUBLOC Studio, which supports both BASIC and Ladder Logic. However, some users are not familiar with BASIC and prefer to use Ladder Logic exclusively. Therefore, we have created MSB LOGIC, a Ladder Logic development environment designed specifically [...]

Read More »

PID Controller for the CUWIN

This sample program illustrates an implementation of a PID controller for the CUWIN 5000/6000 series. The attached Visual Studio solution is a .Net Compact Framework 3.5 solution, but if you want to use it on the CUWIN 3000/4000 series, just copy the files into a .Net Compact Framework 2.0 solution. No modification should be needed. There [...]

Read More »

Moacon Modbus TCP Slave (Server)

This is an implementation of the a Modbus TCP slave (server) for the Moacon. It supports function codes 1, 2, 3, 4, 5, 6, 15, & 16; basically reading and writing bits and words. Attached to this post is the entire source code (fn_modbusTcp.c) and a sample illustrating it's use (ModbusTcpTest.c). We [...]

Read More »


Below are a few PID demo's for the CUBLOC. Enjoy!CB2xx PID Fan with Tachometer Feedback CB2xx PID Heater CB2xx PID Light CB2xx PID Voltage

Read More »

Turn CUWIN LCD Backlight On/Off with Code

This program illustrates how to turn the CUWIN's LCD Backlight on and off with code. It works by calling the Windows API function SetDevicePower. Calling a Windows API function is done using a technique called "Platform Invoke" or "P/Invoke" for short. CUWIN 5000/6000 Series, CWA, CWV Series : C# Code VB Code CUWIN 3000/4000 Series: C# Code

Read More »

Modbus CRC Algorithm for .Net

The following source code for C# and VB.Net can be used to compute the CRC for Modbus RTU communication.  It can be used on any version of the .Net Framework including the .Net Compact Framework for our Windows CE Panel PCs.VB.NetPublic Class Crc16    Private Shared CrcTable As UShort() = {&H0, &Hc0c1, &Hc181, &H140, &Hc301, &H3c0, [...]

Read More »

MoaTouch (MTA-070W) Sample Program

The following is a sample program demonstrating the MoaTouch's real-time clock, digital inputs, digital outputs, and analog inputs. Download source code.

Read More »

Shipping To Europe and Asia

We are happy to announce that we now offer discounted shipping to the following 24 countries:Australia, Belgium, China, Czech Republic, France, Germany, Hong Kong, India, Indonesia, Italy, Japan, Luxembourg, Malaysia, Netherlands, New Zealand, Philippines, Poland, Singapore, Spain, Sweden, Switzerland, Thailand, United Kingdom and VietNam.Products shipping to these countries will ship directly from Korea. 

Read More »

New Website

Welcome To Comfile Technology's New Website Comfile Technology has redesigned its website.  While it is new and maybe unfamiliar, we hope you'll find it more convenient to navigate and locate the information you're looking for.Navigation The navigation menu across the top will provide general information about us and our products. The "Shop" navigation menu on the left will [...]

Read More »


What's News


Contact Us

Toll-Free : (888) 9CUBLOC, (888) 928 2562
Office : (571) 322-5010
Fax : (571) 322-5011

14 Pidgeon hill dr. #310 Sterling VA 20165
Office Hours: Mon-Thu 9am-5:30pm EST
Fri 9am-5:00pm EST

Sales: sales@comfiletech.com
TechSupport: support@comfiletech.com

Copyright 2015 Comfile Technology,Inc. All Rights Reserved.