Pages: [1]   Go Down
Author Topic: serial read garbage problem  (Read 375 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i am a newbie and i want to send a simple integer to arduino via serial communication, i get the value but there is also garbage valuse i don't understand to reslove it... a little help will be appreciated.

Code:
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  while (Serial.available() == 0);
  int val = Serial.read() - '0';
  Serial.flush();
  Serial.println(val);
 
}

Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You have newline sending enabled.

When you send the number '7' for instance, you are actually sending the string "7\n", which is characters 55 and 10.  '0' is character 48, so 55 - 48 yeilds 7.  However, 10 - 48 yields -38.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.gammon.com.au/serial
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks it make sense can you tell me how to resolve the new line?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks it make sense can you tell me how to resolve the new line?
By selecting the option not to send a newline in the serial monitor.  The drop-down box that says "Newline" at the moment.  Change it.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I hope you aren't going to want to send more than "9" to the serial port, or you may have to rethink your approach. And you may have to read this:

http://www.gammon.com.au/serial
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I hope you aren't going to want to send more than "9" to the serial port, or you may have to rethink your approach. And you may have to read this:

http://www.gammon.com.au/serial

I'm sure that URL looks somewhat familiar... Haven't I seen it somewhere before?
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: [1]   Go Up
Jump to: