PORTS Forum

Ports and Interfaces => Serial Ports => Topic started by: GlennP on August 30, 2012, 06:25:22 am

Title: ReadExisting() missing some data?.....
Post by: GlennP 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
Title: Re: ReadExisting() missing some data?.....
Post by: Jan Axelson on August 30, 2012, 03:32:27 pm
Encoding issues?

http://www.lvr.com/forum/index.php?topic=683
Title: Re: ReadExisting() missing some data?.....
Post by: GlennP 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