|
|
View previous topic :: View next topic |
Author |
Message |
Labjac
Joined: 15 Dec 2011 Posts: 20
|
Reading serial data |
Posted: Fri Dec 23, 2011 8:04 am |
|
|
Hallo
I'm trying to read a serial string and then test is, if it's a cetain value I would like to do something, but I'm not sure where I'm going wrong, Firstly the compiler gives me an error if I try to test c please have a look at the program and let me know where I'm going wrong.
Secondly, I want to read a serial string that's about 20char long, how can I do this, the only option I can see is %c which is a single character.
Code: |
#include <16F690.h>
#fuses intrc,nowdt,noprotect,put
#use delay(clock=8M)
#use rs232(baud=9600,xmit=PIN_B7,rcv=PIN_B5,bits=8,ERRORS)
#define In1 PIN_C0
#define Op1 PIN_C1
void main()
{
int16 value,valueana,ch;
value=0;
valueana=10;
setup_port_a(sAN2);
setup_adc( ADC_CLOCK_DIV_16 );
while(1)
{
char c;
Printf("Enter a character:");
delay_ms(1000);
c=getc();
Printf("The character is: %c\n\r",c);
delay_ms(1000);
If c=="2"
{
output_high(Op1);
delay_ms(100);
output_low(Op1);
Printf("Correct \n\r");
}
}
} |
|
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
Reading serial data |
Posted: Fri Dec 23, 2011 8:31 am |
|
|
First, in order to receive more than one character or byte you need a buffer.
Second, to avoid loss some bytes don't use delays waiting for the incoming data, the function getc() already do that automatically without the risk of losing bytes.
A better practice is to use an interrupt service routine, so while the processor is waiting for the incoming byte can do another thing like read a button, and you process the data when the data stream is finished.
To know when the stream is finished you can detect /r/n or any character below "space"(0x20). _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Fri Dec 23, 2011 8:41 am |
|
|
%c, is for _outputting_ a character. Not for reading it.
Learn to use the code buttons for posting here, and indent your code (makes it easier for _everyone_.)...
Code: |
#include <16F690.h>
#fuses intrc,nowdt,noprotect,put
#use delay(clock=8M)
#use rs232(baud=9600,xmit=PIN_B7,rcv=PIN_B5,bits=8,ERRORS)
#include <input.c> //gives the get_string function
#define In1 PIN_C0
#define Op1 PIN_C1
void main(void) {
char cbuffer[24];
char look_for[]="Word";
setup_port_a(sAN2);
setup_adc( ADC_CLOCK_DIV_16 );
do { //This avoids compiler error message from while
Printf("Enter a string:");
//Don't delay - start looking for string ASAP
get_string(cbuffer,24);
Printf("The string is: %s\n\r",cbuffer);
delay_ms(1000);
If (strcmp(cbuffer,look_for,strlen(look_for)==0) {
output_high(Op1);
delay_ms(100);
output_low(Op1);
Printf("Correct \n\r");
}
}
}
|
You need the brackets in your if test.
This will accept a string of characters, and trigger the 'correct' message, if it begins with 'Word'.
You really need to get a basic C primer. The forum is for problems about CCS C specific issues, not basic 'C'.
Best Wishes |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Dec 23, 2011 11:31 am |
|
|
If all he is looking for is a singe byte he really doesn't need a buffer or ISR so the code he has should work if he removes the delays. Getc() will wait, possibly forever, for a Start bit, capture the next 8 bits as data and check that the 9th bit is a Stop bit.
Does the printf() statement report the correct character? Is the problem that "2" is a null terminated string and he should use '2' instead? I would try the ASCII code 0x33 in the comparison.
Once you get the single byte to work and you want to move on to 20 character strings you should look at buffers and Interrupt Service Routines. The ISR will read one character at a time into the buffer, usually until a terminator like <CRLF> is seen when it sets a flag. When main() sees the flag set it checks the buffer for the matching string. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Fri Dec 23, 2011 2:53 pm |
|
|
However the reason his code fails to compile is the missing brackets (as I said in my earlier post). At least with these he should start forward.
Best Wishes |
|
|
Labjac
Joined: 15 Dec 2011 Posts: 20
|
|
Posted: Sat Dec 24, 2011 2:23 am |
|
|
Hi
Telmah even the code you gave me failed to compile, not sure if it's something I'm doing wrong, and if it got something to do with my compiler.
do { //This avoids compiler error message from while
Printf("Enter a string:");
//Don't delay - start looking for string ASAP
get_string(cbuffer,24);
Printf("The string is: %s\n\r",cbuffer);
delay_ms(1000);
If (strcmp(cbuffer,look_for,strlen(look_for)==0) (Compile error). {
output_high(Op1);
delay_ms(100);
output_low(Op1);
Printf("Correct \n\r");
}
}
}
I did add the input.c file but the error I get it the following:
\main.c" Line 27(36,42): Expecting a close paren
Background is a bit more toward structured text programming and some VB so yes, still learning new syntax as I go along, but code it more or less the same.
SherpaDoug the previous code did Printf the correct character, but is went throught the loop twice everytime, but as you said I think it got to do with the delay, will remove it from the code and see what happens.
Thanks. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Sat Dec 24, 2011 4:33 am |
|
|
The compiler is telling you exactly what is wrong. I missed the last close parenthesis on the if statement.
Best Wishes |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Sat Dec 24, 2011 7:04 am |
|
|
If printf() is giving the correct character, I think your original problem is that
If c=="2"
compares c to a null terminated string containing 2 and a null. Try comparing to ASCII code 0x33 or I think '2' should also work (single quotes, not double quotes). _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Sat Dec 24, 2011 10:36 am |
|
|
The reason it won't compile, is that 'IF', _needs_ brackets.
This has been pointed out already.
The correct syntax, is:
Best Wishes |
|
|
|
|
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
|