K210模块的串口发送代码
import sensor, image,time,lcd,utime
import KPU as kpu
import gc, sys
from fpioa_manager import fm
from machine import UART
fm.register(9, fm.fpioa.UART1_TX, force=True)
fm.register(10, fm.fpioa.UART1_RX, force=True) #映射串口IO口
uart_A = UART(UART.UART1, 115200, 8, 0, 1, timeout=1000, read_buf_len=4096) #初始化串口
data = bytearray([0xa3,0xb3,b[5],b[6],qianbai,shige,0xff])
uart_A.write(data)
识别色块 +数字处理
img = sensor.snapshot() #Take a picture and return the image.
lcd.display(img) #lcd屏幕显示
blobs = img.find_blobs([orginal_threshold],pixels_threshold = 2,area_threshold = 125,merge = 1)
if blobs:
for b in blobs:
x = b[0]
y = b[1]
width = b[2]
height = b[3]
img.draw_rectangle([x,y,width,width]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
Lm = (b[2]+b[3])/2 #b[2]色块的外框的宽 ,b[3]色块的外框的高
print(b[5])#中心X坐标
print(b[6])#中心Y坐标
x1=b[5]//100
x2=b[5]%100
y1=b[6]//100
y2=b[6]%100
length = K/Lm
hh = round(length, 2)#两位小数
zz=hh*100#转换为整数
zz=int(zz)
这里zz是4个数字的数,则需要分成千百位和低二位传输 zz是距离。也映射了距离的算法
整体K210代码
import sensor, image,time,lcd,utime
import KPU as kpu
import gc, sys
from fpioa_manager import fm
from machine import UART
# need your connect hardware IO 9/10 to loopback
fm.register(9, fm.fpioa.UART1_TX, force=True)
fm.register(10, fm.fpioa.UART1_RX, force=True) #映射串口IO口
uart_A = UART(UART.UART1, 115200, 8, 0, 1, timeout=1000, read_buf_len=4096) #初始化串口
orginal_threshold =(16, 47, 25, 57, 9, 62)
#摄像头初始化
sensor.reset()
sensor.set_pixformat(sensor.RGB565) #设置帧格式
sensor.set_framesize(sensor.QVGA) #设置帧大小,成功返回True,失败返回False
sensor.set_vflip(1) #后置拍摄模式
sensor.skip_frames(50) #跳过指定(这里是10帧)帧数或者跳过指定时间内的图像,让相机图像在改变相机设置后稳定下来
sensor.set_auto_whitebal(False) #白平衡关闭
#sensor.snapshot(1.8) #去鱼眼化
#LCD初始化
lcd.init()
img = sensor.snapshot() #用摄像头拍摄一张图片
K=650 #the value should be measured K=length*Lm # 实际的大小=K2*直径的像素
K2=10.5/279 #QQVGA模式下K2=10.5/139 #QVGA模式下K2=10.5/279
while(True):
img = sensor.snapshot() #Take a picture and return the image.
lcd.display(img) #lcd屏幕显示
blobs = img.find_blobs([orginal_threshold],pixels_threshold = 2,area_threshold = 125,merge = 1)
if blobs:
for b in blobs:
x = b[0]
y = b[1]
width = b[2]
height = b[3]
img.draw_rectangle([x,y,width,width]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
Lm = (b[2]+b[3])/2 #b[2]色块的外框的宽 ,b[3]色块的外框的高
print(b[5])#中心X坐标
print(b[6])#中心Y坐标
x1=b[5]//100
x2=b[5]%100
y1=b[6]//100
y2=b[6]%100
length = K/Lm
hh = round(length, 2)#两位小数
zz=hh*100#转换为整数
zz=int(zz)
qianbai=zz//100
shige=zz%100
print(zz)
data = bytearray([0xa3,0xb3,b[5],b[6],qianbai,shige,0xff])
uart_A.write(data)
else:
data = bytearray([0xa3,0xb3,0,0,0,0,0xff])
uart_A.write(data)
#如果没有找到的话,返回
print('0000'+' '+'0000'+'\n')
#lcd.display(img)#lcd屏幕显示
STM32串口接受端函数
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
if(huart==&huart5)
{
if(rx_buff[0]==0xa3)
{
if(rx_buff[1]==0xb3)
{
if(rx_buff[Size-1]==0xff)
{
if((rx_buff[2]>106&&rx_buff[2]<212)&&(rx_buff[3]>80&&rx_buff[3]<160))
{
HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,0);
}
else
{
HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,1);
}
}
}
}
}
}
判断帧头帧尾 如果数据正常 判断 数据是否合格 若合格则点灯 否则灭灯
不要忘记加入
HAL_UARTEx_ReceiveToIdle_DMA(&huart5,rx_buff,sizeof(rx_buff));
#define printfl(...) HAL_UART_Transmit(&huart1,(uint8_t*)u_buff,sprintf((char *)u_buff,__VA_ARGS__),0xffff)
uint8_t u_buff[256];
uint8_t rx_buff[50];
extern DMA_HandleTypeDef hdma_uart5_rx;