• linux嵌入式软件中libmudbus库的移植与使用(二)


     相关内容:

      linux嵌入式软件中libmudbus库的移植与使用(一)

      linux嵌入式软件中libmudbus库的移植与使用(二)

    测试环境:

      在上一篇文档中,创建的install文件夹下,看到include、lib,以备主程序调用。将install文件夹下include里modbus中.h文件、lib里的直接复制到

    含有下面测试代码modbus.c文件夹中。

    测试代码modbus.c:

    #include<errno.h>
    #include<string.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <modbus.h>
    
    
    typedef struct
    {
        unsigned short null_0;                    // 0x00
        unsigned short key1_state;            // 0x01: 按键1状态
        unsigned short key2_state;                // 0x02: 按键2状态
        unsigned short key3_state;                // 0x03: 按键3状态
        unsigned short key4_state;                // 0x04: 按键4状态
        unsigned short wifi_comm_state;            // 0x05: wifi通讯状态
        unsigned short batt_comm_state;            // 0x06:电池通讯状态
        unsigned short null_1[9];                // 0x07~0xF
    
        unsigned short percentage;            // 0x10: 电量
        unsigned short voltage;               // 0x11: 电压
        unsigned short in_current;            // 0x12: 充电电流
        unsigned short out_current;        // 0x13: 放电电流
        unsigned short ch_state;            // 0x14: 充电状态
        unsigned short ch_key;                // 0x15: 充电按键
        
           
    }display_t;
    
    
    display_t disp_date;
    
    
    static void 
    display_fresh(void)
    {
        //
        disp_date.key1_state = 1;
        disp_date.key2_state =1;
        disp_date.key3_state = 1;
        disp_date.key4_state =1;
    
    }
    
    int main(void)
    {
        modbus_t *ctx = NULL;
        
    #if 0    
        ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);        
        //设置从机号码
        modbus_set_slave(ctx, 1);  
        //modbus_set_response_timeout(ctx, 3, 0); //sec,usec,超时设置 设置为3s
        if(modbus_connect(ctx) == -1)
        {        
            printf("Connection failed: %s
    ",modbus_strerror(errno));        
            modbus_free(ctx);           
        }
        
        memset(&disp_date, 0, sizeof(display_t));    
        while(1)
        {
            display_fresh();
            printf("111:%d,%d,%d,%d,",disp_date.key1_state,disp_date.key2_state,disp_date.key3_state,disp_date.key4_state);
            modbus_write_registers(ctx, 0, sizeof(display_t)/sizeof(unsigned short), (const uint16_t*)&disp_date);        
            modbus_read_input_registers(ctx, 0, sizeof(display_t)/sizeof(unsigned short), ( uint16_t*)&disp_date);
            printf("222:%d,%d,%d,%d,",disp_date.key1_state,disp_date.key2_state,disp_date.key3_state,disp_date.key4_state);
            usleep(50*1000);
        }
        
    #else 

      ctx = modbus_new_tcp("192.168.43.123", 8080); modbus_set_debug(ctx, TRUE); modbus_set_slave(ctx, 1);//设置从机号码 if (modbus_connect(ctx) == -1) { printf("modbus connection failed: %s ",modbus_strerror(errno)); modbus_free(ctx); } memset(&disp_date, 0, sizeof(display_t)); while(1) { display_fresh(); printf("1111:%d,%d,%d,%d ",disp_date.key1_state,disp_date.key2_state,disp_date.key3_state,disp_date.key4_state); modbus_write_registers(ctx, 0, sizeof(display_t)/sizeof(unsigned short), (const uint16_t*)&disp_date); modbus_read_input_registers(ctx, 0, sizeof(display_t)/sizeof(unsigned short), ( uint16_t*)&disp_date); printf("2222:%d,%d,%d,%d ",disp_date.key1_state,disp_date.key2_state,disp_date.key3_state,disp_date.key4_state); usleep(1000*1000); } #endif modbus_close(ctx); //关闭modbus连接 modbus_free(ctx); //释放modbus资源,使用完libmodbus需要释放掉 return 0; }

    makefile:

    CROSS    =    
    LIB_PWD = $(shell pwd)
    TARGET_BIN = test
    
    
    
    CPP    = $(CROSS)g++ -std=c++11
    CC    = $(CROSS)gcc
    LD    = $(CROSS)ld
    
    
    CFLAGS  += -I$(shell pwd)
    
    CFLAGS  += -Wall -g  
    LDFLAGS += -lmodbus -ldl -lm -lrt  -L$(LIB_PWD)
    STATIC_LIB += $(LIB_PWD)/libmodbus.a
            
            
    SRCS = $(wildcard *.c)
    OBJS = $(patsubst %.c,%.o,$(SRCS))
    %.o: %.c
        $(CPP) $(CFLAGS) -o $@ -c $< 
        
    all:$(OBJS)
        $(CPP) -o $(TARGET_BIN)  $(OBJS)  $(STATIC_LIB) $(LDFLAGS)
       find . -name "*.o"  | xargs rm -f  clean: find . -name "*.o"  | xargs rm -f 
  • 相关阅读:
    格式与布局 float 左右悬浮边框
    格式与布局
    样式表
    c# 验证码
    邮箱登录页面
    表单与框架
    HTML 练习 做简历表
    HTML 基础
    验证码 随机数
    Linux 三剑客 -- awk sed grep
  • 原文地址:https://www.cnblogs.com/listxue/p/13367051.html
Copyright © 2020-2023  润新知