|
|
View previous topic :: View next topic |
Author |
Message |
Scottl
Joined: 21 Feb 2005 Posts: 25
|
fgets ? |
Posted: Mon Oct 22, 2007 4:58 pm |
|
|
I am using a PIC12F683 and trying to get the following code to work! Can someone tell what I am doing wrong. Anything I send to the pic it displays Test 88 never Test 6917.
#use rs232(baud=9600,parity=N,xmit=PIN_A1,rcv=PIN_A0,bits=8,STREAM=COM_A)
#use rs232(baud=9600,parity=N,xmit=PIN_A4,rcv=PIN_A5,bits=8,STREAM=COM_B)
Code: |
void main()
{
char c[4];
while(1)
{
fgets (c, COM_A);
if (c == 6917)
{
fputs(c,COM_B);
fprintf(COM_A, "Test 6917");
}
if (c == 88)
{
fprintf(COM_A, "Test 88");
}
}
|
Thanks in advance,
Scott |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Mon Oct 22, 2007 5:11 pm |
|
|
First of all, this part does not have a hardware uart on board. If you are going to be inputing data to the PIC I would strongly suggest you use one with the hardware.
Next, you have defined your char variable 'c' to hold four bytes. You are trying to store four characters in it but you need a null character to specify the end of the string, ie a '0' (zero). You need to declare it: char c[5] to hold four characters plus the NULL.
It's fairly easy to use software uarts to transmit with but rather tricky to use them to receive data.
Search the forums on software uart and see what challenges others have stumbled upon.
Ronald |
|
|
Scottl
Joined: 21 Feb 2005 Posts: 25
|
|
Posted: Mon Oct 22, 2007 6:25 pm |
|
|
I have setup a test and everything works fine! All data sent comes back exact!
Code: |
fgets (c, COM_A);
fprintf(COM_A, "%s", c);
|
All data is sent back perfectly!
I think the problem is with the following:
Code: |
if (c==88)
{
Do This!
}else{
Do This! I type 88 but it always does this!
}
|
Do you see anything wrong with the my if statement?
Scott |
|
|
Ttelmah Guest
|
|
Posted: Tue Oct 23, 2007 4:16 am |
|
|
If you type '88', you send the two characters '8', and '8'. The single character, with the 'value' '88', is the letter 'X' (upper case).
Best Wishes |
|
|
Scottl
Joined: 21 Feb 2005 Posts: 25
|
|
Posted: Tue Oct 23, 2007 7:44 am |
|
|
Thanks Ttelmah!
This gets me everytime! Everything seems to be working fine. |
|
|
Ttelmah Guest
|
|
Posted: Tue Oct 23, 2007 8:44 am |
|
|
Yes, it is slightly confusing, especially if you are 'used' to languages, that allow a direct string comparison, and particularly if you are not 'used' to thinking about the numeric values that characters represent. After about the 500th error of this sort, you will find that you quite suddenly start to think in the right terms....
Best Wishes |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Oct 25, 2007 1:45 am |
|
|
Try the following:-
Asign a big enough buffer to allow for all incomming data
If you are sure you are only going to be recieving 4 bytes then DON't forget the null terminator as mentioned above so it would be c[5] as gets WILL put it on the end for you.
read up on strcmp and strstr. They return 0 for an EXACT match e.g.
"6917" == "6917" not "6917123"
char c[128];
while(1)
{
fgets (c, COM_A);
if (strcmp(c, "6917") == 0)
{
fputs(c,COM_B);
fprintf(COM_A, "Test 6917");
}
if (strcmp(c, "88") == 0)
{
fprintf(COM_A, "Test 88");
}
} |
|
|
|
|
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
|