Use of Nmodbus

Source: Internet
Author: User

In a recent project, we need to use Modbus RTU to communicate with PLC, and now we will use the process record for future reference.


First, what is the Modbus communication protocol


The Modbus protocol is a common language applied to electronic controllers that supports traditional RS-232, RS-422, RS-485, and Ethernet devices.


Modbus function code
01 READ COIL STATUS
02 READ INPUT STATUS
03 READ HOLDING REGISTER
04 READ INPUT REGISTER
05 WRITE single COIL
06 WRITE single REGISTER
15 WRITE multiple COIL
16 WRITE multiple REGISTER
Ii.. NET implementation of Modbus communication protocol


Because the implementation is not difficult, there are many online users of their own implementation, but here I recommend a github open Source implementation class Library: Nmodbus4,github address: HTTPS://GITHUB.COM/NMODBUS4/NMODBUS4. For TCP, UDP, RTU and other Modbus communication methods have been implemented.






Read and Write methods:


Method name Role Required Parameters return value corresponding function code
Readcoils Read the status of Do

Slave address (8-bit)

BYTE slaveaddress

Start address (16-bit)


UShort Startaddress

Number of reads (16-bit)

UShort Numberofpoints

Bool[] 01
Readinputs Read the state of Di

Slave address (8-bit)

BYTE slaveaddress

Start address (16-bit)


UShort Startaddress

Number of reads (16-bit)

UShort Numberofpoints

Bool[] 02
Readholdingregisters Read the value of AO

Slave address (8-bit)

BYTE slaveaddress

Start address (16-bit)

UShort Startaddress

Number of reads (16-bit)

UShort Numberofpoints

Ushort[] 03
Readinputregisters Read the value of AI

Slave address (8-bit)

BYTE slaveaddress

Start address (16-bit)


UShort Startaddress

Number of reads (16-bit)

UShort Numberofpoints

Ushort[] 04
Writesinglecoil Write value to do

Slave address (8-bit)

BYTE slaveaddress

Coil Address (16-bit)

UShort Coiladdress

Write Value (Boolean)

BOOL Value

No return value 05
Writesingleregister Write value to AO

Slave address (8-bit)

BYTE slaveaddress

Register address (16-bit)

UShort Registeraddress

Write value (16-bit)

UShort Value

No return value 06
Writemultiplecoils Write multi-coil registers

Slave address (8-bit)

BYTE slaveaddress

Start address (16-bit)

UShort Startaddress

Write value (Boolean array)

Bool[] Data

No return value 15
Writemultipleregisters Write multiple hold Registers

Slave address (8-bit)

BYTE slaveaddress


Start address (16-bit)

UShort Startaddress,

Register value (16-bit integer array)

Ushort[] Data

No return value 16
Readwritemultipleregisters Read and write multiple hold registers

Slave address (8-bit)

BYTE slaveaddress

Read start address (16-bit)

UShort Startreadaddress


Number of reads (16-bit)

UShort Numberofpointstoread,

Write start address (16-bit)

UShort Startwriteaddress,

Write value (16-bit integer array)

Ushort[] WriteData

Ushort[] 23


Each method has its own asynchronous method implementation.






Here is an RTU to show how to use it:



1 /// <summary>
 2 /// Create a ModBus RTU connection
 3 /// </summary>
 4 /// <param name="portName">port number</param>
 5 /// <param name="baudRate">Baud rate</param>
 6 /// <param name="parity">parity bit </param>
 7 /// <param name="dataBits">data bits</param>
 8 /// <param name="stopBits">stop bit</param>
 9 /// <returns></returns>
10 public IModbusSerialMaster CreateModBusRtuConnection(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
11 {
12 return CreateModBusRtuConnect(new SerialPort(portName, baudRate, parity, dataBits, stopBits));
13 }
14
15 public IModbusSerialMaster CreateModBusRtuConnection(SerialPort serialPort)
16 {
17 IModbusSerialMaster master = null;
18 if (!serialPort.IsOpen)
19 {
20 try
twenty one                 {
22 serialPort.Open();
twenty three                 }
24 catch (Exception e)
25 {
26 throw e;
27 }
28 }
29 try
30 {
31 master = ModbusSerialMaster.CreateRtu(serialPort);
32 /// can also configure some parameters of the master
33 master.Transport.ReadTimeout = 100; / / read data timeout 100ms
34 master.Transport.WriteTimeout = 100; / / write data timeout 100ms
35 master.Transport.Retries = 3;//Retry times
36 master.Transport.WaitToRetryMilliseconds = 10;//Retry interval
37
38 }
39 catch(Exception e)
40 { throw e;
41 }
42 return master;
43 }
44 }


View Code



Use of Nmodbus


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.