当前位置:网站首页>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;
}
}
}
边栏推荐
- Gray code and binary conversion and typical examples (4bits gray code counter)
- [kibana] problem sorting kibana 7.x no indices match pattern "APM-*“
- 【CDH】通过 ClouderaManager 配置CDH组件用 prometheus 监控采集JMX信息
- Deploy lamp platform -- compilation and installation of Linux, Apache, MySQL and PHP
- MySQL的触发器
- 异步电路设计--同步脉冲器原理及例题
- 谈谈“发布后问题”的度量
- Soft exam review summary
- Verilog daily question (vl6 data series to parallel circuit)
- MySQL数据库增删改查(基础操作命令详解)
猜你喜欢

Verilog 每日一题 (VL24 多bit MUX同步器 跨时域输出)

MySQL的触发器

Introduction to vscade interface

The practice of the beego framework of goweb development: Section II project initialization configuration

MySQL详细学习教程(建议收藏)

Verilog daily question (vl29 single port RAM)

异步电路设计--同步脉冲器原理及例题

高速电路中电容的选型和应用——详解

LNMP source code compilation and installation
![[CDH] configure CDH components through clouderamanager and collect JMX information with Prometheus monitoring](/img/6a/bbc1ab0cfae9139308da4ded1376a8.png)
[CDH] configure CDH components through clouderamanager and collect JMX information with Prometheus monitoring
随机推荐
Andthen of function interface
C#基础面试题(附答案)
Create a custom paging control
JS中为对象数组添加新对象结果导致数组中已存在的对象也修改了
Using SQL server agent job to restore the database regularly
在PDF中插入文本水印
js将本地时间与服务器时间同步
C # basic interview questions (with answers)
Introduction to vscade interface
Blue Bridge Cup embedded competition resources and skills
Shell编程之Sed
[Presto] common commands of Presto
Verilog daily question (vl29 single port RAM)
Verilog daily question (simple implementation of VL30 RAM)
数据库优化——深入理解Mysql索引底层数据结构与算法
Awk of shell script
Net framework
在android开发过程中遇到.sqlite文件处理
渗透测试大杀器kali安装配置
Encountered.Sqlite file processing during Android Development