Sign in to follow this  
Followers 0
PaulDietz

NModbus4 - DataStoreWrittenTo event not firing

2 posts in this topic

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

 

LadderLogicC0Off.png

LadderLogicC0On.png

ConsoleApplication.png

WireShark.png

NetEdit3 General Settings.png

NetEdit3 Peer to Peer Config.png

Share this post


Link to post
Share on other sites

Closing issue -  Updated ECOM100 ECWX IBox and opened private fire wall.  Also put the listen on a seperate thread.  Listener is working now.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0