Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using below codes,

port.Parity = Parity.None;
port.DataBits = 8;
port.StopBits = StopBits.One;
port.Encoding = System.Text.Encoding.ASCII;
port.Name ="COM1";
port.BaudRate=9600;
port.DataReceived += Port_DataReceived;


And while sending commands to Board,
port.RtsEnable = true;
port.Write(System.Text.Encoding.ASCII.GetBytes(dataToSend), 0, dataToSend.Length);
Thread.Sleep(15);
port.RtsEnable = false;


What I have tried:

I have tried using RealTerm, and I'm getting correct response all the time, I'm also getting correct response from above codes but some of the time its returning garbage which is not a case while sending from RealTerm.
Posted
Updated 26-Apr-19 5:23am
v2
Comments
Richard MacCutchan 26-Apr-19 5:02am    
You need to show the code where you are receiving garbage, and explain exactly what is happening.
Shrikant S 26-Apr-19 5:29am    
Its the same codes I'm using to send commands that's mentioned above. I'm receiving garbage in 'Port_DataReceived' event. I'm supposed to recieve '11111' but I'm getting 'RRSSS'. Its almost 1 out of 5 times only that I'm getting this garbage.

1 solution

It's called: "defensive programming"

Quote:
The DataReceived event is not guaranteed to be raised for every byte received. Use the BytesToRead property to determine how much data is left to be read in the buffer.
 
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