libevent事件的简单使用

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总线上报数据时,进入回调处理。可成功接收到数据。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值