View previous topic :: View next topic |
Author |
Message |
iw2nzm
Joined: 23 Feb 2007 Posts: 55
|
fgets issue |
Posted: Thu Apr 19, 2007 5:27 am |
|
|
Hi!
I'm using a sw UART to receive data from a barcode reader. I configured it to send strings + CR/LF (verified via a serial terminal).
I want to store into the string 'text' the data received. So I wrote this code:
Code: | #include <18F4520.h>
#use delay(clock=6250000,RESTART_WDT)
#use rs232(baud=9600,parity=N,xmit=PIN_C1,rcv=PIN_B2,bits=8,stream=BARCODE,ERRORS,TIMEOUT=10,restart_wdt)
#define MAX_LENGHT 16
int1 bar = FALSE;
char text[MAX_LENGHT + 1] = "";
#int_EXT2
void EXT2_isr(void) {
fgets(text, BARCODE);
bar = TRUE;
output_toggle(LED3);
}
void main() {
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_wdt(WDT_ON);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
ext_int_edge(2, H_TO_L);
enable_interrupts(INT_EXT2);
enable_interrupts(GLOBAL);
setup_oscillator(FALSE);
for (;;) {
if (bar) {
bar = FALSE;
output_toggle(LED2);
}
restart_wdt();
}
} |
LED3 toggles every time the barcode sends a string, but LED2 doesn't toggle at all. I'd like to avoid to use fgetc to catch each char...
Where is my fault?
Thanks
Marco / iw2nzm |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Thu Apr 19, 2007 7:38 am |
|
|
Take what I have to offer with a grain of salt, my experience isn't that extensive, but I have RS232 receive functionality in all of my projects and it works. So I will offer my $0.02 worth and it may help... and it will probably get one of the pros to chime in with the correct answer...
Quote: | I'd like to avoid to use fgetc to catch each char |
Why?
I've never seen a serial receive handler that receives a complete string. It's always done character by character. Based upon that observation... I don't think it's possible? (If it is, I'd like to see it done. That would be interesting.)
I won't venture why the LED2 isn't toggling as you expect. One of the pros may be able to?
Use the SISR example. All the code is written. It only takes a few minutes to tweak it, if necessary, to a particular application. It works. Try it. I think you'll like it.
Good luck,
John[/code] |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Apr 19, 2007 7:47 am |
|
|
I suspect the PIC is spending 99% of its time in the ISR waiting for the gets() to complete. Can you turn on a LED when you are in the ISR so you can judge the time spent? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
iw2nzm
Joined: 23 Feb 2007 Posts: 55
|
|
Posted: Thu Apr 19, 2007 11:10 am |
|
|
Thanks to SherpaDoug and jecotrell.
I rewrote the code using fgetc and buffer: now it works fine...
I asked to use fgets because it would be more "elegant" to write... but before it didn't work, now it does...
Marco / iw2nzm |
|
|
|