[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1297
  • Last Modified:

Help parsing text from serial port under linux!

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
jconde
Asked:
jconde
  • 4
1 Solution
 
jcondeAuthor Commented:
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
 
rbrCommented:
Use

fp=fdopen (fd,"rb");


After this command you can use

fgets with the FILE * fp.

fdopen redirects a descriptor to a FILE *.
0
 
jcondeAuthor Commented:
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
Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

 
rakeshnCommented:
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
 
jcondeAuthor Commented:
Answer accepted
0
 
jcondeAuthor Commented:
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

Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now