Go Down

Topic: problem if serial send is not 3 digit. (Read 1 time) previous topic - next topic

vitesse

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: [Select]
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
}

Montreal nodes On globaltuners

AWOL

#1
Feb 07, 2010, 07:41 pm Last Edit: Feb 07, 2010, 07:42 pm by AWOL Reason: 1
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: [Select]
((SerialDataInBufferArray[2])-48)*100;
is easier to read (and understand) as:
Code: [Select]
((SerialDataInBufferArray[2])-'0')*100;
"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.

vitesse

#2
Feb 07, 2010, 07:43 pm Last Edit: Feb 07, 2010, 07:45 pm by vitesse Reason: 1
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.
Montreal nodes On globaltuners

AWOL

I really do suggest you use delimiters of some sort.
Otherwise, it is difficult to cope with dropped/corrupt characters.
"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.

vitesse

#4
Feb 07, 2010, 08:42 pm Last Edit: Feb 07, 2010, 08:50 pm by vitesse Reason: 1
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: [Select]

$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?
Montreal nodes On globaltuners

PaulS

#5
Feb 07, 2010, 11:11 pm Last Edit: Feb 07, 2010, 11:13 pm by PaulS Reason: 1
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', '>'?

vitesse

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.
Montreal nodes On globaltuners

PaulS

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>?

vitesse

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.
Montreal nodes On globaltuners

PaulS

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

Mitch_CA

I recommend you fire the other 19,639 people.  They are redundant ;)

vitesse

#11
Feb 12, 2010, 01:51 am Last Edit: Feb 12, 2010, 01:52 am by vitesse Reason: 1
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


Montreal nodes On globaltuners

Go Up