【发布时间】:2013-09-02 13:22:39
【问题描述】:
我正在尝试使用 libevent 来管理嵌入式 Linux 设备和 pc 之间的串行通信。
libevent 的第一个问题。我在 eclipse 中创建了一个 C 项目,主要是创建一些事件,编译器可以:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <event.h>
#include "function_test.h"
....
int main(void) {
struct event ev_sighup; //reports that the user's terminal is disconnected
struct event ev_sigterm; //program termination
struct event ev_sigint; // program interrupt
int rv = 0;
/* Set up libevent & signal handling */
event_init();
event_set(&ev_sighup, SIGHUP, EV_SIGNAL, peripherals_end, NULL);
event_add(&ev_sighup, NULL);
event_set(&ev_sigterm, SIGTERM, EV_SIGNAL, peripherals_end, NULL);
event_add(&ev_sigterm, NULL);
event_set(&ev_sigint, SIGINT, EV_SIGNAL, peripherals_end, NULL);
event_add(&ev_sigint, NULL);
.....
}
但是,在“function_test.c”中:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <event.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include "function_test.h"
.....
/*serial file descriptor */
int 232_fd= -1;
/* Event triggered when data is available */
struct event ev_rs232read;
.....
event_set(&ev_rs232read, 232_fd, EV_READ|EV_PERSIST, readRs232, NULL);
if ((rv = event_add(&stm32_ev_read, NULL)) < 0) {
// log error
return RTN_ERR;
}
return RTN_OK;
}
而且神秘地 Eclipse 没有找到 event.h(仅在 function_test.c 中),因此我得到了下一个错误:
warning: implicit declaration of function ‘event_set’
../src/function_test.c:114: error: ‘EV_READ’ undeclared (first use in this function)
../src/function_test.c:114: error: (Each undeclared identifier is reported only once
../src/function_test.c:114: error: for each function it appears in.)
../src/function_test.c:114: error: ‘EV_PERSIST’ undeclared (first use in this function)
...
【问题讨论】:
-
Libevent 在这种情况下可能是矫枉过正。首先,libevent 是严格意义上的套接字。理论上,它会响应其他事件:通常是您自己的事件。但它根本不适合串行通信。此外,大多数 libevent 都是为套接字层构建的,并且需要一个 ipaddress 和 port。你在做什么我不得不在 2001 年做,而且效果很好。但我早就丢失了我的代码。
-
问题可能是我使用了2.0之前的libevent版本中的一些功能。另一方面,那么 Libevent 不是管理这种通信的好选择?
标签: serial-port libevent