75 lines
2.6 KiB
C
75 lines
2.6 KiB
C
#include "user_gpio.h"
|
|
|
|
extern uint8_t send_data_flag_cmd;
|
|
extern uint8_t tcp_echo_flags_control;
|
|
uint8_t di_state_last[DI_MAX] = {0};
|
|
uint8_t di_state_now[DI_MAX] = {0};
|
|
void user_write_gpio(communication_do_t *do_data)
|
|
{
|
|
uint8_t i = 0;
|
|
uint8_t start_addr = do_data->start_addr; // 写输出的起始地址
|
|
uint8_t length = do_data->num; // 写输出的数量
|
|
for (i = 0; i < length; i++)
|
|
{
|
|
if (do_data->data[i] == 0x01)
|
|
{
|
|
gpio_do_test(DO_1 + start_addr + i, GPIO_PIN_SET);
|
|
}
|
|
else
|
|
{
|
|
gpio_do_test(DO_1 + start_addr + i, GPIO_PIN_RESET);
|
|
}
|
|
}
|
|
}
|
|
void user_read_gpio(communication_di_t *di_data, uint8_t *tx_data, const uint8_t *const rx_data)
|
|
{
|
|
uint8_t i = 0;
|
|
uint8_t start_addr = di_data->start_addr; // 读输入的起始地址
|
|
uint8_t length = di_data->num; // 读输入的数量
|
|
uint8_t tx_data_len = 7 + length; // 数据长度
|
|
tx_data[0] = FRAME_HEAD; // 帧头
|
|
tx_data[1] = COM_OK; // 状态
|
|
tx_data[2] = rx_data[2]; // 设备号
|
|
tx_data[3] = rx_data[3]; // 命令号
|
|
tx_data[4] = length; // 数据长度
|
|
for (i = 0; i < length; i++)
|
|
{
|
|
tx_data[5 + i] = gpio_di_test(DI_1 + i + start_addr);
|
|
}
|
|
tx_data[5 + length] = xor_compute(tx_data + 1, tx_data_len - 3); // 异或校验
|
|
tx_data[6 + length] = FRAME_TAIL; // 帧尾
|
|
}
|
|
|
|
void user_gpio_trigger(void)
|
|
{
|
|
uint8_t di_ch = 0;
|
|
uint8_t tx_data_len = 7 + DI_MAX;
|
|
uint8_t tx_data[32] = {0};
|
|
tx_data[0] = FRAME_HEAD;
|
|
tx_data[1] = COM_OK;
|
|
tx_data[2] = DEVICE_NUM;
|
|
tx_data[3] = SEND_STATE_CMD;
|
|
tx_data[4] = DI_MAX;
|
|
for (di_ch = 0; di_ch < DI_MAX; di_ch++)
|
|
{
|
|
di_state_now[di_ch] = gpio_di_test(di_ch);
|
|
if (di_state_last[di_ch] != di_state_now[di_ch])
|
|
{
|
|
di_state_last[di_ch] = di_state_now[di_ch];
|
|
send_data_flag_cmd = 1;
|
|
}
|
|
tx_data[5 + di_ch] = di_state_now[di_ch];
|
|
}
|
|
if ((send_data_flag_cmd != 0) && (1 == tcp_echo_flags_control))
|
|
{
|
|
tx_data[5 + DI_MAX] = xor_compute(tx_data + 1, tx_data_len - 3); // 寮傛垨鏍¢獙
|
|
tx_data[6 + DI_MAX] = FRAME_TAIL; // 甯у熬
|
|
user_send_data_control(tx_data, tx_data_len);
|
|
send_data_flag_cmd++;
|
|
if (send_data_flag_cmd > 3) // 杩炵画涓夋涓婁綅鏈烘病鏈夊洖搴旓紝鍒欏仠姝㈠彂閫佹暟鎹寘
|
|
{
|
|
send_data_flag_cmd = 0;
|
|
}
|
|
}
|
|
}
|