dlt645数据读和写

这篇博客详细介绍了如何使用DLT645-2007通信协议进行数据读写操作。`dlt645_2007_read_data`函数用于从设备读取数据,而`dlt645_write_data`函数则实现了向设备写入数据的功能。在读写过程中,涉及到了数据校验、发送和接收消息的步骤,对于理解智能电表等设备的通信过程非常有帮助。
摘要由CSDN通过智能技术生成

/**
 * Name:    dlt645_2007_read_data
 * Brief:   DLT645-2007 数据读取
 * Input:
 *  @ctx:           645句柄
 *  @addr:          从站地址
 *  @code:          数据标识
 *  @read_data:     数据存储地址
 * Output:  None
 */
int dlt645_2007_read_data(dlt645_t *ctx,
                          uint32_t code,
                          uint8_t *read_data)
{
    uint8_t send_buf[DL645_2007_RD_CMD_LEN];
    uint8_t read_buf[DL645_RESP_LEN];

    memset(read_buf, 0, sizeof(read_buf));
    memset(send_buf, 0, sizeof(send_buf));

    memcpy(send_buf + 1, ctx->addr, DL645_ADDR_LEN);

    send_buf[DL645_CONTROL_POS] = C_2007_CODE_RD;
    send_buf[DL645_LEN_POS] = 4;

    uint8_t send_code[4] = {0};
    send_code[0] = (code & 0xff) + 0x33;
    send_code[1] = ((code >> 8) & 0xff) + 0x33;
    send_code[2] = ((code >> 16) & 0xff) + 0x33;
    send_code[3] = ((code >> 24) & 0xff) + 0x33;

    memcpy(send_buf + DL645_DATA_POS, send_code, 4);
    if (dlt645_send_msg(ctx, send_buf, DL645_2007_RD_CMD_LEN) < 0)
    {
        DLT645_LOG("send data error!\n");
        return -1;
    }

    if (dlt645_receive_msg(ctx, read_buf, DL645_RESP_LEN, code, DLT645_2007) < 0)
    {
        DLT645_LOG("receive msg error!\n");
        return -1;
    }

    return dlt645_2007_parsing_data(code, read_buf + DL645_DATA_POS + 4, read_buf[DL645_LEN_POS] - 4, read_data);
}

/**
 * Name:    dlt645_write_data
 * Brief:   DLT645-2007 数据写入
 * Input:
 *  @ctx:           645句柄
 *  @addr:          从站地址
 *  @code:          数据标识
 *  @write_data:    写入数据的指针
 *  @write_len:     写入长度
 * Output:  None
 */
int dlt645_write_data(dlt645_t *ctx,
                      uint32_t addr,
                      uint32_t code,
                      uint8_t *write_data,
                      uint8_t write_len)
{
    uint8_t send_buf[DL645_WR_LEN];
    uint8_t read_buf[DL645_RESP_LEN];

    memset(read_buf, 0, sizeof(read_buf));
    memset(send_buf, 0, sizeof(send_buf));

    memcpy(send_buf + 1, ctx->addr, DL645_ADDR_LEN);

    send_buf[DL645_CONTROL_POS] = C_2007_CODE_WR;
    send_buf[DL645_LEN_POS] = 12 + write_len;

    uint8_t send_code[4] = {0};
    send_code[0] = (code & 0xff) + 0x33;
    send_code[1] = ((code >> 8) & 0xff) + 0x33;
    send_code[2] = ((code >> 16) & 0xff) + 0x33;
    send_code[3] = ((code >> 24) & 0xff) + 0x33;

    for (uint8_t i = 0; i < write_len; i++)
    {
        write_data[i] += 0x33;
    }

    memcpy(send_buf + DL645_DATA_POS, send_code, 4);
    memcpy(send_buf + DL645_DATA_POS + 12, write_data, write_len);
    if (dlt645_send_msg(ctx, send_buf, 24 + write_len) < 0)
    {
        DLT645_LOG("send data error!\n");
        return -1;
    }

    if (dlt645_receive_msg(ctx, read_buf, DL645_RESP_LEN, code, DLT645_2007) < 0)
    {
        DLT645_LOG("receive msg error!\n");
        return -1;
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值