关于openmv循线与stm32串口通讯实现小车控制的问题:为什么采用帧格式串口通讯stm32无法正常接收数据?
-
#THRESHOLD =(0, 23, -12, 5, -5, 13) THRESHOLD_day=(0, 30, -47, 20, -28, 19) import sensor, image, time,math,pyb from pyb import UART,Pin,Timer from pyb import LED from pid import PID import json,ustruct,sensor,time output_str='' uart = UART(3,9600) sensor.reset() sensor.set_vflip(True) sensor.set_hmirror(True) sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QQVGA) sensor.set_windowing((0,60,120,60)) sensor.skip_frames(time = 2000) clock = time.clock() LED(1).on() LED(2).on() LED(3).on() roi_X=[30,0,30,30] roi_stra=[50,0,30,30] rho_pid = PID(p=0.7, i=0) theta_pid = PID(p=0.3, i=0.5,d=0.1) def sending_data(output1,output2): global uart; #frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B]; #data = bytearray(frame) data = ustruct.pack("<bbhhb", #格式为俩个字符俩个短整型(2字节) 0x2C, #帧头1 0x12, #帧头2 int(output1), # up sample by 4 #数据1 int(output2), # up sample by 4 #数据2 0x5B) uart.write(data); while(True): img = sensor.snapshot() img.draw_rectangle(roi_X, color = (255, 0, 0), thickness = 2, fill = False) img.draw_rectangle(roi_stra, color = (255, 0, 0), thickness = 2, fill = False) blobs = img.find_blobs([THRESHOLD_day], roi=roi_X,x_stride=1, y_stride=1,pixels_threshold=10) for blob in blobs: img.draw_rectangle(blob.rect()) img.draw_cross(blob.cx(), blob.cy()) if blob: blobsss = img.find_blobs([THRESHOLD_day],roi=roi_stra,x_stride=1, y_stride=1,pixels_threshold=10) for blob in blobs: img.draw_rectangle(blob.rect()) img.draw_cross(blob.cx(), blob.cy()) if blob: print('go!') sending_data(128,128) output_str='0x2C 0x12 %d %d 0x5B' % (128, 128) #uart.write(output_str) print(output_str) pyb.delay(200) img = sensor.snapshot().binary([THRESHOLD_day]) line1 = img.get_regression([(100,100)], robust = True) if (line1): rho_err = abs(line1.rho())-img.width()/2 if line1.theta()>90: theta_err = line1.theta()-180 else: theta_err = line1.theta() img.draw_line(line1.line(), color = 127) if line1.magnitude()>8: rho_output = rho_pid.get_pid(rho_err,1) theta_output = theta_pid.get_pid(theta_err,1) output = rho_output+theta_output output_str='0x2C 0x12 %d %d 0x5B' % (128-output, 128+output) sending_data(128-output,128+output) #uart.write(output_str) print(output_str) else: output_str='0x2C 0x12 000 128 0x5B' #uart.write(output_str) sending_data(0,128) print(output_str) pass
没有任何报错,我发送数据给stm32的串口2,再用串口1发送给电脑进行测试,但电脑没有显示接收到任何数据?不用帧格式进行简单串口通讯倒可以。怀疑是openmv这边帧格式包装有问题,但是没有给我报任何错。
以下是stm32代码:/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2023 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "stdio.h" #include "string.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ unsigned int sysTime; char SendBuffer[1024]; char SendBuffer2[1024]; uint8_t USART1_RXbuff; uint8_t USART2_RXbuff; uint8_t ch = 0; static uint8_t Cx=0,Cy=0; unsigned int Uart_SyTim=0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ void Openmv_Receive_Data(int16_t Com_Data) { /*Ñ»·Ìå±äÁ¿*/ uint8_t i; /*¼ÆÊý±äÁ¿*/ static uint8_t RxCounter1=0;//¼ÆÊý /*Êý¾Ý½ÓÊÕÊý×é*/ static uint16_t RxBuffer1[5]={0}; /*Êý¾Ý´«Êä״̬λ*/ static uint8_t RxState = 0; /*¶ÔÊý¾Ý½øÐÐУ׼£¬ÅжÏÊÇ·ñΪÓÐЧÊý¾Ý*/ // Uart_SyTim++; // if ((Uart_SyTim % 200) == 0) // { // sprintf((char *)SendBuffer, "test\n"); // HAL_UART_Transmit(&huart1, (uint8_t *)&SendBuffer, strlen(SendBuffer),1000); // }//ÊÇ·ñ½øÈë¸Ãº¯Êý²âÊÔ if(RxState==0&&Com_Data==0x2C) //0x2cÖ¡Í· { RxState=1; RxBuffer1[RxCounter1++]=Com_Data; Uart_SyTim++; if ((Uart_SyTim % 200) == 0) { sprintf((char *)SendBuffer, "test\n"); HAL_UART_Transmit(&huart1, (uint8_t *)&SendBuffer, strlen(SendBuffer),1000); }//ÊÇ·ñ½øÈë¸Ã²½²âÊÔ } else if(RxState==1&&Com_Data==0x12) //0x12Ö¡Í· { RxState=2; RxBuffer1[RxCounter1++]=Com_Data; // Uart_SyTim++; // if ((Uart_SyTim % 200) == 0) // { // sprintf((char *)SendBuffer, "test\n"); // HAL_UART_Transmit(&huart1, (uint8_t *)&SendBuffer, strlen(SendBuffer),1000); // }//ÊÇ·ñ½øÈë¸Ã²½²âÊÔ } else if(RxState==2) { RxBuffer1[RxCounter1++]=Com_Data; // Uart_SyTim++; // if ((Uart_SyTim % 200) == 0) // { // sprintf((char *)SendBuffer, "tttttst\n"); // HAL_UART_Transmit(&huart1, (uint8_t *)&SendBuffer, strlen(SendBuffer),1000); // }//ÊÇ·ñ½øÈë¸Ã²½²âÊÔ if(RxCounter1>=5||Com_Data == 0x5B) //RxBuffer1½ÓÊÜÂúÁË,½ÓÊÕÊý¾Ý½áÊø { // Uart_SyTim++; // if ((Uart_SyTim % 20) == 0) // { // sprintf((char *)SendBuffer, "wate\n"); // HAL_UART_Transmit(&huart1, (uint8_t *)&SendBuffer, strlen(SendBuffer),1000); // }//ÊÇ·ñ½øÈë¸Ã²½²âÊÔ RxState=3; Cx=RxBuffer1[RxCounter1-5]; Cy=RxBuffer1[RxCounter1-4]; // printf("%d\r ",Cx); // printf("%d\r ",Cy); // Uart_SyTim++; // if ((Uart_SyTim % 200) == 0) // { // sprintf((char *)SendBuffer, "Cx,Cy:%d,%d\n",Cx,Cy); // HAL_UART_Transmit(&huart1, (uint8_t *)&SendBuffer, strlen(SendBuffer),1000); // }//ÊÇ·ñ½øÈë¸Ã²½²âÊÔ } } else if(RxState==3)//¼ì²âÊÇ·ñ½ÓÊܵ½½áÊø±êÖ¾ { if(RxBuffer1[RxCounter1-1] == 0x5B) { RxCounter1 = 0; RxState = 0; } else //½ÓÊÕ´íÎó { RxState = 0; RxCounter1=0; for(i=0;i<5;i++) { RxBuffer1[i]=0x00; //½«´æ·ÅÊý¾ÝÊý×éÇåÁã } } } else //½ÓÊÕÒì³£ { RxState = 0; RxCounter1=0; for(i=0;i<5;i++) { RxBuffer1[i]=0x00; //½«´æ·ÅÊý¾ÝÊý×éÇåÁã } } } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART1_UART_Init(); MX_USART2_UART_Init(); /* USER CODE BEGIN 2 */ HAL_UART_Receive_IT(&huart2,(void *)&USART2_RXbuff,1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { uint16_t tempt /*¶¨ÒåÁÙʱ±äÁ¿´æ·Å½ÓÊܵÄÊý¾Ý*/; if(huart->Instance==USART2) { // Uart_SyTim++; // if ((Uart_SyTim % 200) == 0) // { // sprintf((char *)SendBuffer, "ggghgh\n"); // HAL_UART_Transmit(&huart1, (uint8_t *)&SendBuffer, strlen(SendBuffer),1000); // }//ÊÇ·ñ½øÈë´®¿Ú2²âÊÔ tempt=USART2_RXbuff; Openmv_Receive_Data(tempt); } HAL_UART_Receive_IT(&huart2,(void *)&USART2_RXbuff,1);/*ÔٴοªÆô½ÓÊÕÖжÏ*/ } /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */