Click here to Skip to main content
11,496,146 members (452 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
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
            //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!

Posted 6-Dec-12 2:50am
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

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)
             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";
                else if (Reply_Status == (int)REPLY.YES_REPLY)
                    //add try catch
                        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!!" );
                    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();
            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;
            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:

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 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web01 | 2.8.150520.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