当前位置:网站首页>Punctual atomic serial port protocol
Punctual atomic serial port protocol
2022-07-28 17:33:00 【maosql】
Serial protocol \r\n ending
function : The preliminary implementation function uses serial port to send data , Send to by queue SPI, Then grab it with a logic analyzer spi wave form , Whether the data sent by the serial port is consistent .
Background : Doing this project, I found that I am not very familiar with serial port protocol , Because the punctual atom used in school has always used the serial port protocol , I didn't understand the program very much , No deep understanding . Now, when you use it, you realize it yourself , I found that there was one at the end \r Sneaked in , Just as expected , But no suitable method has been found , Just learn from what you learned before stm32 The code written by the punctual atom .
【 Tips: 】 Before the introduction, first understand the representation and differences between carriage return and line feed :
enter : Return to the beginning of the current line , I won't go back to the next line , If you then enter, the previous contents of this line will be overwritten .
Line break : Switch to the next line in the current position , Instead of going back to the first line .
| enter | Line break |
|---|---|
| ox0d | 0x0a |
| \r | \n |
1. Refer to the punctual atomic serial port routine
send out 1 2 3 4 5 Grab the ascii The corresponding value ,
The detailed explanation is fully commented in the code , It is also very useful to understand the serial port protocol of punctual atoms , The deeper you understand, the more you can realize the subtlety . If you look at the , If you don't understand the code, welcome to discuss .
#define UART_BAUDRATE 115200
#define UART_RX_SIZE 256
char rx_buff[UART_RX_SIZE];
uint16_t USART_RX_STA=0;//0000 0000 0000 0000 The doorway is here
void uart_test_poll(uint8_t port)
{
int val;
bool_t err;
while (1)
{
os_delay(10);
val=iot_uart_rx(port);
if (val<0)
{
continue;
}
if((USART_RX_STA&0x8000)==0)// Judge the highest position
{
if(USART_RX_STA&0x4000)//0x0d(\r) If you receive
{
if(val!=0x0a)//0x0a(\n) If not received , Failed to receive again
USART_RX_STA=0;
else
USART_RX_STA|=0x8000; // The highest position 1
}
else
{
if(val==0x0d) // If you receive 0x0d(\r) Just put USART_RX_STA Secondary high position 1
USART_RX_STA|=0x4000;// Secondary high position 1
else// Send the data to buff, Judge if it is greater than buff Capacity stops receiving
{
rx_buff[USART_RX_STA&0x3fff]=(char)val;
USART_RX_STA++;
if(USART_RX_STA>(UART_RX_SIZE-1))
USART_RX_STA=0;
}
}
}
if(USART_RX_STA&0x8000)// If the highest level is 1
{
iot_printf("rx_buff:%s",rx_buff);
iot_printf("UART_TX SIZE:%d,Task send:%d\r\n",UART_RX_SIZE,UART_SIZE);
if(uart_qeue!=NULL)
{
err=os_queue_send(uart_qeue,rx_buff);
if(err==false)
{
iot_printf("uart qeue full,data send fail\r\n");
}
}
os_mem_set(rx_buff,0,sizeof(rx_buff));
USART_RX_STA=0;// All bits are assigned 0
}
}
}
2. Realize it by yourself :

#define UART_BAUDRATE 115200
#define UART_RX_SIZE 256
char rx_buff[UART_RX_SIZE];
_Bool clear_buff_flag=0;
void uart_test_poll(uint8_t port)
{
int val,i=0;
bool_t err;
while (1)
{
os_delay(10);
val=iot_uart_rx(port);
if (val<0)
{
continue;
}
if(i<UART_RX_SIZE)
{
if((char)val=='\n')
{
if(rx_buff[i-1]=='\r')
{
iot_printf("rx_buff:%s",rx_buff);
iot_printf("UART_TX SIZE:%d,Task send:%d\r\n",UART_RX_SIZE,UART_SIZE);
if(uart_qeue!=NULL)
{
err=os_queue_send(uart_qeue,rx_buff);
if(err==false)
{
iot_printf("uart qeue full,data send fail\r\n");
}
}
clear_buff_flag=1;
}
}
else
{
rx_buff[i]=(char)val;
i++;
}
}
if(clear_buff_flag==1)
{
clear_buff_flag=0;
os_mem_set(rx_buff,0,sizeof(rx_buff));
i=0;
}
}
}
边栏推荐
- 在PDF中插入文本水印
- Verilog 每日一题(VL4 移位运算与乘法)
- Awk of shell script
- Iris framework practice of goweb development: project summary and review
- 高速电路中电感的选型和应用
- Using SQL server agent job to restore the database regularly
- Verilog 每日一题(VL26 简易秒表)
- [sqoop] sqoop 1.4.7 installation integration cdh5.13
- @RequestParam使用
- Uparse rich text style of uni app
猜你喜欢

【atlas】atlas 编译报错整理(全)

2021 年全国大学生数据统计与分析竞赛

Verilog 每日一题(VL29 单端口RAM)

Connection design and test platform -- Summary of SystemVerilog interface knowledge points

High speed circuit design practice -- Overview

Verilog daily question (vl27 settable counter)

在android开发过程中遇到.sqlite文件处理

Verilog 每日一题 (VL28 加减计数器)

【impala】【报错解决】 Impala cannot read or execute the parent directory of dfs.domain.socket.path的解决方法

AMQP protocol details
随机推荐
@RequestParam使用
Verilog 每日一题 (VL30 RAM的简单实现)
Export word according to the template, generate compound format tables and variable column tables
FreeRTOS learning notes
从非儿童网站看基线安全到底有多重要
Easy to use vscode plug-in memo
Batch download files
Flat data to tree
Verilog daily question (vl5 signal generator)
mysql实现按照自定义(指定顺序)排序
[atlas] atlas compilation error sorting (all)
Linear algebra and matrix theory (7)
Vscode plug-in automatically adds comments
influxdb2的使用
格雷码和二进制的转换及典型例题(4bits格雷码计数器)
C#基础面试题(附答案)
The actual combat of the beego framework of goweb development: Section III program execution process analysis
Verilog 每日一题(VL29 单端口RAM)
Valarray Library Learning
Self study examination in April 2021