Пример #1
0
        public void SendDataToPlcsim(PlcS7onlineMsgPump.WndProcMessage msg)
        {
            Int32 length = msg.pdu.Length + 4;

            byte[] buffer = new byte[length];

            byte[] pduLengthBytes = BitConverter.GetBytes(msg.pdulength);
            Buffer.BlockCopy(pduLengthBytes, 0, buffer, 0, pduLengthBytes.Length);
            Buffer.BlockCopy(msg.pdu, 0, buffer, pduLengthBytes.Length, msg.pdu.Length);

            IntPtr ptr = Marshal.AllocHGlobal(length);

            Marshal.Copy(buffer, 0, ptr, length);

            SendMessage(m_PlcS7onlineMsgPump_Handle, PlcS7onlineMsgPump.WM_M_SENDDATA, IntPtr.Zero, ptr);

            Marshal.FreeHGlobal(ptr);
        }
Пример #2
0
        public void IsoReceived(IsoServiceProvider client, byte[] data)
        {
            // Skip empty messages
            if (data.Length < 1)
            {
                return;
            }

            // On the first S7 protocol telegram, determine the Plcsim version which has to be used
            // by checking the protocol type in S7 protocol header
            // 0x32 = S7comm
            // 0x72 = S7commPlus (1200/1500)
            if (client.m_S7ProtocolVersionDetected == false)
            {
                string message        = String.Empty;
                bool   plcsim_success = false;
                if (data[0] == 0x72)
                {
                    plcsim_success = client.InitPlcsim(PlcsimProtocolType.S7commPlus);
                    message        = "Connecting to Plcsim using S7Comm-Plus mode for 1200/1500";
                }
                else
                {
                    plcsim_success = client.InitPlcsim(PlcsimProtocolType.S7comm);
                    message        = "Connecting to Plcsim using S7Comm mode for 300/400 or 1200/1500 (not optimized)";
                }
                if (plcsim_success == false)
                {
                    if (monitorDataReceived != null)
                    {
                        monitorDataReceived(client.client.RemoteEndPoint.ToString(), null, "Failed to connect to Plcsim");
                    }
                    client.client.EndConnection();
                    return;
                }
                if (monitorDataReceived != null)
                {
                    monitorDataReceived(client.client.RemoteEndPoint.ToString(), null, message);
                }
                client.m_S7ProtocolVersionDetected = true;
            }

            PlcS7onlineMsgPump.WndProcMessage msg = new PlcS7onlineMsgPump.WndProcMessage();
            msg.pdu       = data;
            msg.pdulength = data.Length;

            if (monitorDataReceived != null)
            {
                monitorDataReceived(client.client.RemoteEndPoint.ToString(), data, String.Empty);
            }
            byte[] res = null;

            // Test if we have to generate our own answer
            res = S7ProtoHook.RequestExchange(data);
            if (res == null)
            {
                client.SendDataToPlcsim(msg);
            }
            else
            {
                client.IsoSend(client.client, res);
            }

            // JYB
            DataReceived?.Invoke(data);
        }