示例#1
0
        public SerialEventMessage(message_frame i_msg)
        {
            m_data = new byte[i_msg.size];
            Buffer.BlockCopy(i_msg.msg, 0, m_data, 0, i_msg.size);
            m_head     = i_msg.msgflags;
            m_sizeData = i_msg.size;

            m_priority = (EVENT_PRIORITY)(m_head >> 5);
            m_event    = GetEventCode(i_msg.msg);
        }
        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);
            }
        }
        public SerialEventMessage( message_frame i_msg)
        {
            m_data = new byte[i_msg.size];
            Buffer.BlockCopy(i_msg.msg, 0, m_data, 0, i_msg.size);
            m_head = i_msg.msgflags;
            m_sizeData = i_msg.size;

            m_priority = (EVENT_PRIORITY)(m_head >> 5);
            m_event = GetEventCode(i_msg.msg);
        }