Click here to Skip to main content
15,945,344 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Just going through, debugging a program that uses
.ReadExisting()

I was wondering if this is why some of the data comes in irregularly i.e. it does not see the delimiter and the end of the string back from the unit.
I was wondering about using the ReadTo method i.e.
.ReadTo(">");

as the data sent is appended with a ">" but trying this yesterday led to a lot of trouble and crashed PC (I'm starting to suspect the board "but it works with HyperTerminal so it must be your code" quote). The routine I am using for send receive is detailed at the bottom it is called with the following means
C#
string ReadData;
        Reply_Status = (int)REPLY.NO_REPLY;
        ReadData = Write_Outstring(Command);


HELP!
Glenn

C#
private string Write_Outstring(Data_To_Unit)
        {
            string Data_From_Unit = "";

            Reply_Status = (int)REPLY.NO_REPLY;
            rtbCommsRecord.Text += Data_To_Unit + "\n";

            myComPort.Write(Data_To_Unit + (char)10 + (char)13);
            {
                while (Reply_Status == (int)REPLY.NO_REPLY)
                {
                    NoDataAtPort.Enabled = true;
                }
                NoDataAtPort.Enabled = false;

                if (Reply_Status == (int)REPLY.TIMEOUT_REPLY)
                {
                    Data_From_PitStop = "TIMEOUT\n";
                }
                else if (Reply_Status == (int)REPLY.YES_REPLY)

                {
                    Data_From_Unit = myComPort.ReadExisting();
                    rtbCommsRecord.Text += Data_From_Unit; // +"\n";
                    rtbIncoming.Text = Data_From_Unit;

                    //                rtbLogged.Text +=ReplyData;
                    if (Data_From_Unit.Contains("UD:"))
                    {
                        // rtbReturnedData.Text += Data_From_Unit + '\n' + '\r';
                        rtbUnlistedData.Text += Data_From_Unit+ '\n';// + '\r'; here is the problem
                    }

                    if (Data_From_Unit.Contains("LD:"))
                    {
                        // rtbReturnedData.Text += Data_From_Unit + '\n' + '\r';
                        rtbListedData.Text += Data_From_Unit;// +'\n';// + '\r';
                    }
                    if (Data_From_Unit.Contains("OK"))
                    {
                        rtbLogged.Text += rtbIncoming.Text;
                    }
                    rtbLogged.Text += rtbIncoming.Text;
                }
                else
                {
                    MessageBox.Show("ERROR IN Pitstop Comms\n");
                    Reply_Status = (int)REPLY.NO_REPLY;
                }
            }

            return (Data_From_Unit);


        }


UPDATE: The Data being returned by ReadExisting() is not complete I found this by sticking a message box to slow/stop comms flow, I think I need an example of how to the above with .ReadTo() which will cause problems if I use .ReadTo(">") some data returns "\n\r>" and some other parts return ">\r>" would .ReadTo(\r and >) be possible?

Glenn
Posted
Updated 29-Feb-12 4:12am
v3

1 solution

it`s better to read data in serial port data receive event.
then it`s also better to read all in buffer using ( ReadExsting ) method after read all data in buffer try to parse string that you want to be parsed or analyzed !
 
Share this answer
 

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