Example #1
0
        // Attached to port receive event (PORT thread)
        private void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (!connected || !port.IsOpen)
            {
                return;
            }

            string temp = port.ReadExisting();

            read_buffer += temp;

            string[] chunks = read_buffer.Split(new string[] { port.NewLine }, StringSplitOptions.None);

            read_buffer = "";

            if (chunks.Last() != "")
            {
                read_buffer = chunks.Last();
                chunks[chunks.Length - 1] = "";
            }

            foreach (var data in chunks)
            {
                Debug.WriteLine(data);

                if (data.Length < 3)
                {
                    continue;
                }

                int channel;
                if (!int.TryParse(data.Substring(0, 2), NumberStyles.HexNumber, null, out channel))
                {
                    continue;
                }

                int type_temp;
                if (!int.TryParse(data.Substring(2, 1), NumberStyles.HexNumber, null, out type_temp))
                {
                    continue;
                }

                vp_type type = (vp_type)type_temp;

                string value_string = data.Substring(3, data.Length - 3);
                object messagedata  = null;

                try
                {
                    switch (type)
                    {
                    case vp_type.vp_void:
                        messagedata = "";
                        break;

                    case vp_type.vp_string:
                        messagedata = value_string;
                        break;

                    case vp_type.vp_boolean:
                        messagedata = Convert.ToBoolean(Convert.ToInt32(value_string, 16));
                        break;

                    case vp_type.vp_byte:
                        messagedata = Convert.ToInt32(Convert.ToByte(Convert.ToInt16(value_string, 16)));
                        break;

                    case vp_type.vp_int:
                        messagedata = Convert.ToInt32(Convert.ToInt16(value_string, 16));
                        break;

                    case vp_type.vp_uint:
                        messagedata = Convert.ToInt32(Convert.ToUInt16(value_string, 16));
                        break;

                    case vp_type.vp_long:
                        messagedata = Convert.ToInt64(Convert.ToInt32(value_string, 16));
                        break;

                    case vp_type.vp_ulong:
                        messagedata = Convert.ToInt64(Convert.ToUInt32(value_string, 16));
                        break;

                    case vp_type.vp_float:
                        messagedata = float.Parse(value_string, CultureInfo.InvariantCulture);
                        break;
                    }

                    MessageReceived?.ThreadAwareRaise(this, new MessageEventArgs <object>(channel, type, messagedata));
                }
                catch (FormatException fe)
                {
                    Debug.WriteLine(fe);
                    continue;
                }
                catch (ArgumentOutOfRangeException ae)
                {
                    Debug.WriteLine(ae);
                    continue;
                }
            }
        }