Author Topic: ReadExisting() missing some data?.....  (Read 19847 times)

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
ReadExisting() missing some data?.....
« on: August 30, 2012, 06:25:22 am »
Hi All,
I am using C# to read from a USB comm port emulation, Most of the time it reads fine, using the below interrupt handler
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            Reply_Status = (int)REPLY.YES_REPLY;
            Pause.Stop();
            //add try catch
            try
            {
                InputData = myComPort.ReadExisting();
                if (InputData != String.Empty)
                {
                    this.BeginInvoke(new SetTextCallback(SetText), new object[] { InputData });
                }
                else if (InputData == String.Empty)
                {
                    return;
                }

            }
            catch(UnauthorizedAccessException ex)
            {
                MessageBox.Show("Error Caught!! "+ex.ToString());
            }
        }
   
But I have noticed in testing that some times this method doesn't always work some times the data is short (missing a few characters)  and some times a stray character causes a blank line in the data (which the get caught by the save  routine as the end of file). Is ReadExisting() reliable or would another method be better such as
 InputData = ReadTo(">")?

-=-=-=-
UpDate
=-=-=-=

 InputData = ReadTo(">")?  seems to be causing alot of problems anyone recommend a better one Read method? As ReadExisting() appears to occasionally throw an exception that can't be caught....
Glenn
« Last Edit: August 30, 2012, 06:44:04 am by GlennP »

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: ReadExisting() missing some data?.....
« Reply #1 on: August 30, 2012, 03:32:27 pm »

GlennP

  • Frequent Contributor
  • ****
  • Posts: 141
Re: ReadExisting() missing some data?.....
« Reply #2 on: August 31, 2012, 09:08:05 am »
I don't think so but I'll have a look, I was advised to use Read(), (from Serial Port Complete) Read() appears to be a method of reading Bytes, I was looking for a drop in replacement for the ReadExisting():
  else if (Reply_Status == (int)REPLY.YES_REPLY)
                {
                    Pause.Stop();
                    //add try catch
                    try
                    {
                        //InputData = myComPort.ReadTo(">");
                        InputData = myComPort.ReadExisting();

I appear to have cured the other issues I was having. The data I am reading back is of variable length and it looks as though the data is being sent correctly the ReadExisting() appears to be the problem!
Glenn

Glenn
« Last Edit: August 31, 2012, 09:15:47 am by GlennP »