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