1.概要
bool[] inputs = master.ReadCoils(1, startAddress, numInputs);
2.代码
using Modbus.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp16
{
class Program
{
static void Main(string[] args)
{
test1();
Console.ReadKey();
}
static private void test1()
{
IPAddress address = new IPAddress(new byte[] { 127, 0, 0, 1 });
using (TcpClient client = new TcpClient(address.ToString(), 502))
{
client.SendTimeout = 1;
ModbusIpMaster master = ModbusIpMaster.CreateIp(client);
ushort startAddress = 0;
ushort numInputs = 10;
bool[] inputs = master.ReadCoils(2, startAddress, numInputs);
for (int i = 0; i < numInputs; i++)
{
Console.WriteLine($"Input {(startAddress + i)}={(inputs[i] ? 1 : 0)}");
}
}
}
}
}
3.运行结果

服务端生成的报文
0001 0000 0006 02 01 0000 000A
事务标识 协议标识 长度 单元标识 功能码 起始地址 读线圈的数量
ReadCoils (1, 0, 10)
该代码示例展示了如何通过Modbus Ip Master库连接到本地IP地址为127.0.0.1的设备,并从起始地址0读取10个线圈的状态。读取的数据以bool数组形式返回,并打印出来。
919

被折叠的 条评论
为什么被折叠?



