CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Trouble with gets() using RS232

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
AdamWebber



Joined: 18 Jun 2014
Posts: 13

View user's profile Send private message

Trouble with gets() using RS232
PostPosted: Thu Jun 26, 2014 1:30 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jun 26, 2014 2:07 pm     Reply with quote

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: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Jun 26, 2014 2:18 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 27, 2014 1:20 pm     Reply with quote

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. Rolling Eyes
Thanks again.
hhuurrkkaann



Joined: 08 Jan 2013
Posts: 14

View user's profile Send private message

PostPosted: Thu Apr 28, 2016 7:01 am     Reply with quote

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: 19538

View user's profile Send private message

PostPosted: Thu Apr 28, 2016 7:50 am     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Fri Apr 29, 2016 11:39 am     Reply with quote

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....
Very Happy Very Happy Very Happy Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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