Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help parsing text from serial port under linux!

Posted on 2000-05-05
6
Medium Priority
?
1,290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

671 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