Go Down

### Topic: problem if serial send is not 3 digit. (Read 2486 times)previous topic - next topic

#### vitesse

##### Feb 07, 2010, 07:35 pm
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 CHOOSERotorDirection();     }for (int i = 0; i < 16; i++)  {SerialDataInBufferArray[i]=0; }        //CLEAR BUFFER AFTER READ} `

#### AWOL

#1
##### Feb 07, 2010, 07:41 pmLast 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.
I speak for myself, not Arduino.

#### vitesse

#2
##### Feb 07, 2010, 07:43 pmLast 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.

#### AWOL

#3
##### Feb 07, 2010, 07:46 pm
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.
I speak for myself, not Arduino.

#### vitesse

#4
##### Feb 07, 2010, 08:42 pmLast 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?

#### PaulS

#5
##### Feb 07, 2010, 11:11 pmLast 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

#6
##### Feb 08, 2010, 01:58 am
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.

#### PaulS

#7
##### Feb 08, 2010, 02:03 am
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

#8
##### Feb 09, 2010, 12:35 am
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.

#### PaulS

#9
##### Feb 09, 2010, 12:47 am
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_79

#10
##### Feb 09, 2010, 07:10 pm
I recommend you fire the other 19,639 people.  They are redundant

#### vitesse

#11
##### Feb 12, 2010, 01:51 amLast 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

Go Up