using System;
using System.Net.Sockets;
using Modbus.Data;
using Modbus.Device;

namespace ModbusMasterExample
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient client = new TcpClient("localhost", 502); // 连接到Modbus TCP服务器
            ModbusIpMaster master = ModbusIpMaster.CreateIp(client); // 创建Modbus Master

            ushort startAddress = 0;
            ushort numRegisters = 10;

            // 读取线圈状态
            bool[] coils = master.ReadCoils(1, startAddress, numRegisters);
            Console.WriteLine("Coils: " + String.Join(", ", coils));

            // 读取离散输入状态
            bool[] inputs = master.ReadInputs(2, startAddress, numRegisters);
            Console.WriteLine("Inputs: " + String.Join(", ", inputs));

            // 读取保持寄存器
            ushort[] holdingRegisters = master.ReadHoldingRegisters(3, startAddress, numRegisters);
            Console.WriteLine("Holding Registers: " + String.Join(", ", holdingRegisters));

            // 读取输入寄存器
            ushort[] inputRegisters = master.ReadInputRegisters(4, startAddress, numRegisters);
            Console.WriteLine("Input Registers: " + String.Join(", ", inputRegisters));
        }
    }
}

实例2:

private static void Main(string[] args)
        {
            try
            {
                ModbusSerialRtuMasterWriteRegisters();
                ModbusTcpMasterReadInputs();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
 
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
 
        /// <summary>
        /// Simple Modbus serial RTU master write holding registers example.
        /// </summary>
        public static void ModbusSerialRtuMasterWriteRegisters()
        {
            using (SerialPort port = new SerialPort("COM3"))
            {
                // configure serial port
                port.BaudRate = 9600;
                port.DataBits = 8;
                port.Parity = Parity.None;
                port.StopBits = StopBits.One;
                port.Open();
 
                var factory = new ModbusFactory();
                IModbusMaster master = factory.CreateRtuMaster(port);
 
                byte slaveId = 1;
                ushort startAddress = 100;
                ushort[] registers = new ushort[] { 1, 2, 3 };
 
                // write three registers
                master.WriteMultipleRegisters(slaveId, startAddress, registers);
            }
        }
 
        /// <summary>
        ///     Simple Modbus TCP master read inputs example.
        /// </summary>
        public static void ModbusTcpMasterReadInputs()
        {
            using (TcpClient client = new TcpClient("127.0.0.1", 502))
            {
                var factory = new ModbusFactory();
                IModbusMaster master = factory.CreateMaster(client);
 
                // read five input values
                ushort startAddress = 100;
                ushort numInputs = 5;
                bool[] inputs = master.ReadInputs(0, startAddress, numInputs);
 
                for (int i = 0; i < numInputs; i++)
                {
                    Console.WriteLine($"Input {(startAddress + i)}={(inputs[i] ? 1 : 0)}");
                }
            }
 
            // output:
            // Input 100=0
            // Input 101=0
            // Input 102=0
            // Input 103=0
            // Input 104=0
        }

上述示例代码创建了一个Modbus TCP客户端,然后使用Modbus Master对象实现了读取线圈状态、离散输入状态、保持寄存器和输入寄存器的功能。当然,实际使用时需要根据实际情况进行修改。

NModbus库在本页面下载

相关新闻

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

邮箱

cloud@modbus.cn

QQ
QQ
微信
微信
分享本页
返回顶部