提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、pandas是什么?
-
二、使用步骤
- 1.引入库
- 2.读入数据
- 总结
前言
当主控芯片发送一串数据,但此数据为一串字符串,需要将此字符串翻译成整型数组,以形成指令,控制串口屏。
例如:esp32单片机接收到字符串:{"action_type":2,"tem_in":35,"tem_in_warn":50,"tem_out":23,"tem_out_warn":50,"data_type":1},就将里面的'2','35','50','23',等数据解析出来,形成整形数据,替换数组中相应的数据,最终将数组发送给串口屏,形成一系列指令。
一、串口是什么?
串行接口简称串口,也称串行通信接口或串行通讯接口,是采用串行通信方式的扩展接口。串行接口是指数据一位一位地顺序传送。
二、使用步骤
1.引入库
代码如下:
#include <HardwareSerial.h> //导入ESP32串口操作库,使用这个库我们可以把串口映射到其他的引脚上使用
2.解码函数
代码如下:文章来源:https://www.toymoban.com/news/detail-708073.html
#include <HardwareSerial.h> //导入ESP32串口操作库,使用这个库我们可以把串口映射到其他的引脚上使用
#include <ArduinoJson.h>
HardwareSerial MySerial_724UG(1); //使用串口时一定要考虑端口重映射
String comdata = "";
uint8_t DataReceive[10];
uint8_t data1[]={0xAA,0x01,0x13,0x00,0x06,0xff,0x40,0xfe,0x0a,0xfd,0x02,0x00}; //转化效率显示页面
uint8_t data2[]={0xAA,0x01,0x14,0x00,0x0a,0xff,0x40,0xfe,0x45,0xfd,0x32,0xfc,0x40,0xfb,0x02,0x00}; //水温值显示页面
uint8_t data3[]={0xAA,0x01,0x15,0x00,0x06,0xff,0x01,0xfe,0x32,0xfd,0x02,0x00}; //
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
MySerial_724UG.begin(115200,SERIAL_8N1,19,21); //端口重映射
}
void loop() {
// put your main code here, to run repeatedly:
while (MySerial_724UG.available() > 0)
{
comdata += char(MySerial_724UG.read());
delay(2);
}
DynamicJsonDocument doc(1024);
deserializeJson(doc, comdata); //解码函数
// You can use a String as a key to get a member from JsonDocument
// No duplication is done.
long num = doc[String("action_type")]; //解析数据,将字符串后面的数据解析出来
switch(num)
{
case 1:
//{"action_type":1,"sw_rate":60,"sw_rate_warn":10,"data_type":1}
data1[6]=doc[String("sw_rate")];
data1[8]=doc[String("sw_rate_warn")];
data1[10]=doc[String("data_type")];
//Serial.println(data1[8]);
Serial.write(data1,sizeof(data1));
delay(50);
Serial.write(data1,sizeof(data1));
break;
case 2:
//{"action_type":2,"tem_in":35,"tem_in_warn":50,"tem_out":23,"tem_out_warn":50,"data_type":1}
data2[6]=doc[String("tem_in")];
data2[8]=doc[String("tem_in_warn")];
data2[10]=doc[String("tem_out")];
data2[12]=doc[String("tem_out_warn")];
data2[14]=doc[String("data_type")];
Serial.write(data2,sizeof(data2));
delay(50);
Serial.write(data2,sizeof(data2));
break;
case 3:
//{"action_type":3,"resin_type":1,"resin_progress":50,"data_type":1}
data3[6]=doc[String("resin_type")];
data3[8]=doc[String("resin_progress")];
data3[10]=doc[String("data_type")];
Serial.write(data3,sizeof(data3));
delay(50);
Serial.write(data3,sizeof(data3));
break;
default:break;
}
comdata = "";
}
#include <HardwareSerial.h> //导入ESP32串口操作库,使用这个库我们可以把串口映射到其他的引脚上使用
HardwareSerial MySerial_stm32(0);
String comdata = "";
uint8_t DataReceive[10];
uint8_t data1[]={0xAA,0x01,0x13,0x00,0x06,0xff,0x40,0xfe,0x0a,0xfd,0x02,0x00}; //发送给串口屏的数据
uint8_t data2[]={0xAA,0x01,0x14,0x00,0x0a,0xff,0x40,0xfe,0x45,0xfd,0x32,0xfc,0x40,0xfb,0x02,0x00}; //水温值显示页面
uint8_t data3[]={0xAA,0x01,0x15,0x00,0x06,0xff,0x01,0xfe,0x32,0xfd,0x02,0x00}; //
//解码函数
void Decode(String str){
int ret = 16; //记录冒号的位置
int reg = 0; //记录逗号的位置
int i=0;
String str_num=""; //接收解码的数据
while(1)
{
ret = comdata.indexOf(":",ret); //寻找从ret位置开始,第一个冒号的位置
reg = comdata.indexOf(",",ret); //寻找从ret位置开始,第一个逗号的位置
if(ret==-1)
{
break;
}
if(reg==-1)
{
reg = comdata.indexOf("}",ret);
}
//Serial.println(ret);
//Serial.println(reg);
str_num=str.substring(ret + 1, reg); //截取字符串中有效数据
//Serial.println(str_num);
DataReceive[i] = str_num.toInt(); //将字符串转为整形数据存于数组中
i++;
ret = reg +1;
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); //设置串口波特率
}
void loop() {
// put your main code here, to run repeatedly:
while (Serial.available() > 0)
{
comdata += char(Serial.read());
delay(2);
}
//Serial.println(comdata);
// 在字符串中间查找子字符串
if (comdata.substring(2, 13) == "action_type") {
//Serial.println("It's a text-based file");
int num = comdata.charAt(15) - '0';
//Serial.println(num);
Decode(comdata);
switch(num)
{
case 1:
data1[6]=DataReceive[0];
data1[8]=DataReceive[1];
data1[10]=DataReceive[2];
//Serial.println(data1[8]);
Serial.write(data1,sizeof(data1)); //将数据发送给串口屏
delay(50);
Serial.write(data1,sizeof(data1));
break;
case 2:
data2[6]=DataReceive[0];
data2[8]=DataReceive[1];
data2[10]=DataReceive[2];
data2[12]=DataReceive[3];
data2[14]=DataReceive[4];
Serial.write(data2,sizeof(data2));
delay(50);
Serial.write(data2,sizeof(data2));
break;
case 3:
data3[6]=DataReceive[0];
data3[8]=DataReceive[1];
data3[10]=DataReceive[2];
Serial.write(data3,sizeof(data3));
delay(50);
Serial.write(data3,sizeof(data3));
break;
default:break;
}
}
comdata = ""; //清空上次接收到的字符串数据
}
总结
例如:以上即是串口接收解析代码,经验证,已经可以使用。文章来源地址https://www.toymoban.com/news/detail-708073.html
到了这里,关于esp32单片机在arduino环境下,串口接收解码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!