dlt645数据读和写

 2023-09-05 阅读 68 评论 0

摘要:/** * 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,

/**
 * 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;
}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/703.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息