Pages: [1]   Go Down
Author Topic: serial read garbage problem  (Read 327 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: 92
Posts: 3969
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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 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: 92
Posts: 3969
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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 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: 92
Posts: 3969
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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Pages: [1]   Go Up
Jump to: