BRM_vscp_message.message_frame.AddByte C# (CSharp) Метод

AddByte() публичный Метод

public AddByte ( byte i_byte ) : bool
i_byte byte
Результат bool
        public bool AddByte(byte i_byte)
        {
            string Ls_string = i_byte.ToString() + " ";
            Debug.Write(Ls_string);
            switch (state_incoming)
            {
                case INCOMING_STATE_NONE:
                    if (INCOMING_SUBSTATE_NONE == substate_incoming && DLE == i_byte)
                    {
                        substate_incoming = INCOMING_SUBSTATE_DLE;
                    }
                    else if (INCOMING_SUBSTATE_DLE == substate_incoming && STX == i_byte)
                    {
                        state_incoming = INCOMING_STATE_STX;
                        substate_incoming = INCOMING_SUBSTATE_NONE;
                        size = 0;
                    }
                    else
                    {
                        state_incoming = INCOMING_STATE_NONE;
                        substate_incoming = INCOMING_SUBSTATE_NONE;
                    }
                    break;

                case INCOMING_STATE_STX:
                    if (INCOMING_SUBSTATE_NONE == substate_incoming && DLE == i_byte)
                    {
                        substate_incoming = INCOMING_SUBSTATE_DLE;
                    }
                    else if (INCOMING_SUBSTATE_DLE == substate_incoming && STX == i_byte)
                    {
                        // This is strange as a DEL STX is not expected here
                        // We try to sync up again...
                        state_incoming = INCOMING_STATE_STX;
                        substate_incoming = INCOMING_SUBSTATE_NONE;
                        size = 0;
                    }
                    else if (INCOMING_SUBSTATE_DLE == substate_incoming && ETX == i_byte)
                    {
                        // We have a packet received
                        state_incoming = INCOMING_STATE_NONE;
                        substate_incoming = INCOMING_SUBSTATE_NONE;

                        msgflags = msg[0];

                        // We must at least got the id == four bytes
                        if (size >= 4)
                        {
                            Debug.WriteLine("");
                            Debug.Write("New vscp message:");
                            return true;
                        }
                    }
                    // We come here if m_data is received
                    // but also for DLE DEL
                    else
                    {
                        substate_incoming = INCOMING_SUBSTATE_NONE;
                        if (size < msg.Length)
                        {
                            msg[size++] = i_byte;
                        }
                        else
                        {
                            // This packet has wrong format as it have
                            // to many databytes - start over!
                            state_incoming = INCOMING_STATE_STX;
                            substate_incoming = INCOMING_SUBSTATE_NONE;
                        }
                    }
                    break;

                case INCOMING_STATE_ETX:
                    break;
            }
            return false;
        }

Usage Example

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                // the the message to the serias interface
                char[] aSplit = { ',' };
                string sDataBytes = txtData.Text;
                string[] asData = sDataBytes.Split(aSplit);
                EventCode chosenEvent = m_eventCodeNameToVal[cbxEventCode.SelectedItem.ToString()];
                EVENT_PRIORITY chosenPriority = m_eventPriorityToval[cbxPriority.SelectedItem.ToString()];

                if (asData.Length == 1 && asData[0].Length == 0)
                {
                    asData = new string[0];
                }
                // length: dle, stx, flags, 4 bytes header, data bytes, dle, etx
                int len = 9 + asData.Length;
                byte[] aVscpMsg = new byte[len];
                aVscpMsg[0] = message_frame.DLE;
                aVscpMsg[1] = message_frame.STX;
                int iTemp = (int)chosenPriority;
                aVscpMsg[2] = (byte) (iTemp << 5);

                iTemp = (int)chosenEvent;
                int iVal = (iTemp >> 24) & 0xFF;
                aVscpMsg[3] = (byte)iVal;

                iVal = (iTemp >> 16) & 0xFF;
                aVscpMsg[4] = (byte)iVal;

                iVal = iTemp & 0xFF;
                aVscpMsg[5] = (byte)iVal;

                // nickname is zero
                aVscpMsg[6] = 0;

                int i = 0;
                for (; i < asData.Length; i++)
                {
                    string sNumber = asData[i];
                    if (sNumber.Length > 0)
                    {
                        aVscpMsg[7 + i] = (byte)Int32.Parse(sNumber, System.Globalization.NumberStyles.HexNumber);
                    }
                    else
                    {
                        aVscpMsg[7 + i] = 0;
                    }
                }
                aVscpMsg[7 + i] = message_frame.DLE;
                i++;
                aVscpMsg[7 + i] = message_frame.ETX;

                m_serialPort.Write(aVscpMsg, 0, aVscpMsg.Length);
                message_frame generated_vscp_message = new message_frame();
                for (int j = 0; j < aVscpMsg.Length; j++)
                {
                    if (generated_vscp_message.AddByte(aVscpMsg[j]))
                    {
                        // a new serial vscp message
                        DateTime now = DateTime.Now;
                        // Create message so CRC is checked if needed
                        SerialEventMessage msg = new SerialEventMessage(generated_vscp_message);
                        string sMsg = msg.ToString();

                        string sTime = now.ToLongTimeString() + ":" + now.Millisecond;
                        if (null != m_tracer)
                        {
                            m_tracer.Write(sTime + " " + sMsg);

                            ListViewItem item = new ListViewItem(sTime, sMsg);
                            item.SubItems.Add(msg.ToString());
                            m_listView.AddItem(item);

                            string sEventName = msg.m_event.ToString();
                            byte[] dataBytes = msg.GetDataBytes();
                            string sCrack = m_msgCracker.Decode(sEventName, dataBytes);
                            if (sCrack.Length > 0)
                            {
                                ListViewItem item2 = new ListViewItem(sTime, sMsg);
                                item2.SubItems.Add(sCrack);
                                m_listView.AddItem(item2);
                            }
                        }
                    }
                }
            }
            catch (IOException ex)
            {
                Debug.WriteLine("IOException: " + ex.Message);
                MessageBox.Show(ex.Message);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                MessageBox.Show(ex.Message);
            }
        }