Hi,
I'm still having trouble with getting the read callback to fire. I've verified
(via Realterm) that when I write my values to the file descriptor, byes are
received on the port. So I know traffic is going back and forth, but I can't
seem to get libev to detect a read on the file descriptor. Thanks.
#define EV_COMPAT3 0
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <ev.h>
#include <errno.h>
ev_io iow;
ev_io ior;
void somecallbackwrite(ev_loop *loop, ev_io *iowatch, int revents)
{
char buffer[10];
char c;
int i;
int count;
memset(&buffer, 0, sizeof(buffer));
buffer[0] = 0x55;
buffer[1] = 0xAA;
buffer[2] = 0xFF;
buffer[3] = 0x02;
printf("somecallbackwrite\n");
i = write(iowatch->fd, &buffer, 4);
printf("write(%d) = %d\n", iowatch->fd, i);
ev_io_stop(loop, iowatch); // stop the write watcher
ev_io_start(loop, &ior); // start the read watcher
}
void somecallbackread(ev_loop *loop, ev_io *iowatch, int revents)
{
char buffer[10];
char c;
int i = 1;
int count;
while (i > 0) {
i = read(iowatch->fd, &c, 1);
count += i;
}
printf("write(%d) = %d\n", iowatch->fd, i);
ev_io_stop(loop, iowatch); // stop the read watcher
ev_io_start(loop, &iow); // start the write watcher
}
main()
{
ev_loop *mainloop;
int fd;
fd = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd >= 0) {
mainloop = ev_default_loop(EVFLAG_AUTO);
ev_io_init(&iow, somecallbackwrite, fd, EV_WRITE);
ev_io_init(&ior, somecallbackread, fd, EV_READ);
ev_io_start(mainloop, &iow);
ev_io_start(mainloop, &ior);
ev_run(mainloop, 0);
close(fd);
} else {
printf("errno = %d\n", errno);
}
}
-----Original Message-----
From: libev [mailto:[email protected]] On Behalf
Of Thilo Schulz
Sent: Monday, December 14, 2015 4:52 PM
To: [email protected]
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
[email protected]
http://lists.schmorp.de/mailman/listinfo/libev
_______________________________________________
libev mailing list
[email protected]
http://lists.schmorp.de/mailman/listinfo/libev