Click here to Skip to main content
11,715,491 members (86,018 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# serialport
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 6-Dec-12 1:50am
Comments
jibesh at 6-Dec-12 17:34pm
   
how do you read the data from the serial port? ReadByte or ReadString ?
can you copy the code here ?
glennPattonWork at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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,
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!)
----------------

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:
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 518
1 Richard MacCutchan 300
2 Maciej Los 259
3 ManasKumarM 155
4 Frankie-C 125
0 Sergey Alexandrovich Kryukov 785
1 CHill60 415
2 Richard MacCutchan 405
3 Maciej Los 389
4 OriginalGriff 320


Advertise | Privacy | Mobile
Web02 | 2.8.150901.1 | Last Updated 7 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100