How to reduce mistakes in serial.read();

Hello, i have a problem. Could you explain me, why does my arduino receive, sometimes, broken DEC of ASCII? How to reduce it?

For exapmle, in C# (VS2008) I send byte, sSerial.writeByte(123), and transmits this number to arduino. It retransmits it to me and sometimes I receive instead of 123 this:
1
23
123
123
123
1
23
Why does it do it? what's the problem?

Thank you.

Unless you post your code nobody will be able to infer why this fails. Most probably because you send characters instead of bytes. In addition you most probably fail to understand the impact of timing on your code.

this is the piece of code in C# prog. :

private void btnSendByte_Click(object sender, EventArgs e)
{
if (Serial.IsOpen)
{
Serial.Write(Convert.ToString(currentBit)); //Where currentBit is a value from 1 to 255. It changes, when i slide my slider.
}
}

this piece if code in arduino:
...
Serial.begin(9600);
...
void loop(){
if(Serial.available() > 0){
check_value = Serial.read();
if(check_value >= 10){
delay(20);
Serial.print(check_value);
}
}

serial communication is asynchoruous, that means "unreliable in time" you dont exactly know when the following byte will arrive. To handle this you need to add tags to the datastream like < and > to identify the start of transmission and the end of transmission.

This ideas is as old as ASCII - http://www.asciitable.com/ - and if you look at the definition of the first 32 "characters" you see they all have a special meaning. For your problem you could keep to the ASCII standard and use STX and ETX to mark the datastream
another more used way is to add a linefeed after every measurement (CR/LF) and read until you have read that one.

like this?:
...
Serial.print('<');
Serial.print(value);
Serial.print('>')
...

or i must send like this?
...
Serial.write('<');
Serial.write(value);
Serial.write('>');
...

like this?:

No.

or i must send like this?

No.

The start and stop markers go in the sending code - the C# application.

Reading from the start marker to the end marker is what the Arduino needs to do. Search the forum for "started && ended" for sample receiving code using start and end markers.

Thanks for help! :slight_smile: