Click here to Skip to main content
15,881,248 members
Home / Discussions / C#
   

C#

 
QuestionCharacters are missing in Serial Port Communication Pin
Ashfaque Hussain16-Feb-14 19:10
Ashfaque Hussain16-Feb-14 19:10 
AnswerRe: Characters are missing in Serial Port Communication Pin
OriginalGriff16-Feb-14 20:09
mveOriginalGriff16-Feb-14 20:09 
GeneralRe: Characters are missing in Serial Port Communication Pin
Ashfaque Hussain16-Feb-14 22:41
Ashfaque Hussain16-Feb-14 22:41 
GeneralRe: Characters are missing in Serial Port Communication Pin
OriginalGriff16-Feb-14 22:47
mveOriginalGriff16-Feb-14 22:47 
GeneralRe: Characters are missing in Serial Port Communication Pin
Marco Bertschi17-Feb-14 1:06
protectorMarco Bertschi17-Feb-14 1:06 
GeneralRe: Characters are missing in Serial Port Communication Pin
OriginalGriff17-Feb-14 1:31
mveOriginalGriff17-Feb-14 1:31 
GeneralRe: Characters are missing in Serial Port Communication Pin
Marco Bertschi17-Feb-14 1:44
protectorMarco Bertschi17-Feb-14 1:44 
AnswerRe: Characters are missing in Serial Port Communication Pin
Marco Bertschi17-Feb-14 1:14
protectorMarco Bertschi17-Feb-14 1:14 
Instead of ReadExisting() you can also use ReadTo:

C#
void arduinoBoard_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    string data = arduinoBoard.ReadTo("\x03");//Read until the EOT code
    //Split into 'date=temperature' formatted text
    string[] dataArray = data.Split(new string[]
    {"\x02", "$" }, StringSplitOptions.RemoveEmptyEntries);
    //Iterate through the split data and parse it into weather data items
    //and add them to the list of received weather data.
    foreach (string dataItem in dataArray.ToList())
    {
       WeatherDataItem weatherDataItem = new WeatherDataItem();
       weatherDataItem.FromString(dataItem);
       weatherDataItems.Add(weatherDataItem);
     }

     if(NewWeatherDataReceived != null)//If there is someone waiting for this event to be fired
     {
       NewWeatherDataReceived(this, new EventArgs()); //Fire the event,
            // indicating that new WeatherData was added to the list.
     }
}


I used the byte 0x03 in my example because I know that a command always ends with 0x03, but this depends on the communication partner. Depending on your communication protocol you may need to replace 0x03 with something else, but in general you should either know the length of the message you receive, or with which byte it is going to end (In case you only know the message length you'd need to use SerialPort.ReadBytes).
The reason for that was stated correctly by OriginalGriff: Serial communication means that the information (text) is transmitted byte by byte, and therefore ReadExisting may not grab all the available bytes in the input buffer, because they were not transmitted yet.

I recommend you to read this article[^], it may help your understanding of serial communication and serial ports.
Women are waiting for love and men are waiting for women. - Wolf Wondratschek

QuestionStarting a new project in C# Pin
FilipJ16-Feb-14 15:18
FilipJ16-Feb-14 15:18 
AnswerRe: Starting a new project in C# Pin
Mycroft Holmes16-Feb-14 16:06
professionalMycroft Holmes16-Feb-14 16:06 
GeneralRe: Starting a new project in C# Pin
FilipJ16-Feb-14 16:55
FilipJ16-Feb-14 16:55 
GeneralRe: Starting a new project in C# Pin
Mycroft Holmes16-Feb-14 17:08
professionalMycroft Holmes16-Feb-14 17:08 
GeneralRe: Starting a new project in C# Pin
FilipJ16-Feb-14 17:38
FilipJ16-Feb-14 17:38 
GeneralRe: Starting a new project in C# Pin
Mycroft Holmes16-Feb-14 17:56
professionalMycroft Holmes16-Feb-14 17:56 
GeneralRe: Starting a new project in C# Pin
FilipJ16-Feb-14 18:10
FilipJ16-Feb-14 18:10 
AnswerRe: Starting a new project in C# Pin
Peter Leow16-Feb-14 16:36
professionalPeter Leow16-Feb-14 16:36 
GeneralRe: Starting a new project in C# Pin
FilipJ16-Feb-14 17:45
FilipJ16-Feb-14 17:45 
AnswerRe: Starting a new project in C# Pin
V.16-Feb-14 21:22
professionalV.16-Feb-14 21:22 
GeneralRe: Starting a new project in C# Pin
FilipJ16-Feb-14 23:46
FilipJ16-Feb-14 23:46 
GeneralRe: Starting a new project in C# Pin
V.16-Feb-14 23:52
professionalV.16-Feb-14 23:52 
GeneralRe: Starting a new project in C# Pin
FilipJ17-Feb-14 0:02
FilipJ17-Feb-14 0:02 
AnswerRe: Starting a new project in C# Pin
Ravi Bhavnani17-Feb-14 7:49
professionalRavi Bhavnani17-Feb-14 7:49 
AnswerRe: Starting a new project in C# Pin
David C# Hobbyist.17-Feb-14 12:15
professionalDavid C# Hobbyist.17-Feb-14 12:15 
QuestionStruct vs Class? PinPopular
David C# Hobbyist.16-Feb-14 12:34
professionalDavid C# Hobbyist.16-Feb-14 12:34 
AnswerRe: Struct vs Class? Pin
Peter Leow16-Feb-14 13:29
professionalPeter Leow16-Feb-14 13:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.