目录

K210-UART串口通信相关函数:

使用K210串口的时候需要映射引脚:

K210与STM32串口通信 

发送单字节:

K210端

STM32端

发送数据包

K210端

STM32端 

K210的UART模块支持全双工通信,可以同时进行数据的发送和接收。在K210上使用UART串口通信,你可以连接外部设备,如传感器、显示器、WiFi模块等,实现数据的交换和控制。

K210-UART串口通信相关函数:

1.machine.UART():该函数用于初始化一个UART串口对象。你可以指定串口的编号(如UART1)、波特率、数据位、校验位、停止位、TX引脚和RX引脚等参数。

import machine

# 初始化串口
uart = machine.UART(1, baudrate=115200, tx=12, rx=13)

在这个示例中,我们使用machine.UART()函数初始化了一个串口对象,指定了串口的编号(1)、波特率(115200)、TX引脚(12)和RX引脚(13)。

或者可以:

machine.UART(uart,baudrate,bits,parity,stop,timeout, read_buf_len)


2.uart.write():该函数用于向串口发送数据。你可以发送字符串或者字节数据。

# 发送数据
uart.write('Hello, UART!')

3.uart.read():该函数用于从串口接收数据。它会阻塞程序直到接收到指定数量的数据或者超时

# 接收数据
data = uart.read(10)
print(data)

4.uart.any():该函数用于检查串口是否有未读取的数据。如果有,返回值为True;否则返回False。

# 检查是否有未读取的数据
if uart.any():
    data = uart.read()
    print(data)

5.uart.flush():该函数用于清空串口的缓冲区,将所有未读取的数据丢弃

使用K210串口的时候需要映射引脚

# IO6→RX1,IO7→TX1
fm.register(6, fm.fpioa.UART1_RX, force=True)
fm.register(7, fm.fpioa.UART1_TX, force=True)

K210与STM32串口通信 

K210与STM32串口通信发送分为两种:一种是发送单字节,一种是发送数据包。因为发送的数据不一样,从而K210的代码和STM32的代码都是不一样的。

本篇文章只涉及K210发送给STM32。

发送单字节:

K210端

from machine import UART, Timer
from fpioa_manager import fm

#映射串口引脚
fm.register(6, fm.fpioa.UART1_RX, force=True)
fm.register(7, fm.fpioa.UART1_TX, force=True)
while True:
    text=uart.read() #读取数据

    if text: #如果读取到了数据
        uart.write('1')

STM32端

void Usart3_Init(unsigned int baud)
{
    GPIO_InitTypeDef gpio_initstruct;
	USART_InitTypeDef usart_initstruct;
	NVIC_InitTypeDef nvic_initstruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
	
	//PA2	TXD
	gpio_initstruct.GPIO_Mode = GPIO_Mode_AF_PP;
	gpio_initstruct.GPIO_Pin = GPIO_Pin_10;
	gpio_initstruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &gpio_initstruct);
	
	//PA3	RXD
	gpio_initstruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	gpio_initstruct.GPIO_Pin = GPIO_Pin_11;
	gpio_initstruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &gpio_initstruct);
	
	usart_initstruct.USART_BaudRate = baud;
	usart_initstruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;		
	usart_initstruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;						
	usart_initstruct.USART_Parity = USART_Parity_No;									
	usart_initstruct.USART_StopBits = USART_StopBits_1;								
	usart_initstruct.USART_WordLength = USART_WordLength_8b;							
	USART_Init(USART3, &usart_initstruct);
	USART_Cmd(USART3, ENABLE);	
	USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);									
	nvic_initstruct.NVIC_IRQChannel = USART2_IRQn;
	nvic_initstruct.NVIC_IRQChannelCmd = ENABLE;
	nvic_initstruct.NVIC_IRQChannelPreemptionPriority = 0;
	nvic_initstruct.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&nvic_initstruct);

}
if(USART_ReceiveData(USART3)=='1')//K210
{
        numbert++;
}

发送数据包

K210端

def str_int(data_str):
    bb = binascii.hexlify(data_str)
    bb = str(bb)[2:-1]
    #print(bb)
    #print(type(bb))
    hex_1 = int(bb[0])*16
    hex_2 = int(bb[1],16)
    return hex_1+hex_2


def send_data(x,y,w,h,msg):
    start = 0x24
    end = 0x23
    length = 5
    class_num = 0x05 #例程编号
    class_group = 0xBB #例程组
    data_num = 0x00 #数据量
    fenge = 0x2c #逗号
    crc = 0 #校验位
    data = [] #数据组

    #参数都为0
    if x==0 and y==0 and w==0 and h ==0:
        pass
    else:
        #x(小端模式)
        low = x & 0xFF #低位
        high = x >> 8& 0xFF #高位
        data.append(low)
        data.append(fenge) #增加","
        data.append(high)
        data.append(fenge) #增加","

        #y(小端模式)
        low = y & 0xFF #低位
        high = y >> 8& 0xFF #高位
        data.append(low)
        data.append(fenge) #增加","
        data.append(high)
        data.append(fenge) #增加","

        #w(小端模式)
        low = w & 0xFF #低位
        high = w >> 8& 0xFF #高位
        data.append(low)
        data.append(fenge) #增加","
        data.append(high)
        data.append(fenge) #增加","

        #h(小端模式)
        low = h & 0xFF #低位
        high = h >> 8& 0xFF #高位
        data.append(low)
        data.append(fenge) #增加","
        data.append(high)
        data.append(fenge) #增加","

    if msg !=None:
        #msg
        for i in range(len(msg)):
            hec = str_int(msg[i])
            data.append(hec)
            data.append(fenge) #增加","
        #print(data)

    data_num = len(data)
    length += len(data)
    #print(length)

    send_merr = [length,class_num,class_group,data_num]
    for i in range(data_num):
        send_merr.append(data[i])
    #print(send_merr)

    #不加上CRC位,进行CRC运算
    for i in range(len(send_merr)):
        crc +=send_merr[i]
    crc = crc%256

    send_merr.insert(0,start) #插入头部
    send_merr.append(crc)
    send_merr.append(end)

    #print(send_merr)
    global send_buf
    send_buf = send_merr

STM32端 

char buf_msg[100] = {'\0'};
u8 new_flag = 0;
u8 r_index = 0;
u16 buf_crc = 0;
u8 tou_flag = 0;
u8 len_flag = 0;
u8 buf_len = 0;
char data[50];

void recv_k210msg(uint8_t recv_msg)
{
    if (recv_msg == '$')
    {
        new_flag = 1;
    }

    if (recv_msg == '#')
    {
        if (buf_len == r_index)
        {
            new_flag = 0;
            tou_flag = 0;
            len_flag = 0;

            buf_crc -= buf_msg[r_index - 1];
            buf_crc %= 256;

            if (buf_crc == buf_msg[r_index - 1])
            {
                deal_recvmsg();
            }
            else
            {
                r_index = 0;
                buf_crc = 0;
            }
        }
    }

    if (new_flag == 1)
    {
        if (recv_msg == '$' && tou_flag == 0)
        {
            tou_flag = 1;
        }
        else
        {
            buf_msg[r_index++] = recv_msg;
            buf_crc += recv_msg;
            if (len_flag == 0)
            {
                buf_len = buf_msg[0];
                len_flag = 1;
            }
        }
    }
}

void deal_recvmsg(void)
{
    u8 index, data_i = 0;
    u8 eg_num = buf_msg[1];
    u8 number = buf_msg[3];
    u8 i_duo = 0;
  
    if (r_index != buf_len)
    {
        buf_len = 0;
        return;
    }

    for (index = 0; index < number; index++)
    {
        if (buf_msg[4 + index] == 0x2c && i_duo == 0)
        {
            i_duo = 1;
            continue;
        }
        data[data_i++] = buf_msg[4 + index];
        i_duo = 0;
    }

    buf_crc = 0;
    r_index = 0;

    memset(buf_msg, 0, sizeof(buf_msg));
    deal_data(eg_num);
}

void deal_data(u8 egnum)
{
    u16 x, y, w, h;
    u8 msg[20] = {'\0'};
    u8 icopy = 0;
    u16 id = 999;
    switch (egnum)
    {
        case 1:
        case 5:
        case 6:
            x = data[1] << 8 | data[0];
            y = data[3] << 8 | data[2];
            w = data[5] << 8 | data[4];
            h = data[7] << 8 | data[6];
            break;

        case 2:
        case 3:
            x = data[1] << 8 | data[0];
            y = data[3] << 8 | data[2];
            w = data[5] << 8 | data[4];
            h = data[7] << 8 | data[6];
            while (*(data + 8 + icopy) != '\0')
            {
                msg[icopy] = *(data + 8 + icopy);
                icopy++;
            }
            break;

        case 4:
            x = data[1] << 8 | data[0];
            y = data[3] << 8 | data[2];
            w = data[5] << 8 | data[4];
            h = data[7] << 8 | data[6];
            id = data[8] << 8 | data[9];

            while (*(data + 10 + icopy) != '\0')
            {
                msg[icopy] = *(data + 10 + icopy);
                icopy++;
            }

            break;

        case 7:
        case 8:
            x = data[1] << 8 | data[0];
            y = data[3] << 8 | data[2];
            w = data[5] << 8 | data[4];
            h = data[7] << 8 | data[6];
            id = data[8];

            break;

        case 9:
            x = data[1] << 8 | data[0];
            y = data[3] << 8 | data[2];
            w = data[5] << 8 | data[4];
            h = data[7] << 8 | data[6];
            while (*(data + 8 + icopy) != '\0')
            {
                msg[icopy] = *(data + 8 + icopy);
                icopy++;
            }
            break;

        case 10:
        case 11:
            id = data[0];
            break;
    }
    
    k210_msg.class_n = egnum;
    k210_msg.x = x;
    k210_msg.y = y;
    k210_msg.w = w;
    k210_msg.h = h;
    k210_msg.id = id;
    strcpy((char*)k210_msg.msg_msg, (char*)msg);

    memset(data, 0, sizeof(data));
}
void USART2_IRQHandler(void)
{
	uint8_t Rx2_Temp;
	if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
	{
		Rx2_Temp = USART_ReceiveData(USART2);
		recv_k210msg(Rx2_Temp);
	}
}

 有读者看到这里可能会想问,能不能用单字节的方式发送字符串,STM32中USART_ReceiveData(USART3)=='1'此处没法等于字符串。

01-31 20:36