Search the Community

Showing results for tags 'modbuslistener'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Found 1 result

  1. Please find listed below the NModbus4 console code.  The DataStoreWrittenTo event is not firing when I do the write to PLC C0 relay coil.  Can anyone tell me how to get the event to fire?  using Modbus.Data; 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; using System.Threading.Tasks; namespace NModbus4ListenerDemo { class Program { #region "Properties" static TcpClient tcpClient = new TcpClient(); static ModbusIpMaster PLCModbusMaster = ModbusIpMaster.CreateIp(tcpClient); static string C0_RelayCoil_Address = "3073"; static ushort C0_RelayCoil_BitAddress = Convert.ToUInt16(Convert.ToInt16(C0_RelayCoil_Address) - 1); #endregion #region "Setup Listener" static IPAddress MiddlewareSlaveIPAddress = IPAddress.Parse("10.95.11.34"); static TcpListener MiddlewareSlaveTcpListener = new TcpListener(MiddlewareSlaveIPAddress, Convert.ToInt32("502")); static ModbusSlave middlewareslave = ModbusTcpSlave.CreateTcp(Convert.ToByte("255"), MiddlewareSlaveTcpListener); #endregion static void Main(string[] args) { string PLCMasterIPAddress = "10.95.11.32"; tcpClient.BeginConnect(PLCMasterIPAddress, 502, null, null); Thread.Sleep(100); #region "Set Relay Coit C0 to false" PLCModbusMaster.WriteSingleCoil(C0_RelayCoil_BitAddress, false); Thread.Sleep(50); bool[] C0_RelayCoil_BitValue = PLCModbusMaster.ReadCoils(C0_RelayCoil_BitAddress, 1); string C0RelayCoilBitValue = C0_RelayCoil_BitValue[0] ? "True" : "False"; Console.WriteLine("C0 Relay Coil = " + C0RelayCoilBitValue); Thread.Sleep(50); #endregion #region "Create DataStore and start listing" MiddlewareSlaveTcpListener.Start(); middlewareslave.DataStore = DataStoreFactory.CreateDefaultDataStore(); middlewareslave.DataStore.DataStoreWrittenTo += ModbusDataStore_DataStoreWrittenTo; middlewareslave.Listen(); Thread.Sleep(50); #endregion #region "Set Relay Coit C0 to false" PLCModbusMaster.WriteSingleCoil(C0_RelayCoil_BitAddress, true); Thread.Sleep(50); C0_RelayCoil_BitValue = PLCModbusMaster.ReadCoils(C0_RelayCoil_BitAddress, 1); C0RelayCoilBitValue = C0_RelayCoil_BitValue[0] ? "True" : "False"; Console.WriteLine("C0 Relay Coil = " + C0RelayCoilBitValue); Thread.Sleep(50); #endregion Console.ReadLine(); #region "ModbusDataStore_DataStoreWrittenTo" void ModbusDataStore_DataStoreWrittenTo(object sender, DataStoreEventArgs e) { Console.WriteLine("ModbusDataStore_DataStoreWrittenTo event fired!"); } #endregion } } } I've attached ladder logic screen shots showing the C0 relay coil being set on by the console application. The console application displaying the messages.  Note the absence of the "ModbusDataStore_DataStoreWrittenTo event fired!" message. WireShark showing the TCP Modbus traffic between the modbus master and slave over port 502.   The PLC NetEdit3 Modbus information Paul