Solved

Help parsing text from serial port under linux!

Posted on 2000-05-05
6
1,242 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Expert Comment

by:rakeshn
Comment Utility
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
Comment Utility
Answer accepted
0
 
LVL 7

Author Comment

by:jconde
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now