GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Aug-2006, 00:42
a3.charles a3.charles is offline
New Member
 
Join Date: Jul 2006
Posts: 17
a3.charles is on a distinguished road

Serial Port Communication


Hi,

I'm currently attempting to access a pheripheral via a serial port. My goal is to be able to manage the data coming in but I have hit a road bump. I am working on a Linux OS and am obviously writing a C program to access the data on the serial port. The problem I am having is as follows.

The device sends streams of data with a starting STX char, followed by a header character(F##) and ends the stream with an ending char ETX. The data is sent at a rate of 38400 with 8N1 (one stop bit). For example the device might send:

FF01 a b c d
F00 Hello Andrew my name is John. What is your name1?
F00 Hello Andrew my name is John. What is your name2?
F00 Hello Andrew my name is John. What is your name3?
F00 Hello Andrew my name is John. What is your name4?
F00 Hello Andrew my name is John. What is your name5?
F00 Hello Andrew my name is John. What is your name6?
F00 Hello Andrew my name is John. What is your name7?
F01 a b c d
F00 Hello Andrew my name is John. What is your name8?
F01 a b c d
F00 Hello Andrew my name is John. What is your name9?
F01 a b c d
F00 Hello Andrew my name is John. What is your name10?
F01 a b c d
F00 Hello Andrew my name is John. What is your name11?
F01 a b c d
F00 Hello Andrew my name is John. What is your name12?
F01 a b c d
F00 Hello Andrew my name is John. What is your name13?

My code will attempt to access only the data with the F00 header, unfortuneatly this is all it saves:
Hello Andrew my name is John. What is your name2?
Hello Andrew my name is John. What is your name4?
Hello Andrew my name is John. What is your name5?
Hello Andrew my name is John. What is your name6?
Hello Andrew my name is John. What is your name7?
0 Hello Andrew my name is John. What is your name8?
1 a b c d
Hello Andrew my name is John. What is your name9?
Hello Andrew my name is John. What is your name10?
Hello Andrew my name is John. What is your name11?
Hello Andrew my name is John. What is your name12?
Missing a line at the start and end

THe data retrieved will vary each time the program is run.

It stores most streams correctly but on occasion the incorrect data is stored.

I'm am configure my serial port as shown below:

CPP / C++ / C Code:
/*Global Variables*/

struct termios oldset,newset;	/*Storage of terminal settings*/
int fd;	/*File descriptor, character*/

/*Use non-canonical input processing*/
/*set serial port settings*/
void configure_serialport(void)
{
  /*Open device*/
  if((fd = open(INPUTDEVICE, O_RDWR| O_NOCTTY | O_NDELAY)) < 0)
    fprintf(stderr, INPUTDEVICE " failed to open in %s.\n",__FUNCTION__);

  /*Save serial port's old settings*/
  tcgetattr(fd,&oldset);
  
  /*Set all parameters of new settings to zero*/
  bzero(&newset, sizeof(newset));

  /*Set parameters*/
  newset.c_cflag |= (BAUDRATE | CS8 | CLOCAL | CREAD);
  newset.c_iflag |= (INPCK | ISTRIP);
  newset.c_oflag &= ~OPOST;
  newset.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);	/*Set local flag to zero for non-conanical mode*/
  
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &newset);
}

What will happen is that the code will search for the STX {'\x02'} character followed by the F00 header then once this is found the stream from the serial port is read into a buffer.

char *read_line(char *filename)

{

  /*Variables*/
  int i=0;	/*Index*/
  char ch='\0';	/*Temporary Storage*/

  /*Clear buffer*/
  line[0] = '\0';

  /*Read in line*/
  do{
    if((ch = getch(0, 0)) != ETX)
      if(i < SENTENCE_LENGTH-1 && ch !='\0')
        line[i++]= ch;

  }while(ch != ETX);
  
  line[i] = '\0';

  return line;

}

Where getch() is:
int getch(char *terminal, int time, int no_char)

{

  /*Variables*/
  char ch = '\0';

  /*Set blocking by time/number of characters*/
  newset.c_cc[VTIME] = time;
  newset.c_cc[VMIN] = no_char;

  /*Implement new settings*/
  tcsetattr(fd, TCSANOW, &newset);
  
  /*Read in the character*/
  if(read(fd, &ch, 1)!=0){
    return ch;  
  }else{
    return '\0';
  }  
}


Once stored into a buffer the stream is read directly to the storage file. The reason i'm storing the stream into a buffer and not reading directly to a file is I need to extract numbers from the stream to do some calculations.

I realise this is a very broad question but if anyone could lend me any ideas to why i occasionaly miss a STX character that would be great?

Cheers
Andrew Charles
Last edited by LuciWiz : 04-Aug-2006 at 10:49. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read from serial port in C eng_girl C Programming Language 3 25-Feb-2008 00:51
Serial port problem chirayu MS Visual C++ / MFC Forum 0 07-Jul-2005 04:52
Problem to read on a serial port collinm C Programming Language 2 30-Mar-2005 02:42
Problem with string and serial port collinm C Programming Language 13 25-Mar-2005 08:39
Serial Port communication in C or C++ dmkst56 C++ Forum 0 23-Mar-2005 21:18

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:00.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.