1、初始化并添加到事件池中
struct event *rs485_bus_ev = event_new(base, rs485_fd, EV_READ | EV_PERSIST, read_rs485_bus_data_cb, NULL);
event_add(rs485_bus_ev, NULL);
2、回调实现
void read_rs485_bus_data_cb(evutil_socket_t fd, short what, void*arg)
{
size_t max_len;
uint8_t ended = 0;
int result;
int i = 0;
result = read(fd, ptr, RS485_MAX_BUF - bytes_read);
bytes_read += result;
ptr += result;
if (bytes_read > 4) {
for (i = 0; i < supp_num; i++){
if (NULL != rs485_pro_fun[i].p_fn_support_protocol) {
if (1 == rs485_pro_fun[i].p_fn_support_protocol(bus_frame[1])) {
if (NULL != rs485_pro_fun[i].p_fn_is_write_cmd) {
if (1 == rs485_pro_fun[i].p_fn_is_write_cmd(bus_frame[1])) {
if(NULL != rs485_pro_fun[i].p_fn_get_ack_end_len) {
max_len = rs485_pro_fun[i].p_fn_get_ack_end_len(bus_frame[1], bus_frame[2], bus_frame[3]);
//syslog_info("\r\n1111111 max_len=%d\r\n", max_len);
if (max_len) {
if (bytes_read >= max_len) {
ended = 1;
syslog_info("\r\n1111111 read end\r\n");
}
}
}
}
}
if (NULL != rs485_pro_fun[i].p_fn_is_read_cmd) {
if (1 == rs485_pro_fun[i].p_fn_is_read_cmd(bus_frame[1])) {
if(NULL != rs485_pro_fun[i].p_fn_get_ack_end_len) {
max_len = rs485_pro_fun[i].p_fn_get_ack_end_len(bus_frame[1], bus_frame[2], bus_frame[3]);
//syslog_info("\r\n22222222 max_len=%d\r\n", max_len);
if (max_len) {
if (bytes_read > max_len) {
ended = 1;
syslog_info("\r\n2222222 read end\r\n");
}
}
}
}
}
}
}
}
}
if ((1 == ended) && bytes_read >= max_len) {
for (uint8_t i = 0; i < supp_num; i++) {
if (NULL != rs485_pro_fun[i].p_fn_support_protocol) {
if (1 == rs485_pro_fun[i].p_fn_support_protocol(bus_frame[1])) {
syslog_info("\r\nNote:link layer s_fp_data_cb_handle\r\n");
syslog_dump(bytes_read, bus_frame);
if(NULL != s_fp_data_cb_handle) {
s_fp_data_cb_handle((uint8_t*)bus_frame, bytes_read);
}
bytes_read = 0;
memset(bus_frame, 0x0, RS485_MAX_BUF);
ptr = (unsigned char *)bus_frame;
}
} else {
bytes_read = 0;
memset(bus_frame, 0x0, RS485_MAX_BUF);
ptr = (unsigned char *)bus_frame;
}
}
}
}
3、当485总线上报数据时,进入回调处理。可成功接收到数据。