Ejemplo n.º 1
0
        void Recv_sock_OnReceivedBytes(CSerialDriver driver, byte[] bytes)
        {
            // 드라이버 이름이 필요하면  driver.Name 확인

            lock (_keylock)
            {
                if (EqpId.Length != 4)
                {
                    return;
                }

                try
                {
                    if (_bytelist == null)
                    {
                        _bytelist = new List <byte>();
                    }

                    _bytelist.AddRange(bytes);

                    bool retry = false;

                    do
                    {
                        retry = false;

                        int startPos = CUtil.PatternAt(_bytelist.ToArray(), _byteModule) - 1;

                        if (startPos > 0)
                        {
                            _sbPacket = new List <byte>();

                            for (int i = startPos; i < _bytelist.Count; i++)
                            {
                                _sbPacket.Add(_bytelist[i]);

                                if (_sbPacket.Count == CommonHeader.GetHeaderSize())
                                {
                                    // parsing header. & 앞으로 받을 남은 길이 확인.
                                    commonHeader = new CommonHeader();
                                    commonHeader.SetHeader(_sbPacket.ToArray());

                                    if (commonHeader.DataLength > 1000)
                                    {
                                        OnUpdateText?.Invoke("Driver Error : Packet Length Error");

                                        _bytelist.RemoveRange(0, i);
                                        retry = true;
                                        break;
                                    }
                                }
                                else if (_sbPacket.Count > CommonHeader.GetHeaderSize())
                                {
                                    //_sbPacket 의 카운터가 header 보다 크면 header 생성이 되었다고 보고.
                                    //총 길이가 header size + Data 길이 etx 만큼 계속 받음.
                                    if (_sbPacket.Count == commonHeader.DataLength + CommonHeader.GetHeaderSize() + 1)
                                    {
                                        int  index = _sbPacket.Count;
                                        byte check = _sbPacket[index - 1];
                                        if (check != (byte)CConstant._etx)
                                        {
                                            OnUpdateText?.Invoke("Error Packet: not exist ETX");
                                            OnUpdateMakeLogText?.Invoke(2, bytes);
                                        }
                                        else
                                        {
                                            byte[] dataPacket = new byte[commonHeader.DataLength];
                                            Array.Copy(_sbPacket.ToArray(), CommonHeader.GetHeaderSize(), dataPacket, 0, commonHeader.DataLength);
                                            List <short> listdata = CUtil.ByteToShortList(dataPacket);

                                            OnUpdateMakeLogText?.Invoke(1, _sbPacket.ToArray());

                                            if (OnRecievePacket != null)
                                            {
                                                try
                                                {
                                                    //OnRecievePacket(commonHeader, listdata);
                                                    OnRecievePacket.BeginInvoke(commonHeader, listdata, null, null);
                                                }
                                                catch (Exception ex)
                                                {
                                                    SystemLogger.Log(Level.Exception, ex.Message, "Driver");
                                                }
                                            }
                                        }
                                        _bytelist.RemoveRange(0, i);
                                        retry = true;
                                        break;
                                    }
                                }
                            }
                        }
                    } while (retry);
                }
                catch (Exception ex)
                {
                    SystemLogger.Log(Level.Exception, ex.Message, "Driver");
                }
            }
            //driver.Send("R");
        }