Solved

Help parsing text from serial port under linux!

Posted on 2000-05-05
6
1,259 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

785 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