Pages: [1]   Go Down
Author Topic: problem if serial send is not 3 digit.  (Read 930 times)
0 Members and 1 Guest are viewing this topic.
Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a project that need to receive only 3 digit.

Here the code I use to read the serial port and to assemble the information.

I expect someone send from 000 to 360 into the system.

I have no problem limiting something out of range later in my project, but how can I make sure it use 3 digit and not only two or one?

If someone send 5 in place of 005 it doesn't compute the right way.

any idea on that?

here the code I use:

Code:
void SerialMessageParser()   // AZYMUTH CALCULATION
{
 
  if (SerialDataInBufferArray[0]=='T' && SerialDataInBufferArray[1]=='='){   // trame 'T='
    
      RequestDegree = RequestDegree + ((SerialDataInBufferArray[2])-48)*100;
      RequestDegree = RequestDegree + ((SerialDataInBufferArray[3])-48)*10;
      RequestDegree = RequestDegree + ((SerialDataInBufferArray[4])-48);


// DIRECTION CHOOSE
RotorDirection();
     }

for (int i = 0; i < 16; i++)  {SerialDataInBufferArray[i]=0; }        //CLEAR BUFFER AFTER READ
}

 
Logged

Montreal nodes On globaltuners

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24433
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If someone send 5 in place of 005 it doesn't compute the right way
So, you need some sort of delimeter to say "that's all I'm going to send".

Or use "atoi". (Don't forget to terminate your string)

Code:
((SerialDataInBufferArray[2])-48)*100;
is easier to read (and understand) as:
Code:
((SerialDataInBufferArray[2])-'0')*100;
« Last Edit: February 07, 2010, 01:42:00 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No I want to only accept information that as 3 digit from a serial communication and reject anything else that as less than 3

Edit: in fact I take this code from another project from a friend, I don't understand why he subtract 48.
« Last Edit: February 07, 2010, 01:45:20 pm by vitesse » Logged

Montreal nodes On globaltuners

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24433
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I really do suggest you use delimiters of some sort.
Otherwise, it is difficult to cope with dropped/corrupt characters.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Would it be possible to build the number like we do in php? Can't find something like that in arduino reference

What I would do in PHP is something like this
Code:
$Finaldigit=$Digit1$Digit2$Digit3

So if I send 2 4 and 5  it would make 245 and if send only 45 would make 45

Is that possible in Arduino language?
« Last Edit: February 07, 2010, 02:50:18 pm by vitesse » Logged

Montreal nodes On globaltuners

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46221
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Without an ending delimiter, how do you know, in a stream of data like '1', '2', '7', 4', '1', '5', '8', '1', '2', which characters go together to form a number?

Can you tell in a stream like '<', '1', '2', '7', '>', '<', 4', '1', '>', '<', '5', '8', '>', '<', '1', '2', '>'?
« Last Edit: February 07, 2010, 05:13:47 pm by PaulS » Logged

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

because I send a command like : X=300  So I assume it's always the caracter 3 to 6 that is used for the number.

Maybe this is a bad conception.
Logged

Montreal nodes On globaltuners

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46221
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Unless you can be assured that the three characters following the = will always be sent, and that there will always be three characters, yes, it is a bad assumption.

What's so difficult about making the sender send <X=300> or <X=30> or <X=5>?
Logged

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

20 000 user can connect and send information. Maybe I will resign to ask the guy that do the flash programing padd the number with 0 to always have good calculation.
Logged

Montreal nodes On globaltuners

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46221
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

20,000 people controlling one Arduino? What exactly is this Arduino going to be doing with the 3 digit number from 0 to 360?
Logged

Waterloo, Canada
Offline Offline
Full Member
***
Karma: 1
Posts: 242
Engineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I recommend you fire the other 19,639 people.  They are redundant smiley-wink
Logged

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

controling an antenna Rotor over internet. On a website that permit to share radio and control it over internet.


I finaly found a "great" solution. (as the user will send X=XXX)

I test to see if I have a number from 0 to 9 on byte 5 if yes I do calculation for 3 digit. If not I test if I have a number  from 0 to 9 on byte 4, if yes I do calculation with only two digit, and at last I use only the byte 3 to only say that the number is from 0 to 9


« Last Edit: February 11, 2010, 07:52:39 pm by vitesse » Logged

Montreal nodes On globaltuners

Pages: [1]   Go Up
Jump to: