/// <summary>
        /// Handles DataReceived Event from SerialPort
        /// </summary>
        /// <param name="sender">Serial Port</param>
        /// <param name="e">Event arguments</param>
        private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // Initialize a buffer to hold the received data
            byte[] buffer = new byte[this.serialPort.ReadBufferSize];

            // There is no accurate method for checking how many bytes are read
            // unless you check the return from the Read method
            int bytesRead = this.serialPort.Read(buffer, 0, buffer.Length);

            // For the example assume the data we are received is ASCII data.
            this.tString += Encoding.ASCII.GetString(buffer, 0, bytesRead);

            // Check if string contains the terminator
            if (this.tString.IndexOf((char)this.terminator) > -1)
            {
                // If tString does contain terminator we cannot assume that it is the last character received
                string workingString = this.tString.Substring(0, this.tString.IndexOf((char)this.terminator));

                // Remove the data up to the terminator from tString
                this.tString = this.tString.Substring(this.tString.IndexOf((char)this.terminator));

                // Do something with workingString
                if (this.DataReceived != null)
                {
                    SerialPortEventArgs args = new SerialPortEventArgs(workingString);
                    this.DataReceived(this, args);
                }
            }
        }
Example #2
0
 void _workingObject_DataReceived(object sender, SerialPortEventArgs arg)
 {
     this.ReceivedText.Text += arg.ReceivedData;
 }
        /// <summary>
        /// Handles DataReceived Event from SerialPort
        /// </summary>
        /// <param name="sender">Serial Port</param>
        /// <param name="e">Event arguments</param>
        private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // Initialize a buffer to hold the received data
            byte[] buffer = new byte[this.serialPort.ReadBufferSize];

            // There is no accurate method for checking how many bytes are read
            // unless you check the return from the Read method
            int bytesRead = this.serialPort.Read(buffer, 0, buffer.Length);

            // For the example assume the data we are received is ASCII data.
            this.tString += Encoding.ASCII.GetString(buffer, 0, bytesRead);

            // Check if string contains the terminator
            if (this.tString.IndexOf((char)this.terminator) > -1)
            {
                // If tString does contain terminator we cannot assume that it is the last character received
                string workingString = this.tString.Substring(0, this.tString.IndexOf((char)this.terminator));

                // Remove the data up to the terminator from tString
                this.tString = this.tString.Substring(this.tString.IndexOf((char)this.terminator));

                // Do something with workingString
                if (this.DataReceived != null)
                {
                    SerialPortEventArgs args = new SerialPortEventArgs(workingString);
                    this.DataReceived(this, args);
                }
            }
        }
Example #4
0
 void _workingObject_DataReceived(object sender, SerialPortEventArgs arg)
 {
     this.ReceivedText.Text += arg.ReceivedData;
 }