View previous topic :: View next topic |
Author |
Message |
AdamWebber
Joined: 18 Jun 2014 Posts: 13
|
Trouble with gets() using RS232 |
Posted: Thu Jun 26, 2014 1:30 pm |
|
|
I am having trouble receiving data using an FT232 converter to the Microcontroller. I am using a PIC16F1947 on the hardware TX1 and RX1 pins. Baud rate is 115200. Clock is 32 MHz internal.
My problem is that when the kbhit() goes true, I attempt to get the string and put it into an array. I only receive the first two bytes of the string. I know that the FT232 is working properly and the data is being sent. Every so often I can randomly receive more bytes but it usually only gives me two. Can someone tell me if my code is correct? I am also using interrupts 1 and 2 for mundane flag setting and counters.
Here's some code:
Code: | #FUSES NOCLKOUT
#FUSES PLL
#FUSES NOWDT
#FUSES INTRC_IO
#FUSES PUT
#FUSES NOMCLR
#FUSES PROTECT
#FUSES NOCPD
#FUSES BROWNOUT
#FUSES NOLVP
#FUSES NODEBUG
#FUSES NOWRT
#FUSES NOVCAP
#use delay(clock=32000000)
#use rs232(uart1,baud=115200,TIMEOUT=30,DISABLE_INTS)
char serialData[20];
while(1)
{
if (kbhit())
{
gets(serialData);
printf("\r\n%c",serialData[0]);
printf("%c",serialData[1]);
printf("%c",serialData[2]);
printf("%c",serialData[3]);
printf("%c",serialData[4]);
printf("%c",serialData[5]);
printf("%c",serialData[6]);
printf("%c",serialData[7]);
}
} |
The reason that I am displaying each individual byte in the array is so that I can see each byte on the computer. Otherwise I would just display the string. |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Thu Jun 26, 2014 2:07 pm |
|
|
Hi,
Your PIC UART is most likely being over-run, and locking up. Add 'Errors' to your #use 232 directive like this:
Quote: |
#use rs232(uart1,baud=115200,TIMEOUT=30,DISABLE_INTS, Errors)
|
You baud rate is awfully fast to be collecting data from the UART without an interrupt handler to do so. For reliable comms., you probably aren't going to be able to get away doing it this way. Also, 'Gets' is a risky function as it will halt the code until a complete string is received. It's better to receive character-by-character, and to stuff the data into an array for processing! See ex_sisr.c in the examples folder!
John |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Thu Jun 26, 2014 2:18 pm |
|
|
John's 100% right ! Use ex_sisr.c as an example of how to reliably get serial data into a PIC. I've used a minor version of it for a few years,running at 115K200 on both UARTS of an 18F46K22 and have NEVER missed a single byte of data.
CCS is great at supplying real,working examples of code snippets so take advantage of their code!!
hth
jay |
|
|
AdamWebber
Joined: 18 Jun 2014 Posts: 13
|
|
Posted: Fri Jun 27, 2014 1:20 pm |
|
|
Thanks for your help. I tried the ex-sisr.c per your suggestions and I kept getting the same results. After further delving into the problem, I found the problem was not with the microcontroller or the FT232 or the embedded code. I probed the signal from the computer with an oscilloscope and found that the signal being transmitted was the problem. It is very unstable after the first two bytes at any baud rate. So my problem lies somewhere in the PC side of the communication routine, not the PIC.
Thanks again. |
|
|
hhuurrkkaann
Joined: 08 Jan 2013 Posts: 14
|
|
Posted: Thu Apr 28, 2016 7:01 am |
|
|
how to get all string data which is coming from any device via rs 232...
as an example, "FR34HFSKLJIL3IJHIL" formatted string data is coming from a device and I want to show this string data on LCD.
Getc function only gets one charecter,
gets function waits for pressing enter but data is not coming from a computer so we do not have enter button and data is coming continously,,, |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Thu Apr 28, 2016 7:50 am |
|
|
Use EX_SISR.c
Then in your main for this:
Code: |
int c;
while (TRUE)
{
if (bkbhit())
{ //A character is in the input buffer
c=bgetc();
//What exactly you do is up to you, but this will go back to the
//top corner of the display when a line feed is received
if (c=='\n')
lcd_gotoxy(0,0);
else
lcd_putc(c); //send this character to the display otherwise
}
}
|
It's completely up to _you_ what you actually do, but this will automatically go back to the corner of the display when a line feed to seen. Otherwise you will need to handle what happens when you go off the right of the display, etc. etc... |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri Apr 29, 2016 11:39 am |
|
|
Mr. T is guiding you straight.
No responsible programmer tries to retrieve an entire sting with gets() -
if for no other reason than that it trashes main program execution in the case that a string never arrives. !!
i and others who do this for a living use variations on parsing of what is in the
serial buffer array and building the string as it arrives but never committing to a potential "hang" waiting for an EOL character.
The function may LOOK compelling because of it's simplicity-
but that is it's weakness too.
basically getstr() can leave your precious program -
"Waiting For Baudot" -
or
the watchdog to bark-
much to your chagrin and dismay....
|
|
|
|