This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: receive data from a interrupt serial device viacallbacks


On Sat, 2005-02-19 at 13:45 +0100, Stefan Rosinger wrote:
> Hi!
> 
> I have a question about receiving characters over an interrupt serial port:
> 
> Whats the easiest approach for receiving 5 to 10 bytes over this serial
> port.
> How can i read the char from the port. i tried it with cyg_io_read() but i
> am not sure whats happening with my received char
> because of the (chan->callback->rcv_char) thing.
> Can I buffer my characters in my application. If yes can somebody give me an
> example how i can do some buffering in my application?
> Do I have to change the serial driver to support block mode reads?
> 
> I know, lot of questions, but any help would be nice!

This is actually quite simple - just use standard I/O functions
on the serial device.  This will get you raw, blocking access to
the port (the default).  e.g.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define PORT "/dev/ser0"

main()
{
  char buf[10];
  int fd, res;

  if ((fd = open(PORT, O_RDWR)) < 0) {
    fprintf(stderr, "Can't open port: %s\n", strerror(errno));
    exit(1);
  }
  while (true) {
    res = read(fd, buf, sizeof(buf));
    if (res < 0) {
      fprintf(stderr, "Can't read from port: %s\n", strerror(errno));
      exit(1);
    }
    // Do something with data
  }
}

Note: I just typed this (no testing), so forgive any little typos.

Note 2: You have to use the interrupt driven serial driver for this
to make sense.  This would typically be different from your diagnostic
I/O channel.

Note 3: Even while your thread is manipulating the data that has been
read, the serial driver will be reading & buffering new data (via
interrupts).  Thus, you should not have to worry about extra buffering
techniques in your code (as long as you consume the data at least as
fast as it can fill the serial drivers own buffers)

-- 
------------------------------------------------------------
Gary Thomas                 |  Consulting for the
MLB Associates              |    Embedded world
------------------------------------------------------------


-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]