Exemplo n.º 1
0
        public void BuildPacket(byte[] bytes)
        {
            try
            {
                //_packet[0] = Convert.ToByte(iterator);
                //_packetList.Add(_packet);
                //Logic.updateConsoleBox(string.Format("wyslałem: {0}", iterator.ToString()));
                //iterator++;
                for (int i = 0; i < bytes.Length; i++)
                {
                    if (!headerFound && bytes[i] == _STX)
                    {
                        ClearPacket();
                        _packet[packetSize.Value] = bytes[i];
                        packetSize++;
                        headerFound = true;
                    }
                    else if (headerFound && bytes[i] == _DLE)
                    {
                        bDLE = true;
                    }
                    else if (headerFound)
                    {
                        if (bDLE)
                        {
                            _packet[packetSize.Value] = bytes[i];
                            bDLE = false;
                        }
                        else if (bytes[i] == _ETX)
                        {
                            if (packetSize.Value == packetExpectedSize - 1 || Math.Abs(packetSize.Value - packetExpectedSize) < packetExpectedSize * difference / 100)
                            {
                                _packet[packetSize.Value] = bytes[i];
                                System.IO.File.WriteAllText(string.Format("{0}/binary/correct/{1}.txt", MainSettings.Default.PatientFolderPath, packetCorrect), System.Text.Encoding.Default.GetString(_packet));
                                _packetList.Add(_packet);
                                ClearPacket();
                                packetCorrect++;
                                packetsCollected++;
                                headerFound = false;
                            }
                            else
                            {
                                headerFound = false;
                                System.IO.File.WriteAllText(string.Format("{0}/binary/{1}.txt", MainSettings.Default.PatientFolderPath, packetsCollected), System.Text.Encoding.Default.GetString(_packet));
                                ClearPacket();
                                packetsCollected++;
                            }
                        }
                        else if (packetSize.Value >= 6407)
                        {
                            ClearPacket();
                        }
                        else
                        {
                            _packet[packetSize.Value] = bytes[i];
                        }
                        packetSize++;
                    }
                }
                bytes = null;
            }

            catch (Exception ex)
            {
                Logic.updateConsoleBox(string.Format("Receiver: {0}", ex.Message));
            }
        }