Converting char received from uart to a int

General discussion on mikroC for dsPIC30/33 and PIC24.
Post Reply
Author
Message
jgiven
Posts: 2
Joined: 17 Nov 2008 14:53
Location: USA

Converting char received from uart to a int

#1 Post by jgiven » 17 Nov 2008 15:34

Hi all, This is my first PIC project and I haven't touched C in about 10 years, so please bear with me. I need to send numeric data via serial RS232 to my PIC24. I am trying to send a data stream that gives how many steps to move a stepper motor and what direction. I typical data stream would consist of "R200E" or "L45E". This data stream would basically get interpreted as 'R' or 'L' for Right or Left rotation, then a numeric value for the number of steps to move the motor, then 'E' to signify the End of the data stream. I get the R,L, and E ok but I can't seem to convert the numberic value to an integer. Here is what I have for code:

void main()
{
int currentposition=0;
char data[];
char receive;
char direction;
int count;
Lcd_Custom_Init_LV_24_33();
Uart1_Init_Advanced(9600,0,0,0);

while(1)
{
while(!Uart1_Data_Ready()){}
receive = Uart1_Read_Char();
Lcd_Custom_Chr(1,1,receive); // Just checking for data.

if(receive == 'E')
{
currentposition = movegauge((int)data, direction);
Lcd_Custom_Out(2,1,data);
}
else if(receive == 'R' || receive == 'L')
{
direction = receive;
count = 0;
Lcd_Custom_Chr(2,5,direction);
}
else
{
data[count] = receive;
}
}
}

thanks
Josh

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#2 Post by Dany » 17 Nov 2008 18:25

Code: Select all

movegauge((int)data, direction);
Hi, I think you need to use "StrToInt" in stead of "Int", the latter is not capable of converting a string into an integer.

So:

Code: Select all

StrToInt(data, IntVar);
currentposition = movegauge(IntVar, direction);;
(IntVar should be declared as an integer).

Additionally: to be able to use StrToInt, the "data" has to be terminated with a zero. This zero could be added if you see the "E" coming in. So:

Code: Select all

if(receive == 'E')
{
data[count] := 0;
StrToInt(data, IntVar);
currentposition = movegauge(IntVar, direction);
Lcd_Custom_Out(2,1,data);
}


Third: I never see "count" changing to another value than zero. So

Code: Select all

else
{
data[count] = receive;
count = count + 1;
} 
And, I do not know very much about (mikro)C, but

Code: Select all

char data[];
seems not good to me, it does not reserve any bytes (I think) to store a string meant to contain an integer string (at least 6 places should be reserved).
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

jgiven
Posts: 2
Joined: 17 Nov 2008 14:53
Location: USA

#3 Post by jgiven » 17 Nov 2008 20:08

Wow! Thanks for the input Dany. You caught quite a few gotcha's in my code. I think the not incrementing the count variable was adding to my confusion too. One question though, I don't see in the help and documentation files where StrToInt() is a library function available to me in C. Is there an include file I need to use?
Josh

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

#4 Post by Sobrietytest » 18 Nov 2008 01:38

I don't think StrToInt exists. IntToStr does becuase it's easy to implement; all it does is convert a numerical integer into it's visual representation. StrToInt is a different matter because it wouldn't be able to differentiate between characters and numbers - not without some lengthy filtering anyway.

If you take a look at the code for programming/reading the date/time on the RTC module you will see what I mean - all those /10+48 & %10+48 are all about resolving the ASCII codes into their numerical equivalents, yet the filter only works for ASCII numbers, anything else throws an error. Therefor StrToInt would be slow and it wouldn't be able to predict the result that you are looking for, because some of the letter values would be the same as the number equivalents, if you see what I mean!

It would be better to write your own filter to deal with expected values (like the RTC code does), i.e. interpret the first byte as your left/right control, take the next three bytes as numerical values and convert them as such. If every control word is four bytes long you don't need a terminator...

L120
R002
L014
etc...

idakota
Posts: 334
Joined: 27 Sep 2006 08:07
Location: Pretoria/South Africa
Contact:

#5 Post by idakota » 18 Nov 2008 10:45

StrToInt does not exist.

However, I have created my own and give it to you freely :D

Code: Select all

int strToInt(char *string)
{
 int output = 0, i = 1, stringLength = strlen(string), temp = 0, numberLength = 0,j;
 char tempCh;

 for(j=0;j<stringLength;j++)
 {
  tempCh = string[j];
  temp = tempCh - 48;
  if (temp >= 0 && temp <= 9)
  {
   numberLength++;
  }
 }

 for(j=0;j<stringLength;j++)
 {
  tempCh = string[j];
  temp = tempCh - 48;
  if (temp >= 0 && temp <= 9)
  {
   output += temp * pow(10, (numberLength-i));
   i++;
  }
 }

 return output;
}
Someone with a bit more knowledge of C then me can probably shorten it. It will accept any string in any form and extract the numbers from it and return an integer.

Some example outputs:
"12345abc" = 12345
"abc12345" = 12345
"123abc45" = 12345
"12.345" = 12345

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#6 Post by Dany » 18 Nov 2008 11:41

jgiven wrote:I don't see in the help and documentation files where StrToInt() is a library function available to me in C.
Hi, I am sorry. The procedure "StrToInt" is available in mikroPascal, and I assumed it would be also in mikroC. :oops:
I see that in the mean time some other people posted their "StrToInt" code. :D Nice.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Ghassen.Halleb
Posts: 4
Joined: 25 Jul 2013 17:58

Re: Converting char received from uart to a int

#7 Post by Ghassen.Halleb » 25 Jul 2013 18:03

jgiven wrote:Hi all, This is my first PIC project and I haven't touched C in about 10 years, so please bear with me. I need to send numeric data via serial RS232 to my PIC24. I am trying to send a data stream that gives how many steps to move a stepper motor and what direction. I typical data stream would consist of "R200E" or "L45E". This data stream would basically get interpreted as 'R' or 'L' for Right or Left rotation, then a numeric value for the number of steps to move the motor, then 'E' to signify the End of the data stream. I get the R,L, and E ok but I can't seem to convert the numberic value to an integer. Here is what I have for code:

void main()
{
int currentposition=0;
char data[];
char receive;
char direction;
int count;
Lcd_Custom_Init_LV_24_33();
Uart1_Init_Advanced(9600,0,0,0);

while(1)
{
while(!Uart1_Data_Ready()){}
receive = Uart1_Read_Char();
Lcd_Custom_Chr(1,1,receive); // Just checking for data.

if(receive == 'E')
{
currentposition = movegauge((int)data, direction);
Lcd_Custom_Out(2,1,data);
}
else if(receive == 'R' || receive == 'L')
{
direction = receive;
count = 0;
Lcd_Custom_Chr(2,5,direction);
}
else
{
data[count] = receive;
}
}
}

thanks
Josh
Thank you for this Code I made some improvement on it I divided it into to parts the first checks if the conversion is possible the second is the conversion function

short int strToInt_verif(char *string){
j;

for(j=0;j<strlen(string);j++)
{
if (!(string[j] - 48 >= 0 && string[j] - 48 <= 9))
{
return(0);
}
}
return(1);}




int strToInt(char *string)
{
int output = 0,j,coef=1;

for(j=strlen(string),coef=1;j>=0;j++)
{


output += (string[j]- 48) * coef;
coef*=10;
}
}

Post Reply

Return to “mikroC for dsPIC30/33 and PIC24 General”