Go Down

Topic: manipulating serial data (Read 2337 times) previous topic - next topic

zodiak

Hello All,

I've been struggling for a while with the following:

I wrote code that fills in the contents of an array with bytes coming in the serial port:

Code: [Select]
while (finish != '#'  && i <= 13){ //if we have more than 13 we have more info than we should...
   command[i] = Serial.read();
   delay(5); // must be included to ensure board receives good data
   finish = command[i];
   Serial.print(command[i], BYTE); //DEBUG
   i++;
   // read the bytes comming in until end of comms (i.e. #); command[i] holds the position information
   // now the program is waiting for another * to come in.    
   }


I'm sending serial data from a PC and the output of the Serial.print(command, BYTE); is fine, this means I can see tat what I send from the PC is well received.

The problem comes when I need to manipulate the values on the array because of what seems to be some kind of variable type mismatch but I can't really tell. The following line does not output what is expected:

Code: [Select]
   g_pos = (command[1] * 100 + command[2] * 10 + command[3]) - 100;
   Serial.print(g_pos);


Because I think above I'm actually doing calculations on ASCII code rather then an integer so I tried the following:

Code: [Select]
g_pos = (char(command[1]) * 100 + char(command[2]) * 10 + char(command[3])) - 100;
   Serial.print(g_pos);


but it also doesn't output an integer. I read somewhere about the atoi() function which seems to be what I need but writing:

Code: [Select]
g_pos = (atoi(command[1]) * 100 + command[2] * 10 + command[3]) - 100;

gives me an error: error: invalid conversion from 'int' to 'const char*'

Am I going the right way with this?

TIA

PaulS

The atoi function takes a string as an argument, not a character.

Code: [Select]
g_pos = atoi(command);
The art of getting good answers lies in asking good questions.

GrooveFlotilla

Code: [Select]
g_pos = (char(command[1]) * 100 + char(command[2]) * 10 + char(command[3])) - 100;
You're very close, and bonus points for perseverence!

"atoi" works on strings, not "char"s, so either terminate your buffer with a zero and call "atoi", or
Code: [Select]
g_pos = ((command[1] - '0') * 100 + (command[2] - '0') * 10 + (command[3]) - '0') - 100;


Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

zodiak

Thank you for the replies, guys!

Groove the following code works like a charm, many thanks:
Code: [Select]
g_pos = ((command[1] - '0') * 100 + (command[2] - '0') * 10 + (command[3]) - '0') - 100;

May I just ask what the code above does? I'm confused, subtracting char '0' to the ascii code on each array element??

PaulS

If you look at the ascii table, there is a code assigned to each letter and number. The value isn't really important, but by subtracting the ascii value of '0' from each character, you get the relative position of the character.

If, for instance, the character is '3', then '3' -  '0' means that the '3' is 3 positions to the left of '0'. That offset is the numerical value of the character.
The art of getting good answers lies in asking good questions.

zodiak

Brilliant and clever! Took me a while to see that but I got it. Thanks!

Go Up