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

I have an issue with some code I am writing for interfacing to a board. The device is connected to the PC via a USB cable and is emulating a Com port. I am using the ReadExisting() method to get the data back from this unit as follows
C#
try
                  {
                      //InputData = myComPort.ReadTo(">");
                      InputData = myComPort.ReadExisting();
                      if (InputData != String.Empty)

This works most of the time occasionally I am seeing the odd bit of missing data from the end of a string most commonly the last string read in. I can't count the characters in as not all the data is the same length (the 5 digit serial numbers weren't long enough so they were altered, where in the doc updates does it say that? another rant!) the data does appear to terminate with a CR/LF (0D / 0A) so I should be able to use a ReadLine() but if I substitute ReadLine() or ReadTo(">") (as the board gives > as a prompt) for ReadExisting() nothing happens I am seeing in a Serial Monitor the data come back SN=09122005 0D 0A 3E to give
SN=09122055
>

I'm guessing that the 3E is throwing the ReadLine() as the 0D is follow 0A followed by 3E anybody any ideas, I am puzzled as to why the ReadTo(">") is not working ideas please!
Glenn
I have altered my code to allow ReadLine() to work, I have not yet had an issue of missing data. A solution or not?
Posted
Updated 4-Sep-12 1:23am
v4
Comments
sjelen 4-Sep-12 7:58am    
check you settings for myComPort.Encoding, if you set it wrong ReadExisting and ReadLine may return unexpected results.
you can customize ReadLine behavior by setting myComPort.NewLine property - string that will be interpreted as new line separator.
glennPattonWork3 4-Sep-12 8:05am    
Thanks for that, the ComPort encoding is not the issue, I realized (more less as I was asking the question that the code was looking for a ">" not always there despite documented that it was (me bitter?, I'm not bitter...Oh JSOP!)

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