| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 | using System;using System.Collections.Generic;using System.Data;using System.Diagnostics;using System.IO;using System.Linq;using System.Net;using System.Net.Sockets;using System.Runtime.InteropServices;using System.Text;using System.Text.RegularExpressions;using System.Threading;using System.Threading.Tasks;using System.Windows;using Aitex.Core.RT.Log;using Aitex.Core.Util;using MECF.Framework.RT.Core.IoProviders;namespace MECF.Framework.Simulator.Core.IoProviders{    public class PlcBuffer    {        public IoType Type;        public int Offset;        public int Size;        public byte[] Buffer;        public bool[] BoolValue;        public ushort[] ShortValue;    }    public class MCProtocolPlcSimulator    {        public event Action<string> NotifyEvent;        private PeriodicJob _threadSocket;        private PeriodicJob _threadTimer;        private Socket _socketServer;        private string _ip;         private bool _stopFlag;        protected List<PlcBuffer> _buffers = new List<PlcBuffer>();         private int _port = 6731;        private int _socketId = 101;        private int _stationId = 102;        private byte[] _bufferIn;        private byte[] _bufferOut;         private MCProtocol.MC_RESPONSE_HEADER _response;         public MCProtocolPlcSimulator(string ip, int port)        {            _ip = ip;            _port = port;            _response = new MCProtocol.MC_RESPONSE_HEADER()            {                ProtocolId = MCProtocol.MC_SUBHEADER_RESPONSE_MESSAGE,                NetworkId = (byte)_socketId,                StationId = (byte)_stationId,                RequestIoNumber = MCProtocol.MC_REQUEST_MODULE_IO_NUMBER,                RequestStationNumber = MCProtocol.MC_REQUEST_MODULE_STATION_NUMBER,                ResponseDataLen = 0,                CompleteCode = (ushort)(MCProtocol.MC_COMPLETE_CODE_SUCCESS  ),            };             _bufferOut = new byte[2048];            _bufferIn = new byte[2048];            _socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            _threadSocket = new PeriodicJob(50, OnMonitor, "MCProtocolPlcSimulator",true);            _threadTimer = new PeriodicJob(1000, OnTimer, "MCProtocolPlcSimulatorTimer", true);        }        protected virtual bool OnTimer()        {            return true;        }        private void PerformNotifyEvent(string msg)        {            if (NotifyEvent != null)            {                NotifyEvent(msg);            }        }        private bool OnMonitor()        {            try            {                EndPoint ep = new IPEndPoint(IPAddress.Loopback, _port);                _socketServer.Bind(ep);                _socketServer.Listen(3);                Socket s;                while (!_stopFlag)                {                    PerformNotifyEvent("Waiting for connection ...");                    s = _socketServer.Accept();                    PerformNotifyEvent("Connected.");                                        s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);                    try                    {                        while (!_stopFlag && OnTransmission(s))                        {                            PerformNotifyEvent("Waiting for another command ...");                        }                        PerformNotifyEvent("receive error buffer content and exit ...");                    }                    catch (Exception ex)                    {                        LOG.WriteExeption(ex);                        return true;                    }                    PerformNotifyEvent("A client disconnected from port " + _port);                    s = null;                }            }            catch (Exception ex)            {                LOG.WriteExeption(ex);                try                {                    _socketServer.Close();                }                catch (Exception )                {                                     }                _socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);                return true;            }            return true;        }        public void Start()        {            _threadSocket.Start();        }        public void Stop()        {            _stopFlag = true;            _threadSocket.Stop();        }        protected bool OnTransmission(Socket s)        {            int size = s.Receive(_bufferIn, 0, MCProtocol.MC_QHEADER_COMMAND_SIZE, SocketFlags.None);            if (size < MCProtocol.MC_QHEADER_COMMAND_SIZE)            {                return false;            }            MCProtocol.MC_COMMAND_HEADER header = MCProtocol.ToStruct<MCProtocol.MC_COMMAND_HEADER>(_bufferIn);            int dataLength = header.RequestDataLen - MCProtocol.MC_BATCH_COMMAND_SIZE - MCProtocol.JUNK_SIZE;            if (dataLength < 0)            {                return false;            }            size = s.Receive(_bufferIn, 0, MCProtocol.MC_BATCH_COMMAND_SIZE, SocketFlags.None);            if (size < MCProtocol.MC_BATCH_COMMAND_SIZE)            {                return false;            }            MCProtocol.MC_BATCH_COMMAND cmd = MCProtocol.ToStruct<MCProtocol.MC_BATCH_COMMAND>(_bufferIn);            if (dataLength > 0)            {                size = s.Receive(_bufferIn, 0, dataLength, SocketFlags.None);                if (size < dataLength)                {                    return false;                }            }             int offset = cmd.Reserved * (0xFFFF+1) + cmd.HeadAddr;            byte[] data = null;            if (cmd.Command == MCProtocol.MC_COMMAND_BATCH_READ)            {                                 data = GetData(offset, cmd.DevicePoints, cmd.SubCommand == MCProtocol.MC_SUBCOMMAND_BIT_UNITS);            }            else            {                SetData(offset, _bufferIn, dataLength, cmd.SubCommand == MCProtocol.MC_SUBCOMMAND_BIT_UNITS);            }            _response.ResponseDataLen = (ushort)(MCProtocol.JUNK_SIZE + (data == null ? 0 : data.Length));            byte[] response = MCProtocol.Struct2Bytes(_response);            Array.Copy(response, 0, _bufferOut, 0, response.Length);            s.Send(_bufferOut, response.Length, SocketFlags.None);             if (data != null)            {                Array.Copy(data, 0, _bufferOut, 0, data.Length);                s.Send(_bufferOut, data.Length, SocketFlags.None);             }            return true;        }         protected void SetData(int headAddr, byte[] data, int length, bool isBit)        {            IoType ioType = isBit ? IoType.DO : IoType.AO;            if (ioType == IoType.DO)            {                bool[] boolData = MCProtocol.TransByteDataToBoolArray(data, 0, length);                PlcBuffer buffer = _buffers.Find(x => x.Offset == headAddr && x.Type == ioType && x.Size == boolData.Length);                if (buffer != null)                {                    buffer.BoolValue = boolData;                }            }            else            {                ushort[] shortValue = MCProtocol.Byte2Ushort(data, 0, length);                PlcBuffer buffer = _buffers.Find(x => x.Offset == headAddr && x.Type == ioType && x.Size == shortValue.Length);                if (buffer != null)                {                    buffer.ShortValue = shortValue;                }                             }         }         protected void SetDi(int headAddr, int offset, bool value)        {            PlcBuffer buffer = _buffers.Find(x => x.Offset == headAddr && x.Type ==  IoType.DI );            if (buffer != null)            {                buffer.BoolValue[offset] = value;            }        }        protected void SetAi(int headAddr, int offset, ushort value)        {            PlcBuffer buffer = _buffers.Find(x => x.Offset == headAddr && x.Type ==   IoType.AI  );            if (buffer != null)            {                buffer.ShortValue[offset] = value;            }        }        protected virtual byte[] GetData(int headAddr, ushort length, bool isBit)        {            IoType ioType = isBit ? IoType.DI : IoType.AI;            PlcBuffer buffer = _buffers.Find(x => x.Offset == headAddr && x.Type == ioType && x.Size == length);            if (buffer == null)            {                return null;            }            byte[] result = null;            if (ioType == IoType.DI)            {                bool[] value = buffer.BoolValue;                int size = (value.Length + 1) / 2;                 result = new byte[size];                for (int i = 0; i < size; i++)                {                    if (value[i * 2 + 0])                        result[i] += 0x10;                    if ((i * 2 + 1) < value.Length)                    {                        if (value[i * 2 + 1])                            result[i] += 0x01;                    }                }                //result = MCProtocol.TransBoolArrayToByteData(buffer.BoolValue);            }            else            {                result = MCProtocol.Ushort2Byte(buffer.ShortValue);            }            return result;        }     }}
 |