Click here to Skip to main content
15,881,654 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm reading data from serial port using Serialport DataReceived event and Updating these data in UI, its working fine but some times i'll get incomplete data like first bit of a byte is missing..even after giving delay ...it is not capturing the first bit some times.here i created separate class file having serialportdataReceieved event and calling methods in xaml.cs file

I used Serialport read method and this is my below code.
C#
 public SerialPortObj(string port)
         {
             szport = port;
             spobj.PortName = szport;           
             spobj.DataReceived += spobj_DataReceived;
             Open();
         }

   public async void spobj_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                //Thread.Sleep(100);              
                byte[] data = new byte[1024];
                byte[] input;
                int iRead = 0;
                //await Task.Delay(100);
                int iNumofBytes = spobj.BytesToRead;
                if (spobj.BytesToRead > 0)
                {
                    input = new byte[spobj.BytesToRead];
                    spobj.Read(input, 0, input.Length);
                    await Task.Delay(100);
                    foreach (var item in input)
                    {
                        data[iRead] = item;
                        iRead++;
                    }
                    var temp = DataReceived;
                    if (temp != null)
                    {
                        temp(data);
                    }
                }
}
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
            }
        }


In UI,xaml.cs
C#
private void btnpress_Click(object sender, RoutedEventArgs e)
      {
          try
          {
              if (!string.IsNullOrEmpty(cmbComportAccess.Text))
              {
                  szPortName = cmbComportAccess.Text;
                  spObjone = new SerialPortObj(szPortName);
                  spObjone.DataReceived += ReadSerialportData;
              }
              else
              {
                  MessageBox.Show("Comport not connected);
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show("Its already reading the data);

          }
          finally
          {

          }
      }

      private void ReadSerialportData(byte[] data)
      {
          List<string> list =new List<string>();
          string szdata = string.Empty;
          string szValue = string.Empty;
          string szone = string.Empty;
          string[] arr = new string[5];
          szone = System.Text.ASCIIEncoding.ASCII.GetString(data);
          arr = szone.Split('\n','\r','\0');
          foreach(var item in arr)
          {
              if (!string.IsNullOrEmpty(item))
              {
                  list.Add(item);

              }
          }


          if (arr.Count() > 0)
          {

                  this.txtData.Dispatcher.BeginInvoke(new MsgDeletegate(GetValueFromSp), szone);

                  this.lblMsgDisplay.Dispatcher.BeginInvoke(new ResultDel(StartProcessing), list);


          else
          {
              MessageBox.Show("No Response from device );
          }

      }


Some timesapplication onload i get "The I/O operation has been aborted because of either a thread exit or an application request" error.

what could be the problem?? how can i solve it...any idea and any suggestion ?
Posted
Updated 31-Dec-13 0:08am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900