# problem if serial send is not 3 digit.

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:

``````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
}
``````

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)

``````((SerialDataInBufferArray[2])-48)*100;
``````

is easier to read (and understand) as:

``````((SerialDataInBufferArray[2])-'0')*100;
``````

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.

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

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

``````\$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?

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’, ‘>’?

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.

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

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.

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

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

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