|
@@ -52,7 +52,7 @@ int fputc(int ch, FILE *f)
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
|
|
|
|
/* USER CODE BEGIN PV */
|
|
|
-uint8_t data_to_send[300];
|
|
|
+uint8_t data_to_send[100];
|
|
|
uint16_t CRC_value = 0;
|
|
|
uint16_t initial_address = 0x00C1;
|
|
|
uint32_t System_version = 0x23051100;
|
|
@@ -116,6 +116,9 @@ int main(void)
|
|
|
/* USER CODE BEGIN WHILE */
|
|
|
while (1)
|
|
|
{
|
|
|
+ __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
|
|
|
+ __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
|
|
|
+
|
|
|
#if Modbus
|
|
|
gpioaStatus = GetPaInputStatus();
|
|
|
gpiobStatus = GetPbOutputStatus();
|
|
@@ -131,8 +134,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,5);
|
|
|
data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[6] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 7, 9); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x02 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x01){
|
|
@@ -144,8 +152,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,5);
|
|
|
data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[6] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 7, 9); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x03 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x01){
|
|
@@ -157,8 +170,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,5);
|
|
|
data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[6] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x04 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x01){
|
|
@@ -170,8 +188,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,5);
|
|
|
data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[6] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x05 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x01){
|
|
@@ -183,8 +206,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,5);
|
|
|
data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[6] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x0A && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x01){
|
|
@@ -200,8 +228,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,5);
|
|
|
data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[6] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x0B && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x01){
|
|
@@ -217,8 +250,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,5);
|
|
|
data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[6] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0xAA && receive_buffer[4] == 0x00 ){
|
|
@@ -232,8 +270,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,5);
|
|
|
data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[6] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0xBB && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x02){
|
|
@@ -247,8 +290,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,7);
|
|
|
data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[8] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 9, 9); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 11);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0xCC && receive_buffer[4] == 0xA5 && receive_buffer[5] == 0x5A){
|
|
@@ -268,8 +316,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,5);
|
|
|
data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[6] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x01 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x02 ){
|
|
@@ -283,8 +336,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,7);
|
|
|
data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[8] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 9, 8); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 11);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x01 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x03 ){
|
|
@@ -300,8 +358,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,9);
|
|
|
data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[10] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 11, 10); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 14);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x01 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x04 ){
|
|
@@ -319,8 +382,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,11);
|
|
|
data_to_send[11] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[12] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 13, 12); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 13, 15);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x01 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x05 ){
|
|
@@ -340,8 +408,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,13);
|
|
|
data_to_send[13] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[14] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 15, 14); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 15, 16);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x02 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x02 ){
|
|
@@ -355,8 +428,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,7);
|
|
|
data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[8] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 9, 8); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 11);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x02 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x03 ){
|
|
@@ -372,8 +450,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,9);
|
|
|
data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[10] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 11, 10); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 14);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x02 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x04 ){
|
|
@@ -391,8 +474,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,11);
|
|
|
data_to_send[11] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[12] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 13, 12); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 13, 15);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x03 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x02 ){
|
|
@@ -406,8 +494,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,7);
|
|
|
data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[8] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 9, 8); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 11);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x03 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x03 ){
|
|
@@ -423,8 +516,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,9);
|
|
|
data_to_send[9] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[10] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 11, 10); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 11, 14);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x04 && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x02 ){
|
|
@@ -438,8 +536,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,7);
|
|
|
data_to_send[7] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[8] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 9, 8); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 9, 11);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == (uint8_t)initial_address && receive_buffer[1] == 0x06 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0x0C && receive_buffer[4] == 0x00 ){
|
|
@@ -455,8 +558,13 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,6);
|
|
|
data_to_send[6] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[7] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 8, 8); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 8, 9);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
else if(receive_buffer[0] == 0xFA && receive_buffer[1] == 0x03 && receive_buffer[2] == 0x00 && receive_buffer[3] == 0xAA && receive_buffer[4] == 0x00 && receive_buffer[5] == 0x01 ){
|
|
@@ -468,29 +576,26 @@ int main(void)
|
|
|
CRC_value = calculate_crc(data_to_send,5);
|
|
|
data_to_send[5] = (uint8_t)(CRC_value & 0xFF);
|
|
|
data_to_send[6] = (uint8_t)(CRC_value >> 8);
|
|
|
- HAL_UART_Transmit(&huart1, data_to_send, 7, 7); // 发送数据
|
|
|
- flag = 0; // 将标志位重新置为0
|
|
|
- }
|
|
|
+ HAL_StatusTypeDef status = HAL_UART_Transmit(&huart1, data_to_send, 7, 9);
|
|
|
+ if (status == HAL_OK) {
|
|
|
+ // 发送成功
|
|
|
+ flag = 0; // 将标志位重新置为0
|
|
|
+ } else {
|
|
|
+ HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ memset(receive_buffer,0,sizeof(receive_buffer));
|
|
|
+ read_new_address_from_flash(ADDR_FLASH_PAGE_61);
|
|
|
+ __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_ORE); // 清除接收溢出标志位
|
|
|
+ __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); // 清除接收中断标志位
|
|
|
}
|
|
|
|
|
|
HAL_UART_Receive_IT(&huart1, &receive_buffer[0], 8);
|
|
|
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
|
|
|
HAL_Delay(1);
|
|
|
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
|
|
|
-
|
|
|
-
|
|
|
- static uint32_t last_receive_time = 0; // 上一次接收到数据的时间
|
|
|
- uint32_t current_time = HAL_GetTick(); // 当前时间
|
|
|
- if (HAL_UART_GetError(&huart1) != HAL_UART_ERROR_NONE) // 如果发生了错误
|
|
|
- {
|
|
|
- NVIC_SystemReset(); // 重启设备
|
|
|
- }
|
|
|
- if (current_time - last_receive_time > 1000) // 如果1秒钟没有接收到数据
|
|
|
- {
|
|
|
- NVIC_SystemReset(); // 重启设备
|
|
|
- }
|
|
|
- last_receive_time = current_time; // 更新上一次接收到数据的时间
|
|
|
-
|
|
|
+
|
|
|
#endif
|
|
|
/* USER CODE END WHILE */
|
|
|
|