開發者網頁: http://easymodbustcp.net/en/
範 例: http://easymodbustcp.net/codesampleseasymodbustcp-net
NET: Modbus-RTU Master Simple Read and Write operations
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 Client Simple Read and Write operations
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);
}
}
}
-------------------------------------------
Read Values from Modus-Server and Publish the Values to a MQTT-Broker
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();
}
}
----------------------------------------------
沒有留言:
張貼留言