C # Modbus Data read using NMODBUS4 Library

Source: Internet
Author: User

Modbus Communication Protocol

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

Modbus Slave

This is the installation package Modbusslavesetup64bit registration code:54555451475662

Modbus Communication Protocol Net Library

This is a lot of open source libraries, my side of the library is GitHub's open Source implementation class Library: Nmodbus4,github address: HTTPS://GITHUB.COM/NMODBUS4/NMODBUS4

I use Modbus slave to simulate a slave machine

Setting up data

NMODBUS4 Read Slave code example

1IPAddress address =NewIPAddress (New byte[] {127,0,0,1 });2             using(TcpClient client =NewTcpClient (address. ToString (),502))3             {4Client. Sendtimeout =1;5                 //Client.op6Modbusipmaster master =modbusipmaster.createip (client);7                 //Master.op8                 //read five input values9                 ushortStartaddress =0;Ten                 ushortNuminputs =Ten; One                 BOOL[] inputs = Master. Readcoils (1, startaddress, numinputs); A  -                  for(inti =0; i < numinputs; i++) -                 { theConsole.WriteLine ($"Input {(startaddress + i)}={(inputs[i]? 1:0)}"); -                 } -  -}

Here is worth noting is the host read method the first parameter is SlaveID if not added startaddress and numinputs Nmodbus will default SlaveID is 01 straight exception (d (╯﹏╰) B painful experience ~ ~)

The above data is the do state just set

In the demo of a read AI value

The code is not bad how much is the method readinputregisters different. Here is the sample code

1IPAddress address =NewIPAddress (New byte[] {127,0,0,1 });2             using(TcpClient client =NewTcpClient (address. ToString (),502))3             {4Client. Sendtimeout =1;5                 //Client.op6Modbusipmaster master =modbusipmaster.createip (client);7                 //Master.op8                 //read five input values9                 ushortStartaddress =0;Ten                 ushortNuminputs =Ten; One                 ushort[] inputs = Master. Readinputregisters (2, startaddress, numinputs); A  -                  for(inti =0; i < numinputs; i++) -                 { theConsole.WriteLine ($"Register {(startaddress + i)}={(Inputs[i])}"); -                 } -  -}

C # Modbus Data read using NMODBUS4 Library

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.