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
Converting char received from uart to a int
Code: Select all
movegauge((int)data, direction);
So:
Code: Select all
StrToInt(data, IntVar);
currentposition = movegauge(IntVar, direction);;
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;
}
Code: Select all
char data[];
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)
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)
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
Josh
-
- Posts: 619
- Joined: 05 Jul 2008 06:05
- Location: Thailand
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...
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...
StrToInt does not exist.
However, I have created my own and give it to you freely
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
However, I have created my own and give it to you freely
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;
}
Some example outputs:
"12345abc" = 12345
"abc12345" = 12345
"123abc45" = 12345
"12.345" = 12345
Hi, I am sorry. The procedure "StrToInt" is available in mikroPascal, and I assumed it would be also in mikroC.jgiven wrote:I don't see in the help and documentation files where StrToInt() is a library function available to me in C.
I see that in the mean time some other people posted their "StrToInt" code. 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)
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)
-
- Posts: 4
- Joined: 25 Jul 2013 17:58
Re: Converting char received from uart to a int
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 functionjgiven 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
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;
}
}