Canada
Offline
Newbie
Karma: 0
Posts: 26
Arduino rocks
|
 |
« on: February 07, 2010, 01:35:06 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: 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
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: February 07, 2010, 01:41:20 pm » |
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;
|
|
|
|
« 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.
|
|
|
|
Canada
Offline
Newbie
Karma: 0
Posts: 26
Arduino rocks
|
 |
« Reply #2 on: February 07, 2010, 01:43:35 pm » |
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
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: February 07, 2010, 01:46:20 pm » |
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.
|
|
|
|
Canada
Offline
Newbie
Karma: 0
Posts: 26
Arduino rocks
|
 |
« Reply #4 on: February 07, 2010, 02:42:52 pm » |
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?
|
|
|
|
« Last Edit: February 07, 2010, 02:50:18 pm by vitesse »
|
Logged
|
Montreal nodes On globaltuners
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 334
Posts: 36433
Seattle, WA USA
|
 |
« Reply #5 on: February 07, 2010, 05:11:42 pm » |
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
Newbie
Karma: 0
Posts: 26
Arduino rocks
|
 |
« Reply #6 on: February 07, 2010, 07:58:37 pm » |
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
Offline
Brattain Member
Karma: 334
Posts: 36433
Seattle, WA USA
|
 |
« Reply #7 on: February 07, 2010, 08:03:54 pm » |
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
Newbie
Karma: 0
Posts: 26
Arduino rocks
|
 |
« Reply #8 on: February 08, 2010, 06:35:25 pm » |
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
Offline
Brattain Member
Karma: 334
Posts: 36433
Seattle, WA USA
|
 |
« Reply #9 on: February 08, 2010, 06:47:21 pm » |
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
Full Member
Karma: 1
Posts: 242
Engineer
|
 |
« Reply #10 on: February 09, 2010, 01:10:46 pm » |
I recommend you fire the other 19,639 people. They are redundant 
|
|
|
|
|
Logged
|
|
|
|
|
Canada
Offline
Newbie
Karma: 0
Posts: 26
Arduino rocks
|
 |
« Reply #11 on: February 11, 2010, 07:51:39 pm » |
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
|
|
|
|
|