|
|
View previous topic :: View next topic |
Author |
Message |
Masa Guest
|
I need help with code which reads characters from rs232? |
Posted: Fri Dec 17, 2004 5:05 am |
|
|
Hi, sorry my bad english
I try to do program which send string to computer e.g( "$52"). When computer get string, it send back some information e.g( -12.4C). My problem is that I can't read that data and put it right format. I have tested interface and that is okay. Only problem is code.
Psuedo code:
char result[10];
int i=0;
printf("$52"): // send string to computer
while( !kbhit() ); // waits computer
while( kbhit){ //try to write data to 'result'
result[i]=getch();
i++;
}
What goes wrong? |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1636 Location: Perth, Australia
|
|
Posted: Fri Dec 17, 2004 5:46 am |
|
|
the pseudo code looks ok. You have not decribed the problem enough to be able to determine the possible problem. Can you post an example of your program the demonstrates the problem including the fuse statements, #use statements and which processor you are using. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Masa Guest
|
Reply |
Posted: Fri Dec 17, 2004 7:15 am |
|
|
Yes,
prosessor is pic16f877
clock=12MHz
I test different baudrates, 1200,2400,9600 but problem not solved. I just wondering is this loop too slow to read characters:
while( kbhit){ //try to write data to 'result'
result[i]=getch();
i++;
}
printf(result)
because, when I test code with terminal program, first I can see $52, that is okay and terminal wait until I send packet. My test packet was 1234. But now I can read only 12 on terminal. |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1636 Location: Perth, Australia
|
|
Posted: Fri Dec 17, 2004 7:28 am |
|
|
you have not posted enough code to see where the problem is. For example, I cannot tell if you are using the harware UART or the Software UART function.
Instead of the loop being too slow, it could be too fast. For example, if the #use RS232 statement is enabling the hardware UART then the program could service getch() so fast that after reading in the second character kbhit() returns false because the third character is still being serialised into the UART. When this happens you will drop out of the while loop. Another problem is that you code does not write a 0 at the end of the string in result. The printf statement could end up printing garbage. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Ttelmah Guest
|
Re: Reply |
Posted: Fri Dec 17, 2004 9:17 am |
|
|
Masa wrote: | Yes,
prosessor is pic16f877
clock=12MHz
I test different baudrates, 1200,2400,9600 but problem not solved. I just wondering is this loop too slow to read characters:
while( kbhit){ //try to write data to 'result'
result[i]=getch();
i++;
}
printf(result)
because, when I test code with terminal program, first I can see $52, that is okay and terminal wait until I send packet. My test packet was 1234. But now I can read only 12 on terminal. |
As this code stands, this is a reasonable response...
The code will arrive at the 'while' line, and unless a character has already arrived, will drop through, and print what has been received. You need to either identify the 'end' of the incoming character string, with a 'marker', 'count', or implement a 'timeout', so that the routine only proceeds if characters do not arrive for some time.
Assuming this is not the 'real' code, and only another piece of psuedo code, the other thing that could apply, is that you are 'falling off the end' of the code. If (for instance), you generate:
printf("Test");
at the end of 'main', using the hardware UART, you will see:
'Te' displayed on the PC. The reason in this case, is that unless you wait for the charcters to be actually _sent_, when the processor gets to the end of main, it is put to sleep, and any characters waiting in the hardware UART, are not sent.
Post the actual code, rather than 'snippets', or 'psuedo code' sections, and somebody will probably be able to tell you what is happening.
Best Wishes |
|
|
Masa Guest
|
code |
Posted: Fri Dec 17, 2004 12:05 pm |
|
|
I wrote new code, because old one was so long.
from headerfile:
#include <16F877A.h>
#device adc=8
#use delay(clock=12000000)
#fuses HS,NOWDT
real code:
#include "D:\\test_rcv.h"
#use rs232(baud=1200, xmit=PIN_c6, rcv=PIN_c7)
char result[10];
void main() {
int i=0;
setup_adc_ports(ANALOG_RA3_RA2_REF);
setup_adc(ADC_CLOCK_DIV_32);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
//setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
SET_TRIS_D(0);
set_tris_b(0b11111101);
set_tris_c(0b10000000);
disable_interrupts(global);
while(1){
printf("$52");
while( !kbhit() ); // wait until computers send 1. character
while( kbhit() ){
result[i]=getc();
i++;
}
printf(result);
}
}
Terminal:
:$52
TX-->example<nul>
:exa$52exa$52
TX-->example<nul>
:
After that the prosessor do nothing, and terminal neither. I try to use get_string() from input.c, but that wasn't fix problem. Should I use interrupts? |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Fri Dec 17, 2004 1:02 pm |
|
|
Masa wrote:
Quote: |
After that the prosessor do nothing, and terminal neither. I try to use get_string() from input.c, but that wasn't fix problem. Should I use interrupts?
|
Yes, for example enable_interrupts(INT_RDA) if you are using the UART.
The way you determine if you are going to use the UART or not is implicit in the directive
#use rs232(baud=1200, xmit=PIN_c6, rcv=PIN_c7)
where if you select rcv=PIN_C7, the UART is enabled and the hardware
handle the incoming chars flagging INT_RDA interrupts.
If you select another rcv PIN different that PIN_C7 (in this processor) the UART is not
active and you must code a software handler to receive a char without interrupts.
Anyway I suggest to search in this forum all the info related RS232 and you will find
tons of code and examples to learn from.
Best wishes,
Humberto |
|
|
C#R# [] Guest
|
RS232 |
Posted: Sat Dec 18, 2004 5:24 pm |
|
|
Hi.. at short time ago, I wrote a code with RS232 and work very weel.. but at begining I had some problems...well...the problem is the watchdog.
The new getc() ( that�s I named getcW() ) stoped the problem..
int getcW ()
{
while ( !(*PIR1 & 0b00100000)) restart_wdt() ;
return (*RCREG);
}
// WATCH DOG... YOU ARE SO BAD...HEHEHE !!!!
humm...that�s good for me ..and I would like to be good for you too....
don�t forget :
#define PIR1 0x0C
#define RCREG 0x1A // for PIC16F871 !!! |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sat Dec 18, 2004 6:55 pm |
|
|
Code: | while( kbhit() ){
result[i]=getc();
i++;
}
printf(result);
|
Your going to fall out of this loop after you receive the first character. The PIC is pretty fast and it takes some time to receive the next char. Also, you need to tell the printf what kind of data you are printing. |
|
|
|
|
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
|