// #include #include #include "FreeRTOS.h" #include "task.h" #include "hart.h" #include "gpio.h" void hart_send(UART_HandleTypeDef *huart, uint8_t *data) { usart_printf(huart, (char *)data); } void parse_scom_hart(st_scom *scom) { if (scom == &scom1_hart) { scom->rx_flag = FALSE; if ((scom->rx_buff[0] == 0xff) && (scom->rx_buff[1] == 0xff) && (scom->rx_buff[scom->rx_len - 1] == 0xAA)) { ; } scom->tx_flag = TRUE; scom->rx_len = 0; } } void hart_communicate(st_scom *scom) { uint8_t hart_data_test[18] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xaa}; HART_RTS(RTS_OFF); if (scom->rx_flag == TRUE) { scom->rx_flag = FALSE; parse_scom_hart(scom); } // scom->tx_flag = TRUE; if (scom->tx_flag == TRUE) { scom->tx_flag = FALSE; HART_RTS(RTS_ON); vTaskDelay(5); memcpy(scom->tx_buff, hart_data_test, sizeof(hart_data_test)); hart_send(&huart1, scom->tx_buff); HART_RTS(RTS_OFF); } } //DEVICE <---SIG---> PC //将来自设备的HART数据装载至com485_tx,将comhart_tx的HART数据发送至设备 void transparent_hart(st_scom *scom) { if (scom != &scom1_hart) return; HART_RTS(RTS_OFF); //DEVICE -> SIG if (scom->rx_flag == TRUE) { scom->rx_flag = FALSE; //中断内接收到的数据是否符合HART数据报,符合则写入com485_tx,准备发送至PC if ((scom->rx_buff[0] == 0xff) && (scom->rx_buff[1] == 0xff) && (scom->rx_buff[scom->rx_len - 1] == 0xAA)) { //SIG -> PC memcpy(scom2_rs485.tx_buff, scom->rx_buff, sizeof(scom->rx_buff)); scom2_rs485.tx_len = scom->rx_len; scom2_rs485.tx_flag = TRUE; } scom->rx_len = 0; } //SIG -> DEVICE if (scom->tx_flag == TRUE) { scom->tx_flag = FALSE; HART_RTS(RTS_ON); vTaskDelay(5); hart_send(&huart1, scom->tx_buff); HART_RTS(RTS_OFF); } }