private void SerialDataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            if (sender == null)
            {
                return;
            }

            BindableSerialPort sp = (BindableSerialPort)sender;

            // Read all available bytes from serial port in one chunk
            try
            {
                byte[] inData = new Byte[sp.BytesToRead];
                sp.Read(inData, 0, inData.Length);

                // Parse all bytes read through BGLib parser
                for (int i = 0; i < inData.Length; i++)
                {
                    bglib.Parse(inData[i]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }
Esempio n. 2
0
        // Serial port read
        private async Task ReadSerialAsync()
        {
            uint ReadBufferLength = 32;
            uint bytesRead        = 0;

            this.bleInputData.InputStreamOptions = InputStreamOptions.Partial;

            try
            {
                bytesRead = await this.bleInputData.LoadAsync(ReadBufferLength);

                if (bytesRead > 0)
                {
                    byte[] receivedBytes = new byte[bytesRead];
                    try
                    {
                        this.bleInputData.ReadBytes(receivedBytes);
                        for (int i = 0; i < receivedBytes.Length; i++)
                        {
                            bglib.Parse(receivedBytes[i]);
                        }
                    }
                    catch (Exception ex)
                    {
                        this.ErrorValue.Text   = "Parse error!";
                        this.ErrorMessage.Text = ex.Message;
                    }
                }
            }
            catch (Exception ex)
            {
                this.ErrorValue.Text   = "Serial read error!";
                this.ErrorMessage.Text = ex.Message;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 一旦ComDevice.DataReceived事件发生,就将从串口接收到的数据显示到接收端对话框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnComReceived(object sender, SerialDataReceivedEventArgs e)
        {
            //开辟接收缓冲区
            byte[] ReDatas = new byte[comDev.BytesToRead];

            //从串口读取数据
            comDev.Read(ReDatas, 0, ReDatas.Length);

            // DEBUG: display bytes read
            // parse all bytes read through BGLib parser
            for (int i = 0; i < ReDatas.Length; i++)
            {
                bglib.Parse(ReDatas[i]);
            }
            //实现数据的解码与显示
        }
Esempio n. 4
0
        private void DataReceivedHandler(
            object sender,
            System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            System.IO.Ports.SerialPort sp = (System.IO.Ports.SerialPort)sender;
            Byte[] inData = new Byte[sp.BytesToRead];
            sp.Read(inData, 0, sp.BytesToRead);

#if PRINT_DEBUG
            // PRINT_DEBUG: display bytes read
            ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("<= RX ({0}) [ {1}]", inData.Length, ByteArrayToHexString(inData)) + Environment.NewLine); });
#endif
            for (int i = 0; i < inData.Length; i++)
            {
                bglib.Parse(inData[i]);
            }
        }
Esempio n. 5
0
        // Serial port event handler for a nice event-driven architecture
        private void DataReceivedHandler(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            System.IO.Ports.SerialPort sp = (System.IO.Ports.SerialPort)sender;
            Byte[] inData = new Byte[sp.BytesToRead];

            // read all available bytes from serial port in one chunk
            sp.Read(inData, 0, sp.BytesToRead);

            // DEBUG: display bytes read
            ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("<= RX ({0}) [ {1}]", inData.Length, ByteArrayToHexString(inData)) + Environment.NewLine); });

            // parse all bytes read through BGLib parser
            for (int i = 0; i < inData.Length; i++)
            {
                bglib.Parse(inData[i]);
            }
        }
Esempio n. 6
0
        private void DataReceivedHandler(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            var sp          = (System.IO.Ports.SerialPort)sender;
            var bytesToRead = sp.BytesToRead;
            var inData      = new Byte[bytesToRead];

            // read all available bytes from serial port in one chunk
            var bytesRead = sp.Read(inData, 0, bytesToRead);

            // DEBUG: display bytes read
            //Console.WriteLine("<= RX ({0}) [ {1}]", inData.Length, ByteArrayToHexString(inData));

            // parse all bytes read through BGLib parser
            for (int i = 0; i < inData.Length; i++)
            {
                bglib.Parse(inData[i]);
            }
        }
Esempio n. 7
0
        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            if (!port.IsOpen)
            {
                return;
            }

            byte[] inData = new byte[port.BytesToRead];

            port.Read(inData, 0, inData.Length);

            Debug.WriteLine("<= RX ({0}) [ {1}]", inData.Length, BitConverter.ToString(inData));

            for (int i = 0; i < inData.Length; i++)
            {
                bluetooth.Parse(inData[i]);
            }
        }
Esempio n. 8
0
        private void DataReceivedHandler(
            object sender,
            System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            System.IO.Ports.SerialPort sp = (System.IO.Ports.SerialPort)sender;
            int size = sp.BytesToRead;

            Byte[] inData = new Byte[size];

            // read all available bytes from serial port in one chunk
            sp.Read(inData, 0, size);

            // DEBUG: display bytes read
            //Console.WriteLine(String.Format("<= RX ({0}) [ {1}]", inData.Length, ByteArrayToHexString(inData)) + Environment.NewLine);

            // parse all bytes read through BGLib parser
            for (int i = 0; i < inData.Length; i++)
            {
                bglib.Parse(inData[i]);
                //Console.WriteLine("isData : " + inData[i]);
            }
        }
Esempio n. 9
0
        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp        = (System.IO.Ports.SerialPort)sender;
            int        numOfByte = sp.BytesToRead;

            Byte[] inData = new Byte[numOfByte];

            // read all available bytes from serial port in one chunk
            sp.Read(inData, 0, numOfByte);

            // DEBUG: display bytes read
            Console.WriteLine("<= RX ({0}) [ {1}]", inData.Length, ByteArrayToHexString(inData));

            mSerial.RcvMsg = true;
            mSerial.RcvLog = String.Format("<= RX ({0}) [ {1}]\r\n", inData.Length, ByteArrayToHexString(inData));

            // parse all bytes read through BGLib parser
            for (int i = 0; i < inData.Length; i++)
            {
                bglib.Parse(inData[i]);
            }
        }