|
|
View previous topic :: View next topic |
Author |
Message |
coderchick
Joined: 12 Jun 2008 Posts: 25
|
receive ASCII string and convert to int |
Posted: Wed Nov 19, 2008 4:26 pm |
|
|
Here's the scenario, my PIC pings a driver to get the current setting of a function. The driver then sends that information as an ASCII string, not a character, but a string. I need to read in that string and then convert it to an int for use throughout the rest of the code. I understand that I can use atoi() to convert the string to an integer, but how can I read in the string? getc() only reads in one character and I keep getting errors when I try to use gets(). Anyone else have a better idea how to go about this?
below is a little test I wrote up to first set the function on the driver to a known value (ranges from -128 to 127), in this case, br = 1, and then later I ping the driver to see what that value is. So anybody have an idea why gets() is giving me problems, or is there a better way to read in the string?
Code: | void main(void)
{
int test;
char temp[10];
output_high(PIN_A0);
puts("br(1)");
while (1)
{
if (input(PIN_B4) == 0)
{
puts("br()");
delay_us(500);
temp = gets();
test = atoi(temp);
}
} |
the delay_us() I have here does not interfere with data being received since the driver does not start sending the string until about 1000us (or 1ms) after the command is given. _________________ KMoe
~We the willing, led by the unknowing, are doing the impossible for the ungrateful. We have done so much, for so long, with so little. We are now qualified to do anything with nothing -M. Theresa |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 19, 2008 4:43 pm |
|
|
Use the get_string() function in this file instead:
Quote: | c:\program files\picc\drivers\input.c |
It prevents you from writing past the end of the input buffer array.
Quote: | So anybody have an idea why gets() is giving me problems |
Debug it by putting a printf statement after the call to gets(), and
display the string that you get:
Quote: |
delay_us(500);
temp = gets();
printf(temp);
test = atoi(temp);
|
Also, there are 3 different versions of atoi() depending on the size of
the input number. Choose the correct one for your application.
Code: | atoi( )
atol( )
atoi32( ) |
|
|
|
coderchick
Joined: 12 Jun 2008 Posts: 25
|
|
Posted: Wed Nov 19, 2008 5:50 pm |
|
|
Yea, the input.c file was one I looked at, I was just seeing if there were other ways.
I'd love to be able to debug and use the printf, but the problem is that it won't even let me compile. It highlights "temp = gets();" and tells me that it is "expecting an LVALUE such as a variable name or *expression". _________________ KMoe
~We the willing, led by the unknowing, are doing the impossible for the ungrateful. We have done so much, for so long, with so little. We are now qualified to do anything with nothing -M. Theresa |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
coderchick
Joined: 12 Jun 2008 Posts: 25
|
|
Posted: Wed Nov 19, 2008 6:28 pm |
|
|
My mistake, I overlooked that when looking for a possible built in function. However, the get_string() function in the input.c file doesn't return a value either, does it? So I don't quite understand how that helps me. I suppose I could modify it to return a value, be it string or int using the atoi, but I'm not sure if I want to mess with those files in case I, or someone else, should need them later. _________________ KMoe
~We the willing, led by the unknowing, are doing the impossible for the ungrateful. We have done so much, for so long, with so little. We are now qualified to do anything with nothing -M. Theresa |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 19, 2008 6:33 pm |
|
|
The get_string() function is safer to use, because it allows you to set
a limit on the number of characters that will be accepted. This means
you won't accidently write past the end of your input buffer, and destroy
other variables that are allocated at those addresses byte the compiler.
It's used for safety.
You don't need to return a value because you already know the address
of the buffer. In C, the address of an array (the buffer) is passed to
another function by giving it the the name of the array. You declared
the array in a line of code so you already know its name. |
|
|
coderchick
Joined: 12 Jun 2008 Posts: 25
|
|
Posted: Thu Nov 20, 2008 2:43 pm |
|
|
alright, i've cut my program down even further, and i still can't seem to read in an ascii string, below is my code:
Code: |
void main(void)
{
char test;
char temp[10];
int i;
output_high(VIDEO_PS_ENABLE);
delay_ms(1000);
puts("br(1)");
get_string(temp,8);
}
void get_string(char* s, unsigned int8 max) {
unsigned int8 len;
char c;
--max;
len=0;
do {
c=getc();
if(c==8) { // Backspace
if(len>0) {
len--;
putc(c);
putc(' ');
putc(c);
}
} else if ((c>=' ')&&(c<='~'))
if(len<=max) {
s[len++]=c;
putc(c);
}
} while(c!=13);
s[len]=0;
} |
now the problem is that it seems to be hanging at c=getc() in the get_string function
Code: | void get_string(char* s, unsigned int8 max) {
unsigned int8 len;
char c;
--max;
len=0;
do {
c=getc(); <--hangs here |
and it just sits there. I think part of the problem is that the driver I've hooked up to the PIC repeats the command i send it (ie. br(1)) before the puts() function is finished, and then sends the actual data (being 1). I don't know if the problem is that it's attempting to read the tail of the repeated command and gets stuck there or what's going on.
I'm going to try to represent data on the TX and RX pins from scoping the lines to try to give an idea what the driver is sending back to me.
Code: |
___ __ _ ___ _ __ ____________________...
TX - |_| |___| |_____| |_| |_| |___|
PIC sends out command
_________________ __ _ ___ _ __ _________
RX - |_| |___| |_____| |_| |_| |___|
driver repeats command before PIC is done sending
_______ __ __ _____ __ _.......
|_| |_| |_| |___| |_|
and then sends out data
|
so any hints, thoughts, reprimands, advice, etc. would be appreciated at this point. I can't believe I'm having such a hard time reading in an ascii string. I bow to the masses, please help! _________________ KMoe
~We the willing, led by the unknowing, are doing the impossible for the ungrateful. We have done so much, for so long, with so little. We are now qualified to do anything with nothing -M. Theresa |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 20, 2008 3:01 pm |
|
|
You didn't post the #include, #fuses, #use delay(), or #use rs232()
statements. Also, you don't need to copy and paste the get_string()
function. Just #include the input.c file. Here's a test program.
Make a program similar to this and see if you can type in a string
in a terminal program on your PC, and get the PIC to receive it
and send it back to the PC.
Code: | #include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <input.c>
//======================================
void main()
{
char temp[10];
printf("Enter string: ");
get_string(temp, sizeof(temp));
printf("\n\rReceived: %s \n\r", temp);
while(1);
} |
|
|
|
coderchick
Joined: 12 Jun 2008 Posts: 25
|
|
Posted: Thu Nov 20, 2008 8:12 pm |
|
|
Code: | #include <stdlib.h>
#include <input.c>
#include <16F887.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES XT //Crystal osc <= 4mhz for PCM/PCH
#FUSES PUT //Power Up Timer
#FUSES MCLR //Master Clear pin enabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPD //No EE protection
#FUSES NOBROWNOUT //No brownout reset
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES NOLVP //No low voltage prgming,
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOWRT //Program memory not write protected
#FUSES BORV40 //Brownout reset at 4.0V
#use delay(clock=4000000)
#use rs232(baud=57600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) |
Quote: |
Make a program similar to this and see if you can type in a string
in a terminal program on your PC |
Using that program I was able to communicate with my PIC through the terminal with no problems. I can also use the terminal to communicate with the driver, however, I still can't combine the two. Now what? The terminal program proves that my code can potentially use the get_string function, and I've been able to prove that I can talk to the driver through the terminal, so now, how do I subtract the terminal and get the two boards talking to each other? _________________ KMoe
~We the willing, led by the unknowing, are doing the impossible for the ungrateful. We have done so much, for so long, with so little. We are now qualified to do anything with nothing -M. Theresa |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 20, 2008 8:43 pm |
|
|
get_string() expects a carriage return to mark the end of a string.
Do you have a '\r' escape sequence at the end of your strings
in your Master PIC code ? Example:
|
|
|
coderchick
Joined: 12 Jun 2008 Posts: 25
|
|
Posted: Fri Nov 21, 2008 6:56 pm |
|
|
Yes, the driver sends out a carriage return of 13, 10, which is standard. I have no control or access to how the driver sends things, but I do know for a fact that it sends out a standard CR.
I guess my PIC is the Master PIC you are referring to in which case I'm using puts instead of printf, which already has the CR behind it. But the driver is receiving the data I send out no problem, it's my PIC receiving the data the driver is sending out that is giving me issues. _________________ KMoe
~We the willing, led by the unknowing, are doing the impossible for the ungrateful. We have done so much, for so long, with so little. We are now qualified to do anything with nothing -M. Theresa |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 21, 2008 7:03 pm |
|
|
Don't put any delays before you call get_string(). In your original code,
you have a 500 us delay after you send the command to start sending
the data. That delay can cause you to miss incoming characters if
more than 3 of them come in during that time. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|