private void socket_ReceiveCompleted(object sender, SocketEventArgs e)
        {
            byte[] data = null;
            Guid   id   = new Guid();
            CommunicationReceiveEventArgs receiveEventArgs = null;

            switch (e.Data[0])
            {
            case 3:
                if (e.DataLength < 21)
                {
                    return;
                }
                id = new Guid(e.Data.Skip(1).Take(16).ToArray());
                int    dataLength = BitConverter.ToInt32(e.Data.Skip(17).Take(4).ToArray(), 0);
                byte[] head       = null;
                if (e.DataLength > 21)
                {
                    head = e.Data.Skip(21).ToArray();
                }
                receiveEventArgs = new CommunicationReceiveEventArgs(this, id, dataLength, head);
                PreviewReceive?.Invoke(this, receiveEventArgs);
                data    = new byte[18];
                data[0] = 4;
                int i = 1;
                do
                {
                    data[i] = e.Data[i];
                    i++;
                }while (i <= 16);
                if (!receiveEventArgs.Handled)
                {
                    receiveEventArgs.Data = new byte[dataLength];
                    ReceiveCache.Add(id, receiveEventArgs);
                    data[17] = 1;
                }
                else
                {
                    data[17] = 0;
                }
                Socket.SendAsync(data);
                break;

            case 4:
                if (e.DataLength != 18)
                {
                    return;
                }
                id = new Guid(e.Data.Skip(1).Take(16).ToArray());
                if (!SendWait.ContainsKey(id))
                {
                    return;
                }
                CommunicationSendEventArgs sendEventArgs = SendWait[id];
                SendWait.Remove(id);
                sendEventArgs.Success = e.Data[17] == 1;
                if (!sendEventArgs.Success)
                {
                    if (SendCompleted != null)
                    {
                        SendCompleted(this, sendEventArgs);
                    }
                    return;
                }
                data = GetSendBlock(sendEventArgs, 0);
                SendCache.Add(data, sendEventArgs);
                SendMark.Add(sendEventArgs, data.Length - 21);
                Socket.SendAsync(data);
                break;

            case 5:
                if (e.DataLength < 22)
                {
                    return;
                }
                id = new Guid(e.Data.Skip(1).Take(16).ToArray());
                if (!ReceiveCache.ContainsKey(id))
                {
                    return;
                }
                receiveEventArgs = ReceiveCache[id];
                int offset = BitConverter.ToInt32(e.Data.Skip(17).Take(4).ToArray(), 0);
                for (int j = 0; j < e.DataLength - 21; j++)
                {
                    receiveEventArgs.Data[offset + j] = e.Data[21 + j];
                }
                if (offset + e.DataLength - 21 == receiveEventArgs.DataLength)
                {
                    ReceiveCache.Remove(id);
                    ReceiveCompleted?.Invoke(this, receiveEventArgs);
                }
                break;
            }
        }
Exemple #2
0
        public PLCConnect(IPEndPoint ip)
        {
            IP   = ip.Address.ToString();
            Port = ip.Port;
            SocketReconnect();

            //发送数据
            new System.Threading.Thread(() =>
            {
                byte[] senddata = null;
                while (true)
                {
                    System.Threading.Thread.Sleep(100);
                    lock (SendCache)
                    {
                        if (SendCache.Count > 10)
                        {
                            SendCache.Clear();
                        }
                        if (SendCache.Count > 0)
                        {
                            senddata = SendCache[0];
                            SendCache.RemoveAt(0);
                        }
                    }

                    if (senddata == null)
                    {
                        continue;
                    }
                    if (s == null)
                    {
                        SocketReconnect();
                        System.Threading.Thread.Sleep(5000);
                    }
                    try
                    {
                        s.Send(senddata);
                        IsCommcation = true;
                    }
                    catch { SocketReconnect(); System.Threading.Thread.Sleep(5000); }
                    senddata = null;
                }
            })
            {
                IsBackground = true
            }.Start();

            //接收数据
            new System.Threading.Thread(() =>
            {
                byte[] b = new byte[1024];
                while (true)
                {
                    if (s == null)
                    {
                        continue;
                    }
                    if (s.Available == 0)
                    {
                        continue;
                    }
                    int len = 0;
                    try
                    {
                        len = s.Receive(b);
                    }
                    catch
                    {
                    }
                    if (len == 0)
                    {
                        continue;
                    }
                    byte[] recedata = b.Take(len).ToArray();
                    ReceData_Event?.Invoke(this, recedata);
                }
            })
            {
                IsBackground = true
            }.Start();
        }
Exemple #3
0
 /// <summary>
 /// 构造函数
 /// </summary>
 protected ProtocolDriver()
 {
     _Commands = new Manager <string, IProtocolCommand>();
     SendCache = new SendCache();
 }
        public IngersollRandConnecter(IPEndPoint as_ip, IPEndPoint as_bindingip = null)
        {
            IP        = as_ip.Address.ToString();
            Port      = as_ip.Port;
            BindingIp = as_bindingip;
            SocketReconnect();

            //发送心跳和问询数据
            new System.Threading.Thread(() =>
            {
#if 处理心跳
                int count_temp = 0;
#endif
                while (true)
                {
                    System.Threading.Thread.Sleep(200);
                    lock (SendCache)
                    {
                        SendCache.Add(AskTightResult);
#if 处理心跳
                        if (++count_temp >= 5)
                        {
                            SendCache.Add(HeartData);
                            count_temp = 0;
                        }
#endif
                    }
                }
            })
            {
                IsBackground = true
            }.Start();

            //发送数据
            new System.Threading.Thread(() =>
            {
                byte[] senddata = null;
                while (true)
                {
                    System.Threading.Thread.Sleep(100);
                    lock (SendCache)
                    {
                        if (SendCache.Count > 10)
                        {
                            SendCache.Clear();
                        }
                        if (SendCache.Count > 0)
                        {
                            senddata = SendCache[0];
                            SendCache.RemoveAt(0);
                        }
                    }

                    if (senddata == null)
                    {
                        continue;
                    }
                    if (s == null)
                    {
                        SocketReconnect();
                        System.Threading.Thread.Sleep(5000);
                    }
                    try
                    {
                        s.Send(senddata);
                        AppMessage.Add("发送数据", AppMessage.MsgType.英格索兰, false, AppMessage.ImportantEnum.Normal, senddata.ToStandardString(false, senddata.Length));
                        //LastCommcationTime = DateTime.Now;
                    }
                    catch { SocketReconnect(); System.Threading.Thread.Sleep(5000); }
                    senddata = null;
                }
            })
            {
                IsBackground = true
            }.Start();

            //接收数据
            new System.Threading.Thread(() =>
            {
                byte[] recedata = new byte[1024];
                while (true)
                {
                    System.Threading.Thread.Sleep(1);
                    if (s != null && s.Available > 0)
                    {
                        int rece_count_temp = s.Receive(recedata);
                        AppMessage.Add("接收数据", AppMessage.MsgType.英格索兰, false, AppMessage.ImportantEnum.Normal, recedata.ToStandardString(false, rece_count_temp));
                        if (rece_count_temp == 17)
                        {
                            bool newdata = (recedata[9] == 0 && recedata[10] == 1);
                            if (newdata)
                            {
                                bool ok       = (recedata[11] == 0 && recedata[12] == 1);
                                decimal tr    = (recedata[13] * 257 + recedata[14]) / 100m;
                                decimal angle = recedata[15] * 257 + recedata[16];
                                if (tr != 0 || angle != 0) //复位后可能数据都是0,需要排除
                                {
                                    lock (SendCache)       //发送复位指令,否则下次又要接收到相同的数据了
                                    {
                                        SendCache.Add(Reset);
                                    }
                                    OnGetData_Event(new TightResult(ok, tr, angle));
                                    //AddReceData(new TightResult(ok, tr, angle));
                                }
                            }
#if 处理心跳
#else
                            LastCommcationTime = DateTime.Now;
#endif
                        }
#if 处理心跳
                        else if (rece_count_temp == 11)//心跳数据
                        {
                            LastCommcationTime = DateTime.Now;
                        }
#endif
                    }
                }
            })
            {
                IsBackground = true
            }.Start();
        }