TryCreate() public static method

public static TryCreate ( byte packet, int length = -1 ) : Message
packet byte
length int
return Message
Exemplo n.º 1
0
        public static new Message TryCreate(byte[] buffer, int bufferLength = -1)
        {
            if (bufferLength < 0)
            {
                bufferLength = buffer.Length;
            }
            if (!IsValid(buffer))
            {
                return(Message.TryCreate(buffer, bufferLength));
            }

            return(new InternalMessage((DeviceAddress)buffer[0], buffer.SkipAndTake(3, ParseDataLength(buffer, bufferLength))));
        }
Exemplo n.º 2
0
        public static new Message TryCreate(byte[] packet, int length = -1)
        {
            if (length < 0)
            {
                length = packet.Length;
            }
            if (!IsValid(packet))
            {
                return(Message.TryCreate(packet, length));
            }

            return(new InternalMessage((DeviceAddress)packet[0], packet.SkipAndTake(3, ParseDataLength(packet))));
        }
Exemplo n.º 3
0
        static void iBus_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            ISerialPort port = (ISerialPort)sender;

            if (port.AvailableBytes == 0)
            {
                Logger.Warning("Available bytes lost! " + port.ToString());
                return;
            }
            lock (bufferSync)
            {
                byte[] data = port.ReadAvailable();
                #if DEBUG
                //Logger.Info(data.ToHex(' '), "<!");
                #endif

                /*#if DEBUG // TODO remove #if after tests
                 * if (messageBufferLength > 0)
                 * {
                 *  Logger.Warning("WD");
                 *  var elapsed = (DateTime.Now - lastMessage).GetTotalMilliseconds();
                 *  if (elapsed > messageReadTimeout)
                 *  {
                 *      Logger.Warning("Buffer skip: timeout ("+elapsed+"ms) data: " + messageBuffer.SkipAndTake(0, messageBufferLength).ToHex(" "));
                 *      messageBufferLength = 0;
                 *  }
                 * }
                 #endif*/
                if (messageBufferLength + data.Length > messageBuffer.Length)
                {
                    Logger.Info("Buffer overflow. Extending it. " + port.ToString());
                    byte[] newBuffer = new byte[messageBuffer.Length * 2];
                    Array.Copy(messageBuffer, newBuffer, messageBufferLength);
                    messageBuffer = newBuffer;
                }
                if (data.Length == 1)
                {
                    messageBuffer[messageBufferLength++] = data[0];
                }
                else
                {
                    Array.Copy(data, 0, messageBuffer, messageBufferLength, data.Length);
                    messageBufferLength += data.Length;
                }
                while (messageBufferLength >= Message.PacketLengthMin)
                {
                    Message m = Message.TryCreate(messageBuffer, messageBufferLength);
                    if (m == null)
                    {
                        if (!Message.CanStartWith(messageBuffer, messageBufferLength))
                        {
                            Logger.Warning("Buffer skip: non-iBus data detected: " + messageBuffer[0].ToHex());
                            SkipBuffer(1);
                            continue;
                        }
                        return;
                    }
                    ProcessMessage(m);
                    //#if DEBUG
                    //m.PerformanceInfo.TimeEnqueued = DateTime.Now;
                    //#endif
                    //messageReadQueue.Enqueue(m);
                    SkipBuffer(m.PacketLength);
                }
                lastMessage = DateTime.Now;
            }
        }
Exemplo n.º 4
0
        protected virtual void bus_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            ISerialPort port = (ISerialPort)sender;

            if (port.AvailableBytes == 0)
            {
                Logger.Warning("Available bytes lost! " + port.ToString());
                return;
            }
            lock (bufferSync)
            {
                byte[] data = port.ReadAvailable();
                if (messageBufferLength + data.Length > messageBuffer.Length)
                {
                    Logger.Warning("Buffer overflow. Extending it. " + _portName);
                    byte[] newBuffer = new byte[messageBuffer.Length * 2];
                    Array.Copy(messageBuffer, newBuffer, messageBufferLength);
                    messageBuffer = newBuffer;
                }
                if (data.Length == 1)
                {
#if DebugOnRealDeviceOverFTDI && OnBoardMonitorEmulator // remove 0x00 when FTDI sends it for first time
                    if (data[0] == 0x00)
                    {
                        return;
                    }
#endif
                    messageBuffer[messageBufferLength++] = data[0];
                }
                else
                {
                    Array.Copy(data, 0, messageBuffer, messageBufferLength, data.Length);
                    messageBufferLength += data.Length;
                }
                bool shouldDisplayCorruptedMessageBuffer = true;
                while (messageBufferLength >= Message.PacketLengthMin)
                {
                    Message m = Message.TryCreate(messageBuffer, messageBufferLength);
                    if (m == null)
                    {
                        if (!Message.CanStartWith(messageBuffer, messageBufferLength))
                        {
                            if (shouldDisplayCorruptedMessageBuffer)
                            {
                                //Logger.Warning("Buffer skip: non-" + _portName + " data detected: " + messageBuffer[0].ToHex());
                                //Logger.Warning("Buffer skip: non-" + _portName + " data detected: " + messageBuffer.ToHex());
                                shouldDisplayCorruptedMessageBuffer = false;
                            }

                            SkipBuffer(1);
                            continue;
                        }
                        return;
                    }
                    ProcessMessage(m);
                    //#if DEBUG
                    //m.PerformanceInfo.TimeEnqueued = DateTime.Now;
                    //#endif
                    //messageReadQueue.Enqueue(m);
                    SkipBuffer(m.PacketLength);

                    shouldDisplayCorruptedMessageBuffer = true;
                }
                lastMessage = DateTime.Now;
            }
        }