Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
Questions are there a Serial Port Guru's on the CodeProject,I pretty sure my issue is caused by the hardware but I need a work around. I have to replace an elderly PSION Walkabout with a Netbook, I now have all the comms documentation have set up my port and can talk to the hardware. I appears that a buffer is not being cleared of the data once it has been processed. I have gone through the angony of
 //myComPort.DiscardInBuffer();
//myComPort.DiscardOutBuffer();
//rtbIncomingData.Text = "";
uncommentted to clear the buffers on the PC, this appears to have no effect, I'm pretty sure it being latched until read (which would make sense) is there a Serial Port Class method to clear any latching (vain hope!) Could I be on to something here as the PSION code has several undocumented features / routines which I can't see. Ah well of to MSDN I go!

Glenn
Posted
Comments
Jibesh 6-Dec-12 17:34pm    
how do you read the data from the serial port? ReadByte or ReadString ?
can you copy the code here ?
glennPattonWork3 7-Dec-12 4:09am    
Ah okay!, was typing the question and got grabbed to do something else! I will post my code in the solution box as it will appear a bit nicer!! Glenn

1 solution

This I know is a bit of a cludgy method but it is the only one I have to effectivly hold the reading code until there is a reply with out locking up windows it is called by Write_Board("hello");
which will send hello out of myComPort with a \n & \r the timer NoDataAtPort is atimer with a 10 second time out which should be suitable if the device has died or been taken away,
C#
private string Write_Board(string Data_To_Board)
       {

           Pause.Start();
            myComPort.Write(Data_To_Board + (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_Board = "TIMEOUT\n";
                   Pause.Stop();
               }
               else if (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 (null);
                       }

                   }
                   catch (UnauthorizedAccessException)
                   {
                        MessageBox.Show("Error Caught!!" );
                   }

               }
               else
               {

                   Cursor.Current = Cursors.No;
                   Reply_Status = (int)REPLY.NO_REPLY;
               }
           }
           Data_From_Board = Data_From_Board + "|||";

           return (Data_From_Board);

       }

----------------
also I am using another timer to see if there is any data at the port also Pause is a timer 100 ms time out (I'm starting to wonder if I have toxic timer syndrome!)
----------------

C#
public int WaitForData(int Timeout)
{
    Stopwatch sw = Stopwatch.StartNew();
    sw.Start();
    this.Invoke(new MethodInvoker(delegate()
    {
        rtbIncomingData.Text += ReplyData;
    }));

    while (sw.ElapsedMilliseconds < Timeout && myComPort.BytesToRead > 0)
    {

        Thread.Sleep(1);  // sleep for 1 millisecond
    }

    int elapsed = (int)sw.ElapsedMilliseconds;
    sw.Stop();
    return elapsed;
}

This method is one I have use multiple times with success I did developed it in a caffine fuel weekend to prevent bits of data replies going missing ReadExisting() was chosen as the read method (I have used it with ReadLine() when I was sure there was a \n in the data) as there is no documentation for this hardware and the PSION appears to strip everything but the data and I can't get any data to appear using it while read existing I do.

Any ideas:
 
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