.NET:Modbus-RTU Master Simple Read and Write 操作

using System;
using EasyModbus;
 
namespace ModbusRS485Master
{
	class Program
	{
		public static void Main(string[] args)
		{
			ModbusClient modbusClient = new ModbusClient("COM1");
			//modbusClient.UnitIdentifier = 1; Not necessary since default slaveID = 1;
			//modbusClient.Baudrate = 9600;	// Not necessary since default baudrate = 9600
			//modbusClient.Parity = System.IO.Ports.Parity.None;
			//modbusClient.StopBits = System.IO.Ports.StopBits.Two;
			//modbusClient.ConnectionTimeout = 500;			
			modbusClient.Connect();
			
			Console.WriteLine("Value of Discr. Input #1: "+modbusClient.ReadDiscreteInputs(0,1)[0].ToString());	//Reads Discrete Input #1
			Console.WriteLine("Value of Input Reg. #10: "+modbusClient.ReadInputRegisters(9,1)[0].ToString());	//Reads Inp. Reg. #10
			
			modbusClient.WriteSingleCoil(4,true);		//Writes Coil #5
			modbusClient.WriteSingleRegister(19,4711);	//Writes Holding Reg. #20
			
			Console.WriteLine("Value of Coil #5: "+modbusClient.ReadCoils(4,1)[0].ToString());	//Reads Discrete Input #1
			Console.WriteLine("Value of Holding Reg.. #20: "+modbusClient.ReadHoldingRegisters(19,1)[0].ToString());	//Reads Inp. Reg. #10
			modbusClient.WriteMultipleRegisters(49, new int[10] {1,2,3,4,5,6,7,8,9,10});
			modbusClient.WriteMultipleCoils(29, new bool[10] {true,true,true,true,true,true,true,true,true,true,});
			
			Console.Write("Press any key to continue . . . ");
			Console.ReadKey(true);
		}
	}
}

NET:Modbus-TCP 客户端简单读写操作

namespace ModbusClientApplication
{
    class Program
    {
        public static void Main(string[] args)
        {
            ModbusClient modbusClient = new ModbusClient("190.201.100.100", 502);    //Ip-Address and Port of Modbus-TCP-Server
            modbusClient.Connect();                                                    //Connect to Server
            modbusClient.WriteMultipleCoils(4, new bool[] {true, true, true, true, true, true, true, true, true, true});    //Write Coils starting with Address 5
            bool[] readCoils = modbusClient.ReadCoils(9,10);                        //Read 10 Coils from Server, starting with address 10
            int[] readHoldingRegisters = modbusClient.ReadHoldingRegisters(0,10);    //Read 10 Holding Registers from Server, starting with Address 1

            // Console Output
            for (int i = 0; i < readCoils.Length; i++)
                Console.WriteLine("Value of Coil " + (9 + i + 1) + " " + readCoils[i].ToString());
                        
            for (int i = 0; i < readHoldingRegisters.Length; i++)
                Console.WriteLine("Value of HoldingRegister " + (i + 1) + " "+ readHoldingRegisters[i].ToString());            
            modbusClient.Disconnect();                                                //Disconnect from Server
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}

模组服务器读取值并将值发布到 MQTT 代理

 class Program
    {
        static void Main(string[] args)
        {
            EasyModbus.ModbusClient modbusClient = new EasyModbus.ModbusClient("127.0.0.1", 502);
            //Increase the Connection Timeout to 5 seconds
            modbusClient.ConnectionTimeout = 5000;
            //We create a Log File. This will also active the Event logging 
            modbusClient.LogFileFilename = "test.txt";
            //The Messages sent to the MQTT-Broker will be retained in the Broker. After subscribtion, the client will automatically
            //receive the last retained message. By default the Retain-Flag is FALSE -> Messages will not be retained
            modbusClient.MqttRetainMessages = true;
            //Connect to the ModbusTCP Server
            modbusClient.Connect();
            while (true)
            {
                // We read two registers from the Server, and Publish them to the MQTT-Broker. By default Values will be published
                // on change By default we publish to the Topic 'easymodbusclient/' and the the address e.g. ''easymodbusclient/holdingregister1'
                // The propery "Password" and "Username" can be used to connect to a Broker which require User and Password. By default the 
                //MQTT-Broker port is 1883
                int[] holdingRegister = modbusClient.ReadHoldingRegisters(60, 2, "www.mqtt-dashboard.com");
                System.Threading.Thread.Sleep(1000);
            }
            modbusClient.Disconnect();
            Console.ReadKey();
        }
    }

自动从模组服务器轮询值并将其发布到 MQTT 代理 – 主题已更改

        static void Main(string[] args)
        {
            EasyModbus.EasyModbus2Mqtt easyModbus2Mqtt= new EasyModbus.EasyModbus2Mqtt();
            //easyModbus2Mqtt.AutomaticReconnect = false;
            easyModbus2Mqtt.MqttUserName = "sr555";
            easyModbus2Mqtt.MqttPassword = "******************";
            easyModbus2Mqtt.MqttBrokerPort = 18972;
            //easyModbus2Mqtt.MqttBrokerAddress = "broker.hivemq.com";
            //easyModbus2Mqtt.MqttBrokerAddress = "192.168.178.101";
            easyModbus2Mqtt.MqttBrokerAddress = "m21.cloudmqtt.com";
            easyModbus2Mqtt.ModbusIPAddress = "127.0.0.1";
            easyModbus2Mqtt.AddReadOrder(EasyModbus.FunctionCode.ReadCoils, 2, 0, 200, new string[] { "easymodbusclient/customtopic1", "easymodbusclient/customtopic2" });
            easyModbus2Mqtt.AddReadOrder(EasyModbus.FunctionCode.ReadInputRegisters, 2, 0, 200);
            EasyModbus.ReadOrder readOrder = new EasyModbus.ReadOrder();
            readOrder.Hysteresis = new int[10] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 };
            readOrder.Unit = new string[10] {"°C", "°C", "°C", "°C", "°C", "°C", "°C", "°C", "°C", "°C"};
            readOrder.Scale = new float[10] { 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f };
            readOrder.Quantity = 10;
            readOrder.StartingAddress = 10;
            readOrder.FunctionCode = EasyModbus.FunctionCode.ReadHoldingRegisters;          
            easyModbus2Mqtt.AddReadOrder(readOrder);
            easyModbus2Mqtt.start();
        }

Modbus-TCP 服务器,将更改时的值发布到 MQTT-Broker

   class Program
    {
        static void Main(string[] args)
        {
            Program application = new Program();
            application.startServer();

        }

        public void startServer()
        {
            EasyModbus.ModbusServer modbusServer = new EasyModbus.ModbusServer();
            modbusServer.MqttRootTopic = "examplemodbusserver";
            modbusServer.MqttBrokerAddress = "www.mqtt-dashboard.com";
            modbusServer.Listen();
            modbusServer.holdingRegistersChanged += new EasyModbus.ModbusServer.HoldingRegistersChanged(holdingRegistersChanged);
            Console.ReadKey();
            
            modbusServer.StopListening();
        }

        public void holdingRegistersChanged(int startingAddress, int quantity)
        {
            Console.WriteLine(startingAddress);
            Console.WriteLine(quantity);
        }
    }

EasyModbusTCP/UDP/RTU .NET库链接在本页下载

相关新闻

发表回复

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

邮箱

cloud@modbus.cn

QQ
QQ
微信
微信
SHARE
TOP