PaulDietz

MrPLC Member
  • Content count

    4
  • Joined

  • Last visited

Posts posted by PaulDietz


  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

     

    LadderLogicC0Off.png

    LadderLogicC0On.png

    ConsoleApplication.png

    WireShark.png

    NetEdit3 General Settings.png

    NetEdit3 Peer to Peer Config.png


  2. I have an Automation Direct DL06 setup in a peer to peer configuration.  The PLC is the master and the PC is the slave.  The ladder logic is straight forward, just sending a double word (4 bytes) from the master starting a V3000 to PC.  I've set V3000 to "Test".  See the attached DebugLadderLogic1 image. 

    When the ladder logic runs the ModbusDataStore_DataReceived event fires.  The code below is my best attempt to get the value "Test" from the modbus data store.  Instead of getting "Test", I get 25940 and 29811...  I would appreciate any assistance getting the "Test" value out of the data store modbus memory. 

    DebugLadderLogic1.PNG

    NModbus4 data capture event.PNG