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

Getting a string from the serial port

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Julian A.
Guest







Getting a string from the serial port
PostPosted: Thu Jun 10, 2004 6:52 am     Reply with quote

Hello everybody !

I'm a beginner who want to send a string of characters (a genome actually) from a PC (with linux) to a robot (controlled by a PIC16f877) by using the serial port.

So here is my code.
On the PIC side with the CCS compilator, i use gets() to get the string of characters (the new genome) from the PC:

Code:
#case
#include <16F877.h>
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)

signed int8 Genome[10]={10,7,21,242,36,7,15,18,4,7}; //initial genome
char tempgenome[10]; //the new genome will be put here

void get_new_genome() {
  unsigned int8 i;
 
  gets(tempgenome); //getting the string (new genome) from the PC
  for(i=0;i<10;i++){
    Genome[i] = (signed int)tempgenome[i]; //substitue old with new genome
  }

void main {
   while(TRUE) {
      if (kbhit()) {
         char CharReceived;
         CharReceived=getc();
         switch (CharReceived) {
                 case 'g': printf("getting new genome"); 
                           get_new_genome();
                 break;
                 default: printf("bad cmd\n\r");
                 break; }
        }
   }
}

And on the PC side with a gcc compilator, I send the command 'g' and the string (new genome) to the PIC by using the primitive write():
Code:
#include <stdio.h>   /* Standard input/output definitions */
#include <stdlib.h>
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <iostream.h>

int write_string(void)
{
  int fd; /* File descriptor for the port */
  const char genome[] = { 168, 21, 242, 190, 155, 24, 5, 165, 23, 12}; //genome to be send
  FILE *fp;
 
  //testing opening of serial port
  fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1)
    {
      perror("open_port: Unable to open /dev/ttyS0 - ");
    }
  else
    {
      fcntl(fd, F_SETFL, 0);
      //write the command 'g' and the new genome to the PIC
      write(fd, "g", 1); 
      write(fd, genome, 10);
      return (fd);
    }
}

int main(int argc, char **argv)
{
  write_string();
}

And the problem is when I run this program on the PC side, the PIC only display "getting new genome" to a terminal and freeze. I thought it may be because the function 'gets()' wait for someone typing <enter> to gets the string, so I tried to write an <enter> by putting :
Code:
write(fd,"\n",1);
after the writing of the genome, but it didn't solve the problem...

Am I doing something wrong here ? Thanks a lot for your help ! Very Happy

Have a nice day,
Julian.
Ttelmah
Guest







PostPosted: Thu Jun 10, 2004 7:05 am     Reply with quote

Though it probably should not cause your problem, remember that 'strings' in C, are null terminated. So a ten character string, will require eleven characters of storage (gets adds the extra character). Hence the storage area you have allocated will be overflowing, and this could lead to any sort of problem...

Best Wishes
Guest








PostPosted: Thu Jun 10, 2004 10:01 am     Reply with quote

Ttelmah wrote:
Though it probably should not cause your problem, remember that 'strings' in C, are null terminated. So a ten character string, will require eleven characters of storage (gets adds the extra character). Hence the storage area you have allocated will be overflowing, and this could lead to any sort of problem...

Best Wishes

Thanks for answering Ttelmah ! Very Happy

Actually it seems that there is indeed a sort of storage overflow, because one strange thing is if I send data to the serial port with a program like this...
Code:
write(fd,"g",1); 
write(fd,genome,10);
write(fd,"\n\r",2);

... it freeze after the sending of the 'g', but if I type the 'g' with the keyboard and then execute the program with only the 2 last lines :
Code:
write(fd,genome, 10);
write(fd,"\n\r",2);

... it works well...

Is there a difference between sending the character 'g' to the serial port with
Code:
write(fd,"g",1)
and manually typing 'g' with the keyboard Question I'm a little bit lost now... Confused
Julian A.
Guest







PostPosted: Thu Jun 10, 2004 11:55 am     Reply with quote

Sorry to make the questions & the answers, but I think my problem is in fact that I can send only a limited number of bytes to the serial port before it freeze the connection. The bytes are actually send trough the serial port, but the PIC doesn't seem able to get them after 2 or 3 write() consecutively.
Is it normal (flow control problem?) ? How can I remediate to this ?

Thanks very much,
Julian.
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Thu Jun 10, 2004 2:06 pm     Reply with quote

It may be the problem is the PIC receives the "g" but it has then to print it got it with printf("getting new genome"); This printf consumes PIC cpu cycles so the PIC is busy when the next char arrives from the PC. Again this is the nature of asynchronous communications. What would work is for you either to not respond to the PC until the whole sentence is received and the PC is then waiting for a response or to bite the bullet and learn how to write a RS232 receive isr and circular buffer.

I'll guess the 3 chars you get are the "g" and two in the hardware buffer of the PIC UART the fourth char arrives and has nowhere to go since the PIC is still busy with your printf.
Ttelmah
Guest







PostPosted: Thu Jun 10, 2004 3:18 pm     Reply with quote

Douglas Kennedy wrote:
It may be the problem is the PIC receives the "g" but it has then to print it got it with printf("getting new genome"); This printf consumes PIC cpu cycles so the PIC is busy when the next char arrives from the PC. Again this is the nature of asynchronous communications. What would work is for you either to not respond to the PC until the whole sentence is received and the PC is then waiting for a response or to bite the bullet and learn how to write a RS232 receive isr and circular buffer.

I'll guess the 3 chars you get are the "g" and two in the hardware buffer of the PIC UART the fourth char arrives and has nowhere to go since the PIC is still busy with your printf.

I would think this is very likely to be the problem.
Seriously, I'd suggest the original poster switches to using the interrupt driven buffered serial I/O, as given in the example program ex_sisr. This is a better solution all round, since the interrupt routine will still work while the message is being printed, and if the buffer is a little larger than the 'expected' number of characters, and perhaps modified to overwrite 'old' characters in the event of an overflow, there will be no hangup.

Best Wishes
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