Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help parsing text from serial port under linux!

Posted on 2000-05-05
6
Medium Priority
?
1,295 Views
Last Modified: 2008-02-20
Hi, I have a news feed that is delivered straight to one of the serial ports of my linux box.

all the news come in the following format:

ZCZC (start of note)
DATE
SOME TEXT
TITLE
NEWS BODY
SOME OTHER TEXT
NNNN  (end of note)
.
.
.
ZCZC
NEXT NEWS...


I need to be able to parse when ZCZC and NNNN come from the serial port, and the problem I have is that I don't receive these complete words in one buffer...

I.E.  On one buffer I might receive Z and on the second one I will receive CZC that when concatenated they will form the ZCZC word (same for NNNN)...I might also receive a buffer something like: Buffer1: NNN  Buffer2: N ZC Buffer3: ZC and so on...

Can someone please help me figure out how to do this in ANSI C?

I am not an expert C programmer :(

Below is the code I use for reading from the serial port.

Thank you very much,

Jorge


int main(argc, argv)
int argc;
char *argv[];
{
  int fd,res;
  struct termios viejo,nuevo;
  char buf[255];
  fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd <0)
    {
      exit(-1);
    }

  tcgetattr(fd,&viejo);
  bzero(&nuevo, sizeof(nuevo));
  nuevo.c_cflag = 1200 | CRTSCTS | CS8 | CLOCAL | CREAD;
  nuevo.c_iflag = IGNPAR;
  nuevo.c_oflag = 0;
  nuevo.c_lflag = 0;

  nuevo.c_cc[VTIME]    = 0;
  nuevo.c_cc[VMIN]     = 5;

  tcflush(fd, TCIFLUSH);
  tcsetattr(fd,TCSANOW,&nuevo);

  fcntl(fd, F_SETFL, FNDELAY);
  for (;;)
    {
      if ((res = read(fd,buf,sizeof(buf) - 1)) > 0) {
      buf[res]=0;
/*THIS IS WHERE I PRINT TO STDERR WHAT COMES IN FROM THE SERIAL PORT*/
      fprintf (stderr,"%s",buf);
      }
    }
  tcsetattr(fd,TCSANOW,&viejo);
  close(fd);
}
0
Comment
Question by:jconde
  • 4
6 Comments
 
LVL 7

Author Comment

by:jconde
ID: 2780035
In not so many words, what I want is a function that will work just as fgets does but instead of reading from a file stream, that reads from the serial port...

Read all characters untill a CR LF (\n) is found!

Thanks again,

Jorge
0
 
LVL 10

Accepted Solution

by:
rbr earned 300 total points
ID: 2780188
Use

fp=fdopen (fd,"rb");


After this command you can use

fgets with the FILE * fp.

fdopen redirects a descriptor to a FILE *.
0
 
LVL 7

Author Comment

by:jconde
ID: 2783608
rbr, I will grade the question on monday!

I had a problem with my news receiver today and obviously couldn't try that one out!

thanks,

jorge
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:rakeshn
ID: 2849002
You can use a single circular buffer instead...
Receive the chars from serial port and keep adding them to the buffer.If the last location of the buffer is reached then start filling the next char from the start of the buffer..i.e use a circular buffer...After that get the location of ZCZC & NNNN.The date between these two pointers is the data u want...here also u will have to check whether the pointer for ZCZC < NNNN or > NNNN and program u r logic accordingly.I hope u r using this routine to read telegrams which are in ISTF format.I have done similar softwares and this circular buffer concept works well. A buffer of 4K should work for this as this is the maximum size a message can go ..
Best of luck
0
 
LVL 7

Author Comment

by:jconde
ID: 2888040
Answer accepted
0
 
LVL 7

Author Comment

by:jconde
ID: 2888041
Hi,

That didn't seem to work out!...Somehow, my stream is always empty!

It was a great idea though.

I had to write a Gets function of my own.

Thanks for your input!

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question