|
@ -13,6 +13,35 @@ static modbus_processer_t* g_processer; |
|
|
|
|
|
|
|
|
#define GET_UINT16(uint8) (uint16_t)(((uint16_t)((uint8)[0]) << 8) + (uint8)[1]) |
|
|
#define GET_UINT16(uint8) (uint16_t)(((uint16_t)((uint8)[0]) << 8) + (uint8)[1]) |
|
|
|
|
|
|
|
|
|
|
|
static void modbus_processer_process_data(uint8_t* rx, uint16_t rxlen) { |
|
|
|
|
|
/** |
|
|
|
|
|
* @brief modbus接收完成以后在这里进行处理数据 |
|
|
|
|
|
* |
|
|
|
|
|
*/ |
|
|
|
|
|
//如果不是广播信息或者不是当前设备的信息,丢弃 |
|
|
|
|
|
if (rx[0] != 0 && rx[0] != g_processer->modbus_device_id) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
// CRC校验失败 |
|
|
|
|
|
if (!ZCheckRTUMessageIntegrity(rx, rxlen)) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
// if(functioncode) |
|
|
|
|
|
static ModbusMasterData_t rxorder; |
|
|
|
|
|
static ModbusSlaveData_t txorder; |
|
|
|
|
|
//将接受到的数据进行组包 |
|
|
|
|
|
ModbusProcessRxData(rx, rxlen, &rxorder); |
|
|
|
|
|
static uint16_t txsendlen = 0; |
|
|
|
|
|
txorder.deviceId = g_processer->modbus_device_id; |
|
|
|
|
|
//功能码 |
|
|
|
|
|
txorder.functionCode = rxorder.functionCode; |
|
|
|
|
|
|
|
|
|
|
|
modbus_processer_context_t context = {0}; |
|
|
|
|
|
context.tx = &txorder; |
|
|
|
|
|
context.rx = &rxorder; |
|
|
|
|
|
g_processer->process_rx(&context); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
void modbus_processer_init(modbus_processer_t* processer) { g_processer = processer; } |
|
|
void modbus_processer_init(modbus_processer_t* processer) { g_processer = processer; } |
|
|
void modbus_processer_push_data(uint8_t rxdata) { |
|
|
void modbus_processer_push_data(uint8_t rxdata) { |
|
|
if (!s_modbus_uart_rx_buf_is_processing) { |
|
|
if (!s_modbus_uart_rx_buf_is_processing) { |
|
@ -99,4 +128,30 @@ void modbus_send_10(modbus_processer_context_t* context, ModbusStatus modbus_sta |
|
|
modbus_status); |
|
|
modbus_status); |
|
|
g_processer->tx(g_processer->modbus_processer_tx_buf, sendlegth); |
|
|
g_processer->tx(g_processer->modbus_processer_tx_buf, sendlegth); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void modbus_processer_try_process_data() { |
|
|
|
|
|
/** |
|
|
|
|
|
* @brief |
|
|
|
|
|
* 根据modbus协议,当超过3.5个字符时间后依然没有收到数据 |
|
|
|
|
|
*/ |
|
|
|
|
|
if (s_modbus_uart_rx_off != 0) { |
|
|
|
|
|
uint16_t modbus_uart_rx_off_before = s_modbus_uart_rx_off; |
|
|
|
|
|
|
|
|
|
|
|
g_processer->port_delay_us(g_processer->modbus_baundrate_one_packet_delay_us); |
|
|
|
|
|
|
|
|
|
|
|
g_processer->port_enter_critical(); |
|
|
|
|
|
if (s_modbus_uart_rx_off == modbus_uart_rx_off_before) { |
|
|
|
|
|
s_modbus_uart_rx_buf_is_processing = true; |
|
|
|
|
|
} |
|
|
|
|
|
g_processer->port_exit_critical(); |
|
|
|
|
|
|
|
|
|
|
|
if (s_modbus_uart_rx_buf_is_processing) { |
|
|
|
|
|
modbus_processer_process_data(g_processer->modbus_processer_rx_buf, s_modbus_uart_rx_off); |
|
|
|
|
|
g_processer->port_enter_critical(); |
|
|
|
|
|
s_modbus_uart_rx_off = 0; |
|
|
|
|
|
s_modbus_uart_rx_buf_is_processing = false; |
|
|
|
|
|
g_processer->port_exit_critical(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |