C#上位机与三菱PLC通讯

C#上位机与三菱PLC通讯

废话多说

节奏快的生活,我们都在追求快,本人也是,所以本人分享的东西基本都是适用于想快速上手可以顺利接轨项目的筒子们,让浏览者可以站在别人的肩膀上走的更远,将更多的时间精力花费在研究其他的技术难点上面,比如高并发、高可用、分布式处理、线程间同步等等上面。在通讯协议研究上面可以偷懒,毕竟和标准的硬件通讯协议是死的,无需在此方面上煞费苦心去研究,去优化下逻辑处理它不香吗?

PLC如何设置MC通讯

首先先来介绍一下PLC调试调试软件GXWork2怎么配置MC协议通讯,打开项目工程,选择参数->网络参数,如下图所示:
C#上位机与三菱PLC通讯_第1张图片
点击它,然后选择“内置以太网板设置”,然后点击"开始设定",接着走,
C#上位机与三菱PLC通讯_第2张图片
然后选择 “设定内置以太网参数” 如果选用TCP协议则打开方式务必选取“MC协议”,此处记得勾选上在下面的"允许Run写入",否则可能会出现上位机写入某些寄存器值失败,如下所示:
C#上位机与三菱PLC通讯_第3张图片
C#上位机与三菱PLC通讯_第4张图片

MC通讯协议报文介绍

基于Qna-3E协议进行ASCII的读写寄存器操作

1、读取指令
首先我们先来看下一报文格式:

5000 副头部
00 网络编号
FF PLC编号
03FF IO编号
00 站号
000C 请求数据长度
0010 时钟
0401 指令
0000 子指令
D
数据区
0027A6 起始地址
0001 读取长度*

主要讲下指令和子指令的代号,详细功能介绍见下图:C#上位机与三菱PLC通讯_第5张图片
我们以读取三菱Q系列的链接寄存器W1509为例,我们写入十六进制的报文格式就是:
5000 00 FF FF03 00 0C00 0100 01040000 091500 B4 0100
同理可以适用于D寄存区和B寄存区,由于开头的报文5000 00 FF FF03 00 0C00 0100 01040000是固定的,所以我们可以先统一写成下述的方法来获得我们需要发送到PLC服务端的字节数组:

		public byte[] GetReadByteArr(string ItemName, int ReadCount)
        {
            ItemData = new int[ReadCount];
            byte[] recvData = new byte[1024];
            if (!string.IsNullOrEmpty(ItemName))
            {
                string Get_Type = ItemName.Substring(0, 1);
                string Get_Name = ItemName.Substring(1, ItemName.Length - 1).PadLeft(6, '0');
                byte[] SendTag = new byte[2] { 0x50, 0x00 };
                byte[] SendNetNo = new byte[1] { 0x00 };
                byte[] SendPLCNo = new byte[1] { 0xff };
                byte[] SendIONo = new byte[2] { 0xff, 0x03 };
                byte[] SendSTNo = new byte[1] { 0x00 };
                byte[] SendDataLenth = new byte[2] { 0x0c, 0x00 };
                byte[] Sendtime = new byte[2] { 0x01, 0x00 };
                byte[] SendOrdF = new byte[4] { 0x01, 0x04, 0x00, 0x00 };
                if (Get_Type == "d" | Get_Type == "D")
                {
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0xa8 };
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    return sendBuffer;
                }
                else if (Get_Type == "w" | Get_Type == "W")
                {
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0xb4 };
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    return sendBuffer;
                }
                else if (Get_Type == "b" | Get_Type == "B")
                {
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0xa0 };
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    return sendBuffer;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;

            }
        }

2、写入指令
首先我们先来看下一报文格式:

5000 副头部
00 网络编号
FF PLC编号
03FF IO编号
00 站号
001C 请求数据长度
0010 时钟
1401 指令
0000 子指令
D
数据区
0027A6 起始地址
0001 读取长度
FFFF 写入数据*

其中指令和子指令见上面的指令和子指令功能码介绍,在此不再赘述。
我们以写入三菱Q系列的链接寄存器W1609,写入1为例,我们写入十六进制的报文格式就是:
5000 00 FF FF03 00 0D00 0100 01140000 091600 A80100
同理可以适用于D寄存区,由于开头的报文5000 00 FF FF03 00 和时钟0100指令加子指令01140000是固定的,所以我们可以先统一写成下述的方法来获得我们需要发送到PLC服务端的字节数组:

		public byte[] GetWriteArr(string ItemName, int WriteCount, ref int[] ItemData)
        {
            if (!string.IsNullOrEmpty(ItemName))
            {
                string Get_Type = ItemName.Substring(0, 1);
                string Get_Name = ItemName.Substring(1, ItemName.Length - 1).PadLeft(6, '0');
                byte[] SendTag = new byte[2] { 0x50, 0x00 };
                byte[] SendNetNo = new byte[1] { 0x00 };
                byte[] SendPLCNo = new byte[1] { 0xff };
                byte[] SendIONo = new byte[2] { 0xff, 0x03 };
                byte[] SendSTNo = new byte[1] { 0x00 }; 
                byte[] Sendtime = new byte[2] { 0x01, 0x00 };
                byte[] SendOrdF = new byte[4] { 0x01, 0x14, 0x00, 0x00 };
                if (Get_Type == "d" | Get_Type == "D")
                {
                    byte[] SendDataLenth = new byte[2] { (byte)Convert.ToByte((WriteCount * 2 + 12).ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte((WriteCount * 2 + 12).ToString("X4").Substring(0, 2), 16) };
                   
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0xa8 };//WriteCount.ToString("X4")
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(WriteCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(WriteCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] SendData = new byte[2 * WriteCount];
                    for (int i = 0; i < WriteCount; i++)
                    {
                        SendData[i * 2] = (byte)Convert.ToByte(ItemData[i].ToString("X4").Substring(2, 2), 16);
                        SendData[i * 2 + 1] = (byte)Convert.ToByte(ItemData[i].ToString("X4").Substring(0, 2), 16);
                    }
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length + SendData.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    SendData.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length + SendWordLenth.Length);

                    return sendBuffer;
                }
                else if (Get_Type == "w" | Get_Type == "W")
                {
                    byte[] SendDataLenth = new byte[2] { (byte)Convert.ToByte((WriteCount * 2 + 12).ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte((WriteCount * 2 + 12).ToString("X4").Substring(0, 2), 16) };
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0xb4 };//WriteCount.ToString("X4")
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(WriteCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(WriteCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] Senddata = new byte[2 * WriteCount];
                    for (int i = 0; i < WriteCount; i++)
                    {
                        Senddata[i * 2] = (byte)Convert.ToByte(ItemData[i].ToString("X4").Substring(2, 2), 16);
                        Senddata[i * 2 + 1] = (byte)Convert.ToByte(ItemData[i].ToString("X4").Substring(0, 2), 16);
                    }
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length + Senddata.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    Senddata.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length + SendWordLenth.Length);

                    return sendBuffer;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

3、响应帧内容
C#上位机与三菱PLC通讯_第6张图片
理解同读寄存器指令一样,就不再赘述了,接着走。

*需要注意的是字节的高低位和十六进制的数据引用。

通讯协议的实现

结合之前的博客文章
C#开发的Socket客户端:Socket客户端代码https://blog.csdn.net/weixin_38950816/article/details/116202602
由于MC协议是基于TCP通讯来实现的,我们完全可以将上述的报文协议结合到我们的Socket客户端的代码当中,当我们和Socket服务端的通讯建立成功之后,线程内进行定时的发送读取寄存器数据和进行一系列的逻辑写入进行操作就可以了,当然其中还是有稍稍改动的地方。相当于学以致用了,哈哈哈!
话不多说,上代码:

    public class MistubQPlcLink
    {
        public PlcStatus Plcstatus = PlcStatus.Init;
        public string IPAdress;
        public bool connected = false;
        public Socket clientSocket;
        private IPEndPoint hostEndPoint;
        private Byte[] SendDataPro;
        private Byte[] RecvDataPro;
        private Byte[] SendData;
        private Byte[] RecvData;
        private Byte SendOrRecv;
        private  AutoResetEvent autoConnectEvent = new AutoResetEvent(false);
        private SocketAsyncEventArgs lisnterSocketAsyncEventArgs;

        public delegate void StartListeHandler();
        public event StartListeHandler StartListen;

        public delegate void ReceiveMsgHandler(byte[] info);
        public event ReceiveMsgHandler OnMsgReceived;

        private List<SocketAsyncEventArgs> s_lst = new List<SocketAsyncEventArgs>();

        public MistubQPlcLink(string hostName, int port)
        {
            IPAdress = hostName;
            IPAddress[] hostAddresses = Dns.GetHostAddresses(hostName);
            this.hostEndPoint = new IPEndPoint(hostAddresses[hostAddresses.Length - 1], port);
            this.clientSocket = new Socket(this.hostEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        }
        /// 
        /// 连接服务端
        /// 
        /// 
        private bool Connect()
        {
            using (SocketAsyncEventArgs args = new SocketAsyncEventArgs())
            {
                args.UserToken = this.clientSocket;
                args.RemoteEndPoint = this.hostEndPoint;
                args.Completed += new EventHandler<SocketAsyncEventArgs>(this.OnConnect);
                this.clientSocket.ConnectAsync(args);
                bool flag = autoConnectEvent.WaitOne(2000);
                if (this.connected)
                {
                    this.lisnterSocketAsyncEventArgs = new SocketAsyncEventArgs();
                    byte[] buffer = new byte[1024];
                    this.lisnterSocketAsyncEventArgs.UserToken = this.clientSocket;
                    this.lisnterSocketAsyncEventArgs.SetBuffer(buffer, 0, buffer.Length);
                    this.lisnterSocketAsyncEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(this.OnReceive);
                    this.StartListen();
                    return true;
                }
                return false;
            }
        }
        /// 
        /// 判断有没有连接上
        /// 
        /// 
        /// 
        private void OnConnect(object sender, SocketAsyncEventArgs e)
        {
            this.connected = (e.SocketError == SocketError.Success);
            autoConnectEvent.Set();
        }
        /// 
        /// 发送
        /// 
        /// 
        public void Send(byte[] mes)
        {
            if (this.connected)
            {
                EventHandler<SocketAsyncEventArgs> handler = null;
                byte[] buffer = mes;
                SocketAsyncEventArgs senderSocketAsyncEventArgs = null;
                lock (s_lst)
                {
                    if (s_lst.Count > 0)
                    {
                        senderSocketAsyncEventArgs = s_lst[s_lst.Count - 1];
                        s_lst.RemoveAt(s_lst.Count - 1);
                    }
                }
                if (senderSocketAsyncEventArgs == null)
                {
                    senderSocketAsyncEventArgs = new SocketAsyncEventArgs();
                    senderSocketAsyncEventArgs.UserToken = this.clientSocket;
                    senderSocketAsyncEventArgs.RemoteEndPoint = this.clientSocket.RemoteEndPoint;
                    if (handler == null)
                    {
                        handler = delegate(object sender, SocketAsyncEventArgs _e)
                        {
                            lock (s_lst)
                            {
                                s_lst.Add(senderSocketAsyncEventArgs);
                            }
                        };
                    }
                    senderSocketAsyncEventArgs.Completed += handler;
                }
                senderSocketAsyncEventArgs.SetBuffer(buffer, 0, buffer.Length);
                this.clientSocket.SendAsync(senderSocketAsyncEventArgs);
            }
            else
            {
                this.connected = false;
            }
        }
        /// 
        /// 监听服务端
        /// 
        public void Listen()
        {
            if (this.connected && this.clientSocket != null)
            {
                try
                {
                    (lisnterSocketAsyncEventArgs.UserToken as Socket).ReceiveAsync(lisnterSocketAsyncEventArgs);
                }
                catch (Exception)
                {
                }
            }
        }
        /// 
        /// 断开连接
        /// 
        /// 
        private int Disconnect()
        {
            int res = 0;
            try
            {
                this.clientSocket.Shutdown(SocketShutdown.Both);
            }
            catch (Exception)
            {
            }
            try
            {
                this.clientSocket.Close();
            }
            catch (Exception)
            {
            }
            this.connected = false;
            this.Plcstatus = PlcStatus.Close;
            return res;
        }
        /// 
        /// 数据接受
        /// 
        /// 
        /// 
        private void OnReceive(object sender, SocketAsyncEventArgs e)
        {
            if (e.BytesTransferred == 0)
            {
                try
                {
                    this.clientSocket.Shutdown(SocketShutdown.Both);
                }
                catch (Exception)
                {
                }
                finally
                {
                    if (this.clientSocket.Connected)
                    {
                        this.clientSocket.Close();
                    }
                }
                byte[] info = new Byte[] { 0 };
                this.OnMsgReceived(info);
            }
            else
            {
                byte[] bf = new byte[e.BytesTransferred];
                Buffer.BlockCopy(e.Buffer, 0, bf, 0, e.BytesTransferred);
                //for (int i = 0; i < e.BytesTransferred; i++)
                //{
                //    buffer[i] = e.Buffer[i];
                //}
                this.OnMsgReceived(bf);
                Listen();
            }
        }
        /// 
        /// 接受完成
        /// 
        /// 
        private void OmrlonSocketClient_OnMsgReceived(byte[] info)
        {
            if (info[0] != 0)
            {
                if (this.SendOrRecv == 1)
                {
                    this.SendDataPro = info;
                    this.SendOrRecv = 0;
                }
                else if (this.SendOrRecv == 2)
                {
                    this.SendData = info;
                    this.SendOrRecv = 0;
                }
                else if (this.SendOrRecv == 3)
                {
                    this.RecvDataPro = info;
                    this.SendOrRecv = 0;
                }
                else if (this.SendOrRecv == 4)
                {
                    this.RecvData = info;
                    this.SendOrRecv = 0;
                }
            }
            else
            {
                if (this.SendOrRecv == 1)
                {
                    this.SendDataPro = new Byte[] { 0 };
                    this.SendOrRecv = 0;
                }
                else if (this.SendOrRecv == 2)
                {
                    this.SendData = new Byte[] { 0 };
                    this.SendOrRecv = 0;
                }
                else if (this.SendOrRecv == 3)
                {
                    this.RecvDataPro = new Byte[] { 0 };
                    this.SendOrRecv = 0;
                }
                else if (this.SendOrRecv == 4)
                {
                    this.RecvData = new Byte[] { 0 };
                    this.SendOrRecv = 0;
                }
            }
        }
        /// 
        /// 建立连接的方法
        /// 
        /// 
        public bool OpenLinkPLC()
        {
            bool flag = false;
            this.StartListen += new StartListeHandler(OmrlonSocketClient_StartListen);
            this.OnMsgReceived += new ReceiveMsgHandler(OmrlonSocketClient_OnMsgReceived);
            flag = this.Connect();
            if (!flag)
            {
                return flag;
            }
            Plcstatus = PlcStatus.Run;
            return true;
        }

        /// 
        /// 关闭连接的方法
        /// 
        /// 
        public int CloseLinkPLC()
        {
            this.Plcstatus = PlcStatus.Close;
            return this.Disconnect();
        }
        /// 
        /// 监听的方法
        /// 
        private void OmrlonSocketClient_StartListen()
        {
            this.Listen();
        }

        /// 
        /// 校验
        /// 
        /// 
        /// 
        private string FCS(string Value)
        {
            int num2 = 0;
            for (int i = 0; i < Value.Length; i++)
            {
                byte[] bytes = Encoding.ASCII.GetBytes(Value.Substring(i, 1));
                num2 ^= bytes[0];
            }
            return num2.ToString("X");
        }



        /// 
        /// 写入PLC寄存器值(位)
        /// 
        /// 寄存器名称
        /// 数量
        /// 写入数组
        /// 
        public bool ax_WriteBit(string ItemName, int WriteCount, ref int[] ItemData)
        {
            bool ret = false;
            if (!string.IsNullOrEmpty(ItemName))
            {
                string Get_Type = ItemName.Substring(0, 1);
                //int Get_Name = int.Parse(ItemName.Substring(1, ItemName.Length - 1));
                string Get_Name = ItemName.Substring(1, ItemName.Length - 1);
                if (Get_Type == "m" | Get_Type == "M" | Get_Type == "b" | Get_Type == "B" | Get_Type == "x" | Get_Type == "X" | Get_Type == "Y" | Get_Type == "y")
                {
                    string TxCommand;
                    string CpuTime = "0010";//cpu监视定时器
                    string GetOrder = Get_Ord_String(4);
                    string SetData = "";
                    for (int j = 0; j < WriteCount; j++)
                    {
                        SetData = SetData + ItemData[j];
                    }
                    string Sendf = string.Format(CpuTime + GetOrder + Get_Type.ToUpper() + "*{0}{1}{2}", Get_Name.PadLeft(6, '0'), WriteCount.ToString("X4"), SetData);
                    TxCommand = "500000FF03FF00" + Sendf.Length.ToString("X4") + Sendf;
                    Byte[] sendBuffer = Encoding.Default.GetBytes(TxCommand.ToCharArray());
                    this.SendOrRecv = 1;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                        string Getstring = Encoding.Default.GetString(SendDataPro).Trim('\0');
                        try
                        {
                            string SubHeader = Getstring.Substring(0, 4);
                            string EndCord = Getstring.Substring(18, 4);
                            if (SubHeader == "D000" && EndCord == "0000")//正常
                            {
                                ret = true;
                            }
                        }
                        catch (Exception)
                        {
                           
                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }
            }
            return ret;
        }

        /// 
        /// 写入PLC寄存器值(字)
        /// 
        /// 寄存器名称
        /// 数量
        /// 写入数组
        /// 
        public bool ax_WriteWord(string ItemName, int WriteCount, ref int[] ItemData)
        {
            bool ret = false;
            if (!string.IsNullOrEmpty(ItemName))
            {
                string Get_Type = ItemName.Substring(0, 1);
                //int Get_Name = int.Parse(ItemName.Substring(1, ItemName.Length - 1));
                string Get_Name = ItemName.Substring(1, ItemName.Length - 1);
                if (Get_Type == "d" | Get_Type == "D" | Get_Type == "w" | Get_Type == "W")
                {
                    string TxCommand;
                    string CpuTime = "0010";//cpu监视定时器
                    string GetOrder = Get_Ord_String(2);
                    string SetData = "";
                    for (int j = 0; j < WriteCount; j++)
                    {
                        SetData = SetData + ItemData[j].ToString("X4");
                    }
                    string Sendf = string.Format(CpuTime + GetOrder + Get_Type.ToUpper() + "*{0}{1}{2}", Get_Name.PadLeft(6, '0'), WriteCount.ToString("X4"), SetData);
                    TxCommand = "500000FF03FF00" + Sendf.Length.ToString("X4") + Sendf;
                    Byte[] sendBuffer = Encoding.Default.GetBytes(TxCommand.ToCharArray());
                    this.SendOrRecv = 1;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                        string Getstring = Encoding.Default.GetString(SendDataPro).Trim('\0');
                        try
                        {
                            string SubHeader = Getstring.Substring(0, 4);
                            string EndCord = Getstring.Substring(18, 4);
                            if (SubHeader == "D000" && EndCord == "0000")//正常
                            {
                                ret = true;
                            }
                        }
                        catch (Exception)
                        {
                            
                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }
            }
            return ret;
        }

        /// 
        /// 读Word
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool ax_ReadWord(string ItemName, int ReadCount, out int[] ItemData)
        {
            bool ret = false;
            ItemData = new int[ReadCount];
            byte[] recvData = new byte[1024];
            if (!string.IsNullOrEmpty(ItemName))
            {
                string Get_Type = ItemName.Substring(0, 1);
                //int Get_Name = int.Parse(ItemName.Substring(1, ItemName.Length - 1));
                string Get_Name = ItemName.Substring(1, ItemName.Length - 1);
                if (Get_Type == "d" | Get_Type == "D" | Get_Type == "w" | Get_Type == "W" | Get_Type == "b" | Get_Type == "B")
                {
                    string TxCommand;
                    string CpuTime = "0010";//cpu监视定时器
                    string GetOrder = Get_Ord_String(1);
                    string Sendf = string.Format(CpuTime + GetOrder + Get_Type.ToUpper() + "*{0}{1}", Get_Name.PadLeft(6, '0'), ReadCount.ToString("X4"));
                    TxCommand = "500000FF03FF00" + Sendf.Length.ToString("X4") + Sendf;
                    Byte[] sendBuffer = Encoding.Default.GetBytes(TxCommand.ToCharArray());
                    this.SendOrRecv = 3;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                        string Getstring = Encoding.Default.GetString(RecvDataPro).Trim('\0');
                        if (Getstring.Length == 22 + ReadCount * 4)
                        {
                            string SubHeader = Getstring.Substring(0, 4);
                            string EndCord = Getstring.Substring(18, 4);
                            if (SubHeader == "D000" && EndCord == "0000")//正常
                            {
                                for (int i = 0; i < ReadCount; i++)
                                {
                                    //D00000FF03FF000060 0000   0000 0000 0000 0000 0000 
                                    //                          0000 0000 0000 0000 0000 
                                    //0000 0000 0000 0000 0000 
                                    //0000 007B 0000 0000 0000 
                                    //0000 0000 0000
                                    ItemData[i] = Convert.ToInt32(Getstring.Substring(22 + (i * 4), 4), 16);
                                }
                                ret = true;
                            }
                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }
            }
            return ret;
        }

        /// 
        /// 读位
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool ax_ReadBit(string ItemName, int ReadCount, out int[] ItemData)
        {
            bool ret = false;
            ItemData = new int[ReadCount];
            byte[] recvData = new byte[1024];
            if (!string.IsNullOrEmpty(ItemName))
            {
                string Get_Type = ItemName.Substring(0, 1);
                //int Get_Name = int.Parse(ItemName.Substring(1, ItemName.Length - 1));
                string Get_Name = ItemName.Substring(1, ItemName.Length - 1);
                if (Get_Type == "m" | Get_Type == "M" | Get_Type == "b" | Get_Type == "B" | Get_Type == "x" | Get_Type == "X" | Get_Type == "Y" | Get_Type == "y")
                {
                    string TxCommand;
                    string CpuTime = "0010";//cpu监视定时器
                    string GetOrder = Get_Ord_String(3);
                    if (Get_Name == "1200")
                    {
                        //500000FF03FF000018001004010001B*0012000030
                    }
                    string Sendf = string.Format(CpuTime + GetOrder + Get_Type.ToUpper() + "*{0}{1}", Get_Name.PadLeft(6, '0'), ReadCount.ToString("X4"));
                    TxCommand = "500000FF03FF00" + Sendf.Length.ToString("X4") + Sendf;
                    Byte[] sendBuffer = Encoding.Default.GetBytes(TxCommand.ToCharArray());
                    this.SendOrRecv = 3;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                        string Getstring = Encoding.Default.GetString(RecvDataPro).Trim('\0');
                        if (Getstring.Length == 22 + ReadCount)
                        {
                            string SubHeader = Getstring.Substring(0, 4);
                            string EndCord = Getstring.Substring(18, 4);
                            if (SubHeader == "D000" && EndCord == "0000")//正常
                            {
                                for (int i = 0; i < ReadCount; i++)
                                {
                                    ItemData[i] = int.Parse(Getstring.Substring(22 + i, 1));
                                    if (ItemData[i] == 1)
                                    {

                                    }
                                }
                                ret = true;
                            }
                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }
            }
            return ret;
        }

        /// 
        /// 命令+子命令
        /// 
        /// 1,读取word;2,写入word;3,读取bit;4,写入bit
        /// 
        private string Get_Ord_String(int type)
        {
            string Rt = "";
            switch (type)
            {
                case 1://读取word
                    Rt = "04010000";
                    break;
                case 2://写入word
                    Rt = "14010000";
                    break;
                case 3://读取bit
                    Rt = "04010001";
                    break;
                case 4://写入bit
                    Rt = "14010001";
                    break;
                default:
                    break;
            }
            return Rt;
        }


        //50 00 副标头
        //00 ff 
        //ff 03 
        //00
        //0c 00 
        //01 00 
        //01 14 
        //00 00
        //A6 27 00
        //A8
        //01 00
        //01 02
        /// 
        /// 写入PLC寄存器值(位)
        /// 
        /// 寄存器名称
        /// 数量
        /// 写入数组
        /// 
        public bool ax_WriteBit_hex(string ItemName, int WriteCount, ref int[] ItemData)
        {
            bool ret = false;
            if (!string.IsNullOrEmpty(ItemName))
            {
                string Get_Type = ItemName.Substring(0, 1);
                string Get_Name = ItemName.Substring(1, ItemName.Length - 1).PadLeft(6, '0');
                if (Get_Type == "m" | Get_Type == "M")
                {
                    byte[] SendTag = new byte[2] { 0x50, 0x00 };
                    byte[] SendNetNo = new byte[1] { 0x00 };
                    byte[] SendPLCNo = new byte[1] { 0xff };
                    byte[] SendIONo = new byte[2] { 0xff, 0x03 };
                    byte[] SendSTNo = new byte[1] { 0x00 };
                    byte[] SendDataLenth = new byte[2] { (byte)Convert.ToByte((WriteCount / 2 + WriteCount % 2 + 12).ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte((WriteCount / 2 + WriteCount % 2 + 12).ToString("X4").Substring(0, 2), 16) };
                    byte[] Sendtime = new byte[2] { 0x10, 0x00 };
                    byte[] SendOrdF = new byte[4] { 0x01, 0x14, 0x01, 0x00 };
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0x90 };//WriteCount.ToString("X4")
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(WriteCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(WriteCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] SendData = new byte[WriteCount / 2 + WriteCount % 2];
                    for (int i = 0; i < WriteCount; i++)
                    {
                        if (i % 2 == 0)
                        {
                            SendData[i / 2] = (byte)(16 * ItemData[i]);
                        }
                        else
                        {
                            SendData[i / 2] = (byte)((int)SendData[i / 2] + ItemData[i]);
                        }
                    }
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length + SendData.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    SendData.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length + SendWordLenth.Length);
                    this.SendOrRecv = 1;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                        try
                        {
                            if (SendDataPro[0] == 0xd0 && SendDataPro[1] == 0x00 && SendDataPro[9] == 0x00 && SendDataPro[10] == 0x00)//正常
                            {
                                ret = true;
                            }
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }
                else if (Get_Type == "b" | Get_Type == "B")
                {
                    byte[] SendTag = new byte[2] { 0x50, 0x00 };
                    byte[] SendNetNo = new byte[1] { 0x00 };
                    byte[] SendPLCNo = new byte[1] { 0xff };
                    byte[] SendIONo = new byte[2] { 0xff, 0x03 };
                    byte[] SendSTNo = new byte[1] { 0x00 };
                    byte[] SendDataLenth = new byte[2] { (byte)Convert.ToByte((WriteCount / 2 + WriteCount % 2 + 12).ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte((WriteCount / 2 + WriteCount % 2 + 12).ToString("X4").Substring(0, 2), 16) };
                    byte[] Sendtime = new byte[2] { 0x10, 0x00 };
                    byte[] SendOrdF = new byte[4] { 0x01, 0x14, 0x01, 0x00 };
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0xa0 };//WriteCount.ToString("X4")
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(WriteCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(WriteCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] SendData = new byte[WriteCount / 2 + WriteCount % 2];
                    for (int i = 0; i < WriteCount; i++)
                    {
                        if (i % 2 == 0)
                        {
                            SendData[i / 2] = (byte)(16 * ItemData[i]);
                        }
                        else
                        {
                            SendData[i / 2] = (byte)((int)SendData[i / 2] + ItemData[i]);
                        }
                    }
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length + SendData.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    SendData.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length + SendWordLenth.Length);
                    this.SendOrRecv = 1;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                        try
                        {
                            if (SendDataPro[0] == 0xd0 && SendDataPro[1] == 0x00 && SendDataPro[9] == 0x00 && SendDataPro[10] == 0x00)//正常
                            {
                                ret = true;
                            }
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }
                else if (Get_Type == "x" | Get_Type == "X")
                {
                    byte[] SendTag = new byte[2] { 0x50, 0x00 };
                    byte[] SendNetNo = new byte[1] { 0x00 };
                    byte[] SendPLCNo = new byte[1] { 0xff };
                    byte[] SendIONo = new byte[2] { 0xff, 0x03 };
                    byte[] SendSTNo = new byte[1] { 0x00 };
                    byte[] SendDataLenth = new byte[2] { (byte)Convert.ToByte((WriteCount / 2 + WriteCount % 2 + 12).ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte((WriteCount / 2 + WriteCount % 2 + 12).ToString("X4").Substring(0, 2), 16) };
                    byte[] Sendtime = new byte[2] { 0x10, 0x00 };
                    byte[] SendOrdF = new byte[4] { 0x01, 0x14, 0x01, 0x00 };
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0x9c };//WriteCount.ToString("X4")
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(WriteCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(WriteCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] SendData = new byte[WriteCount / 2 + WriteCount % 2];
                    for (int i = 0; i < WriteCount; i++)
                    {
                        if (i % 2 == 0)
                        {
                            SendData[i / 2] = (byte)(16 * ItemData[i]);
                        }
                        else
                        {
                            SendData[i / 2] = (byte)((int)SendData[i / 2] + ItemData[i]);
                        }
                    }
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length + SendData.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    SendData.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length + SendWordLenth.Length);
                    this.SendOrRecv = 1;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                        try
                        {
                            if (SendDataPro[0] == 0xd0 && SendDataPro[1] == 0x00 && SendDataPro[9] == 0x00 && SendDataPro[10] == 0x00)//正常
                            {
                                ret = true;
                            }
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }
                else if (Get_Type == "Y" | Get_Type == "y")
                {
                    byte[] SendTag = new byte[2] { 0x50, 0x00 };
                    byte[] SendNetNo = new byte[1] { 0x00 };
                    byte[] SendPLCNo = new byte[1] { 0xff };
                    byte[] SendIONo = new byte[2] { 0xff, 0x03 };
                    byte[] SendSTNo = new byte[1] { 0x00 };
                    byte[] SendDataLenth = new byte[2] { (byte)Convert.ToByte((WriteCount / 2 + WriteCount % 2 + 12).ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte((WriteCount / 2 + WriteCount % 2 + 12).ToString("X4").Substring(0, 2), 16) };
                    byte[] Sendtime = new byte[2] { 0x10, 0x00 };
                    byte[] SendOrdF = new byte[4] { 0x01, 0x14, 0x01, 0x00 };
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0x9d };//WriteCount.ToString("X4")
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(WriteCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(WriteCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] SendData = new byte[WriteCount / 2 + WriteCount % 2];
                    for (int i = 0; i < WriteCount; i++)
                    {
                        if (i % 2 == 0)
                        {
                            SendData[i / 2] = (byte)(16 * ItemData[i]);
                        }
                        else
                        {
                            SendData[i / 2] = (byte)((int)SendData[i / 2] + ItemData[i]);
                        }
                    }
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length + SendData.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    SendData.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length + SendWordLenth.Length);
                    this.SendOrRecv = 1;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                        try
                        {
                            if (SendDataPro[0] == 0xd0 && SendDataPro[1] == 0x00 && SendDataPro[9] == 0x00 && SendDataPro[10] == 0x00)//正常
                            {
                                ret = true;
                            }
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }
            }
            return ret;
        }

        /// 
        /// 写入PLC寄存器值(字)
        /// 
        /// 寄存器名称
        /// 数量
        /// 写入数组
        /// 
        public bool ax_WriteWord_hex(string ItemName, int WriteCount, ref int[] ItemData)
        {
            bool ret = false;
            if (!string.IsNullOrEmpty(ItemName))
            {
                string Get_Type = ItemName.Substring(0, 1);
                string Get_Name = ItemName.Substring(1, ItemName.Length - 1).PadLeft(6, '0');
                if (Get_Type == "d" | Get_Type == "D")
                {
                    byte[] SendTag = new byte[2] { 0x50, 0x00 };
                    byte[] SendNetNo = new byte[1] { 0x00 };
                    byte[] SendPLCNo = new byte[1] { 0xff };
                    byte[] SendIONo = new byte[2] { 0xff, 0x03 };
                    byte[] SendSTNo = new byte[1] { 0x00 };
                    byte[] SendDataLenth = new byte[2] { (byte)Convert.ToByte((WriteCount * 2 + 12).ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte((WriteCount * 2 + 12).ToString("X4").Substring(0, 2), 16) };
                    byte[] Sendtime = new byte[2] { 0x01, 0x00 };
                    byte[] SendOrdF = new byte[4] { 0x01, 0x14, 0x00, 0x00 };
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0xa8 };//WriteCount.ToString("X4")
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(WriteCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(WriteCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] SendData = new byte[2 * WriteCount];
                    for (int i = 0; i < WriteCount; i++)
                    {
                        SendData[i * 2] = (byte)Convert.ToByte(ItemData[i].ToString("X4").Substring(2, 2), 16);
                        SendData[i * 2 + 1] = (byte)Convert.ToByte(ItemData[i].ToString("X4").Substring(0, 2), 16);
                    }
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length + SendData.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    SendData.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length + SendWordLenth.Length);
                    this.SendOrRecv = 1;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                        try
                        {
                            if (SendDataPro[0] == 0xd0 && SendDataPro[1] == 0x00 && SendDataPro[9] == 0x00 && SendDataPro[10] == 0x00)//正常
                            {
                                ret = true;
                            }
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }
                else if (Get_Type == "w" | Get_Type == "W")
                {
                    byte[] SendTag = new byte[2] { 0x50, 0x00 };
                    byte[] SendNetNo = new byte[1] { 0x00 };
                    byte[] SendPLCNo = new byte[1] { 0xff };
                    byte[] SendIONo = new byte[2] { 0xff, 0x03 };
                    byte[] SendSTNo = new byte[1] { 0x00 };
                    byte[] SendDataLenth = new byte[2] { (byte)Convert.ToByte((WriteCount * 2 + 12).ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte((WriteCount * 2 + 12).ToString("X4").Substring(0, 2), 16) };
                    byte[] Sendtime = new byte[2] { 0x01, 0x00 };
                    byte[] SendOrdF = new byte[4] { 0x01, 0x14, 0x00, 0x00 };
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0xb4 };//WriteCount.ToString("X4")
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(WriteCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(WriteCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] Senddata = new byte[2 * WriteCount];
                    for (int i = 0; i < WriteCount; i++)
                    {
                        Senddata[i * 2] = (byte)Convert.ToByte(ItemData[i].ToString("X4").Substring(2, 2), 16);
                        Senddata[i * 2 + 1] = (byte)Convert.ToByte(ItemData[i].ToString("X4").Substring(0, 2), 16);
                    }
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length + Senddata.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    Senddata.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length + SendWordLenth.Length);
                    this.SendOrRecv = 1;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                        try
                        {
                            if (SendDataPro[0] == 0xd0 && SendDataPro[1] == 0x00 && SendDataPro[9] == 0x00 && SendDataPro[10] == 0x00)//正常
                            {
                                ret = true;
                            }
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }

            }
            return ret;
        }

        /// 
        /// 读Word
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool ax_ReadWord_hex(string ItemName, int ReadCount, out int[] ItemData)
        {
            bool ret = false;
            ItemData = new int[ReadCount];
            byte[] recvData = new byte[1024];
            if (!string.IsNullOrEmpty(ItemName))
            {
                string Get_Type = ItemName.Substring(0, 1);
                string Get_Name = ItemName.Substring(1, ItemName.Length - 1).PadLeft(6, '0');
                if (Get_Type == "d" | Get_Type == "D")
                {
                    byte[] SendTag = new byte[2] { 0x50, 0x00 };
                    byte[] SendNetNo = new byte[1] { 0x00 };
                    byte[] SendPLCNo = new byte[1] { 0xff };
                    byte[] SendIONo = new byte[2] { 0xff, 0x03 };
                    byte[] SendSTNo = new byte[1] { 0x00 };
                    byte[] SendDataLenth = new byte[2] { 0x0c, 0x00 };
                    byte[] Sendtime = new byte[2] { 0x01, 0x00 };
                    byte[] SendOrdF = new byte[4] { 0x01, 0x04, 0x00, 0x00 };
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0xa8 };
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    this.SendOrRecv = 3;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                         try
                        {
                            if (RecvDataPro[0] == 0xd0 && RecvDataPro[1] == 0x00 && RecvDataPro[9] == 0x00 && RecvDataPro[10] == 0x00)//正常
                            {
                                for (int i = 0; i < ReadCount; i++)
                                {
                                    ItemData[i] = ((int)RecvDataPro[12 + i * 2]) * 256 + ((int)RecvDataPro[11 + i * 2]);
                                }
                                ret = true;
                            }
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }
                else if (Get_Type == "w" | Get_Type == "W")
                {
                    byte[] SendTag = new byte[2] { 0x50, 0x00 };
                    byte[] SendNetNo = new byte[1] { 0x00 };
                    byte[] SendPLCNo = new byte[1] { 0xff };
                    byte[] SendIONo = new byte[2] { 0xff, 0x03 };
                    byte[] SendSTNo = new byte[1] { 0x00 };
                    byte[] SendDataLenth = new byte[2] { 0x0c, 0x00 };
                    byte[] Sendtime = new byte[2] { 0x01, 0x00 };
                    byte[] SendOrdF = new byte[4] { 0x01, 0x04, 0x00, 0x00 };
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0xb4 };
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    this.SendOrRecv = 3;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                        try
                        {
                            if (RecvDataPro[0] == 0xd0 && RecvDataPro[1] == 0x00 && RecvDataPro[9] == 0x00 && RecvDataPro[10] == 0x00)//正常
                            {
                                for (int i = 0; i < ReadCount; i++)
                                {
                                    ItemData[i] = ((int)RecvDataPro[12 + i * 2]) * 256 + ((int)RecvDataPro[11 + i * 2]);
                                }
                                ret = true;
                            }
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }
                else if (Get_Type == "b" | Get_Type == "B")
                {
                    byte[] SendTag = new byte[2] { 0x50, 0x00 };
                    byte[] SendNetNo = new byte[1] { 0x00 };
                    byte[] SendPLCNo = new byte[1] { 0xff };
                    byte[] SendIONo = new byte[2] { 0xff, 0x03 };
                    byte[] SendSTNo = new byte[1] { 0x00 };
                    byte[] SendDataLenth = new byte[2] { 0x0c, 0x00 };
                    byte[] Sendtime = new byte[2] { 0x01, 0x00 };
                    byte[] SendOrdF = new byte[4] { 0x01, 0x04, 0x00, 0x00 };
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0xa0 };
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    this.SendOrRecv = 3;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                        try
                        {
                            if (RecvDataPro[0] == 0xd0 && RecvDataPro[1] == 0x00 && RecvDataPro[9] == 0x00 && RecvDataPro[10] == 0x00)//正常
                            {
                                for (int i = 0; i < ReadCount; i++)
                                {
                                    ItemData[i] = ((int)RecvDataPro[12 + i * 2]) * 256 + ((int)RecvDataPro[11 + i * 2]);
                                }
                                ret = true;
                            }
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }
            }
            return ret;
        }

        /// 
        /// 读位
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool ax_ReadBit_hex(string ItemName, int ReadCount, out int[] ItemData)
        {
            bool ret = false;
            ItemData = new int[ReadCount];
            byte[] recvData = new byte[1024];
            if (!string.IsNullOrEmpty(ItemName))
            {
                string Get_Type = ItemName.Substring(0, 1);
                string Get_Name = ItemName.Substring(1, ItemName.Length - 1).PadLeft(6, '0');
                if (Get_Type == "m" | Get_Type == "M")
                {
                    byte[] SendTag = new byte[2] { 0x50, 0x00 };
                    byte[] SendNetNo = new byte[1] { 0x00 };
                    byte[] SendPLCNo = new byte[1] { 0xff };
                    byte[] SendIONo = new byte[2] { 0xff, 0x03 };
                    byte[] SendSTNo = new byte[1] { 0x00 };
                    byte[] SendDataLenth = new byte[2] { 0x0c, 0x00 };
                    byte[] Sendtime = new byte[2] { 0x10, 0x00 };//0x01, 0x00
                    byte[] SendOrdF = new byte[4] { 0x01, 0x04, 0x01, 0x00 };
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0x90 };
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);               
                    this.SendOrRecv = 3;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                        try
                        {
                            if (RecvDataPro[0] == 0xd0 && RecvDataPro[1] == 0x00 && RecvDataPro[9] == 0x00 && RecvDataPro[10] == 0x00)//正常
                            {
                                for (int i = 0; i < ReadCount; i++)//38
                                {
                                    if (i % 2 == 0)
                                    {
                                        ItemData[i] = int.Parse(((int)RecvDataPro[11 + i / 2]).ToString("X2").Substring(0, 1));
                                    }
                                    else
                                    {
                                        ItemData[i] = int.Parse(((int)RecvDataPro[11 + i / 2]).ToString("X2").Substring(1, 1));
                                    }
                                }
                                ret = true;
                            }
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }
                else if (Get_Type == "b" | Get_Type == "B")
                {
                    byte[] SendTag = new byte[2] { 0x50, 0x00 };
                    byte[] SendNetNo = new byte[1] { 0x00 };
                    byte[] SendPLCNo = new byte[1] { 0xff };
                    byte[] SendIONo = new byte[2] { 0xff, 0x03 };
                    byte[] SendSTNo = new byte[1] { 0x00 };
                    byte[] SendDataLenth = new byte[2] { 0x0c, 0x00 };
                    byte[] Sendtime = new byte[2] { 0x10, 0x00 };//0x01, 0x00
                    byte[] SendOrdF = new byte[4] { 0x01, 0x04, 0x01, 0x00 };
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0xa0 };
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    this.SendOrRecv = 3;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                        try
                        {
                            if (RecvDataPro[0] == 0xd0 && RecvDataPro[1] == 0x00 && RecvDataPro[9] == 0x00 && RecvDataPro[10] == 0x00)//正常
                            {
                                for (int i = 0; i < ReadCount; i++)//38
                                {
                                    if (i % 2 == 0)
                                    {
                                        ItemData[i] = int.Parse(((int)RecvDataPro[11 + i / 2]).ToString("X2").Substring(0, 1));
                                    }
                                    else
                                    {
                                        ItemData[i] = int.Parse(((int)RecvDataPro[11 + i/ 2]).ToString("X2").Substring(1, 1));
                                    }
                                }
                                ret = true;
                            }
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }
                else if (Get_Type == "x" | Get_Type == "X")
                {
                    byte[] SendTag = new byte[2] { 0x50, 0x00 };
                    byte[] SendNetNo = new byte[1] { 0x00 };
                    byte[] SendPLCNo = new byte[1] { 0xff };
                    byte[] SendIONo = new byte[2] { 0xff, 0x03 };
                    byte[] SendSTNo = new byte[1] { 0x00 };
                    byte[] SendDataLenth = new byte[2] { 0x0c, 0x00 };
                    byte[] Sendtime = new byte[2] { 0x10, 0x00 };//0x01, 0x00
                    byte[] SendOrdF = new byte[4] { 0x01, 0x04, 0x01, 0x00 };
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0x9c };
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    this.SendOrRecv = 3;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                        try
                        {
                            if (RecvDataPro[0] == 0xd0 && RecvDataPro[1] == 0x00 && RecvDataPro[9] == 0x00 && RecvDataPro[10] == 0x00)//正常
                            {
                                for (int i = 0; i < ReadCount; i++)//38
                                {
                                    if (i % 2 == 0)
                                    {
                                        ItemData[i] = int.Parse(((int)RecvDataPro[11 + i / 2]).ToString("X2").Substring(0, 1));
                                    }
                                    else
                                    {
                                        ItemData[i] = int.Parse(((int)RecvDataPro[11 + i / 2]).ToString("X2").Substring(1, 1));
                                    }
                                }
                                ret = true;
                            }
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }
                else if (Get_Type == "Y" | Get_Type == "y")
                {
                    byte[] SendTag = new byte[2] { 0x50, 0x00 };
                    byte[] SendNetNo = new byte[1] { 0x00 };
                    byte[] SendPLCNo = new byte[1] { 0xff };
                    byte[] SendIONo = new byte[2] { 0xff, 0x03 };
                    byte[] SendSTNo = new byte[1] { 0x00 };
                    byte[] SendDataLenth = new byte[2] { 0x0c, 0x00 };
                    byte[] Sendtime = new byte[2] { 0x10, 0x00 };//0x01, 0x00
                    byte[] SendOrdF = new byte[4] { 0x01, 0x04, 0x01, 0x00 };
                    byte[] SendWordName = new byte[3] { (byte)Convert.ToByte(Get_Name.Substring(4, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(2, 2), 16), (byte)Convert.ToByte(Get_Name.Substring(0, 2), 16) };
                    byte[] SendWordValue = new byte[1] { 0x9d };
                    byte[] SendWordLenth = new byte[2] { (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(2, 2), 16), (byte)Convert.ToByte(ReadCount.ToString("X4").Substring(0, 2), 16) };
                    byte[] sendBuffer = new byte[SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length
                                               + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length
                                               + SendWordLenth.Length];
                    SendTag.CopyTo(sendBuffer, 0);
                    SendNetNo.CopyTo(sendBuffer, SendTag.Length);
                    SendPLCNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length);
                    SendIONo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length);
                    SendSTNo.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length);
                    SendDataLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length);
                    Sendtime.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length);
                    SendOrdF.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length);
                    SendWordName.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length);
                    SendWordValue.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length);
                    SendWordLenth.CopyTo(sendBuffer, SendTag.Length + SendNetNo.Length + SendPLCNo.Length + SendIONo.Length + SendSTNo.Length + SendDataLenth.Length + Sendtime.Length + SendOrdF.Length + SendWordName.Length + SendWordValue.Length);
                    this.SendOrRecv = 3;
                    int numPro = 0;
                    this.Send(sendBuffer);
                    while (this.SendOrRecv != 0 && numPro < 100)
                    {
                        Thread.Sleep(5);
                        numPro++;
                    }
                    if (numPro < 100)
                    {
                        try
                        {
                            if (RecvDataPro[0] == 0xd0 && RecvDataPro[1] == 0x00 && RecvDataPro[9] == 0x00 && RecvDataPro[10] == 0x00)//正常
                            {
                                for (int i = 0; i < ReadCount; i++)//38
                                {
                                    if (i % 2 == 0)
                                    {
                                        ItemData[i] = int.Parse(((int)RecvDataPro[11 + i / 2]).ToString("X2").Substring(0, 1));
                                    }
                                    else
                                    {
                                        ItemData[i] = int.Parse(((int)RecvDataPro[11 + i / 2]).ToString("X2").Substring(1, 1));
                                    }
                                }
                                ret = true;
                            }
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        this.SendOrRecv = 0;
                    }
                }
            }
            return ret;
        }

        #region IDispose member
        public void Dispose()
        {
            if (this.clientSocket.Connected)
            {
                this.clientSocket.Close();
            }
        }
        #endregion    
    }

最后,具体的逻辑就是将上述的报文协议进行自己的理解,然后结合上一篇Socket客户端的代码,只需要在接收到服务端的回传数据事件中进行数据解析,当然如果还不知道怎么用的或者哪里还有疑惑的可以留言或者私信。

你可能感兴趣的:(C#三菱PLC通讯,C#Socket客户端,c#,socket,以太网,asp.net)