ISO Example for reading/writing uart/rs-232

Chris Galas ChrisG at nytec.com
Tue Dec 15 06:12:04 CET 2015


Thanks for the Write example!

So, for a Read example, I would setup a different callback (or is it advantageous to use the same callback as the Write) and read until the buffer is drained (see below)? Would it be required  to issue a ev_io_stop to halt the callback? Probably not. Another scenario: Could I put the read callback into a separate thread than the write callback (if say we are using a rs-485 asynchronous 4 wire uart)? Thanks!

void readcallback(ev_loop *loop, ev_io *ioread, int revents) {
        char c;
        int I = 1;
        while (I > 0)
                I = read(ioread->fd, c, 1);
        // ev_io_stop(loop, ioread);
}

-----Original Message-----
From: libev [mailto:libev-bounces+chrisg=nytec.com at lists.schmorp.de] On Behalf Of Thilo Schulz
Sent: Monday, December 14, 2015 4:52 PM
To: libev at lists.schmorp.de
Subject: Re: ISO Example for reading/writing uart/rs-232

Hi,

wrote last source off the top of my head and the declaration of fd got lost while reshuffeling. This should work for real.

#define EV_COMPAT3 0

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <ev.h>

void somecallback(ev_loop *loop, ev_io *iowatch, int revents) {
        write(iowatch->fd, "booya", 5);
        ev_io_stop(loop, iowatch);
}

main()
{
        ev_io iow;
        ev_loop *mainloop;
        int fd;

        fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);

        if(fd >= 0)
        {
                mainloop = ev_default_loop(EVFLAG_AUTO);
                ev_io_init(&iow, somecallback, fd, EV_WRITE);
                ev_io_start(mainloop, &iow);
                ev_run(mainloop, 0);
                close(fd);
        }
}


_______________________________________________
libev mailing list
libev at lists.schmorp.de
http://lists.schmorp.de/mailman/listinfo/libev


More information about the libev mailing list