On the screen Multiway program that allows you to communicate with devices via a Modbus. That's what I'm interested: this program can work in modbus slave mode via TCP or COM port.
How can this be repeated in c#? How to get a packet 0A 05 00 0DFF00 1C 82from the master in the console via TCP or COM port?
The packet 0A 05 00 0DFF00 1C 82 means writing the 13th register of the Slave device No.10 (PC) to the "True" position. Detailed transcript here (Modbus RTU, Request)...
I understand how to implement master mode using Easy Modbus.dll. I can't implement the slave mode. I was advised the NModbus library. At the moment without success:
check1
NModbus Slave Network ignoring request intended for NModbus Slave 1
Code:
using System.IO.Ports;
using NModbus;
using NModbus.Serial;
const string PrimarySerialPortName = "COM3";
var cts = new CancellationTokenSource();
Console.CancelKeyPress += (sender, eventArgs) => cts.Cancel();
try
{
await StartModbusSerialRtuSlaveNetwork(cts.Token);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
return 0;
static async Task StartModbusSerialRtuSlaveNetwork(CancellationToken cancellationToken)
{
using (SerialPort slavePort = new SerialPort(PrimarySerialPortName))
{
// configure serial port
slavePort.BaudRate = 57600;
slavePort.DataBits = 8;
slavePort.Parity = Parity.None;
slavePort.StopBits = StopBits.One;
slavePort.Open();
IModbusFactory factory = new ModbusFactory();
IModbusSlaveNetwork modbusSlaveNetwork = factory.CreateRtuSlaveNetwork(slavePort);
slavePort.ReadTimeout = 100;
slavePort.WriteTimeout = 500;
//var acTechDataStore = new SlaveStorage();
ISlaveDataStore dataStore = new NModbus.Data.SlaveDataStore();
Console.WriteLine("check1");
IModbusSlave slave10 = factory.CreateSlave(10);
modbusSlaveNetwork.AddSlave(slave10);
await modbusSlaveNetwork.ListenAsync(cancellationToken);
Console.WriteLine("check2");
await Task.Delay(1, cancellationToken);
}
}