当前位置:网站首页>Using 8266 as serial port debugging tool
Using 8266 as serial port debugging tool
2022-04-23 18:55:00 【Brick Porter】
There are students who do industrial control everyday , I must have had this experience , Some machines or equipment use serial communication, but debugging the real object is too troublesome or the cost is too high . At this time, if there is something that can simulate the equipment, it will be twice as much work . Generally speaking, the protocol of serial communication process is relatively simple , It's usually simple to send and receive . Looking for a circle of software serial ports on the Internet generally charges and uses drivers , Compatibility and use cost are relatively high .
There's just one in my hand CH340 Of USB Serial port and 8266, and 8266 Support network , So if you use a program to connect to 8266.8266 The tools responsible for forwarding serial port and network can not be debugged perfectly . Of course, it also expands the effect of direct remote debugging of equipment , and 8266+340 The cost is just 10 Yuan , Add a few lines of code to solve the problem perfectly .
This article only discusses the use of software simulation equipment . under these circumstances 8266 It's a transit , Read the serial port data from the upper computer software and forward it to and 8266 Network connected Software , At this time, the data sent by the upper level can be received on the software , At the same time, the software can make various responses , To test the correctness of the data processed by the host computer .
In order to facilitate testing and software connection , Here is the 8266 Of IP Write dead . The code is as follows :
WiFi.mode(WIFI_STA);
Serial.begin(115200);
IPAddress local_IP(192, 168, 1, 166);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
// Set up
bool bConfigSucessed = WiFi.config(local_IP, gateway, subnet); // Set static IP
Then connect to wifi, Start a server to receive network connections .
// Global server , Listening port 9527
WiFiServer server(9527);
// Start server
server.setNoDelay(true);
server.begin();
The process is relatively simple , Receive serial port data software to the connected network end , Receive network data software to serial port .
void loop()
{
static WiFiClient client = server.available();
if (!client)
{
client = server.available();
}
if (!client)
{
return;
}
client.setNoDelay(true);
String comdata = "";
char buf[101];
while (Serial.available() > 0) // The number of characters received by the serial port is greater than zero .
{
int size = Serial.read(buf, 100);
buf[size] = '\0';
comdata += buf;
delay(2);
}
/****************** Serial port print data ************************/
if (comdata != "") // If you receive data
{
//Serial.println(comdata);
client.print(comdata);
client.flush();
}
while (client.available())
{
// Read client initiated TCP request
unsigned char len = 0;
size_t readsize = client.readBytes((char *)&len, 1);
//Serial.println(readsize);
//Serial.println(len);
if (len > 200)
{
client.stop();
return;
}
char *buf = new char[len];
readsize = client.readBytes(buf, len);
buf[readsize] = '\0';
String req = buf;
delete[] buf;
//Serial.println(req);
if (req.startsWith("com:"))
{
//Serial.println("COM");
String msg = req.substring(4);
Serial.updateBaudRate(atoi(msg.c_str()));
}
else if (req.startsWith("send:"))
{
//Serial.println("SEND");
String msg = req.substring(5);
//Serial.println(msg.length());
Serial.print(msg);
}
}
}
A minimalist protocol is implemented here , One was used byte To save the data length , Only... Can be received here 200 Data within , Of course, it can be extended , However, a message in general communication is less than 100 , Generally enough , You can also split long data into 200 Forwarding within . Also, only one client can be accepted here , If necessary, it can also be extended . thus 8266 It's basically working .
版权声明
本文为[Brick Porter]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210603256921.html
边栏推荐
- 2022.04.23(LC_714_买卖股票的最佳时机含手续费)
- Sword finger offer II 116 Number of provinces - spatial complexity O (n), time complexity O (n)
- ctfshow-web362(SSTI)
- os_authent_prefix
- Summary of actual business optimization scheme - main directory - continuous update
- 7、 DOM (Part 2) - chapter after class exercises and answers
- About the operation of unit file reading (I)
- Deeply understand what new and make in golang are and what are the differences?
- Golang 语言实现TCP UDP通信
- Query the logistics update quantity according to the express order number
猜你喜欢
ctfshow-web362(SSTI)
STM32: LCD显示
STM32: LCD display
2022.04.23 (the best time for lc_714_to buy and sell stocks, including handling charges)
昇腾 AI 开发者创享日全国巡回首站在西安成功举行
mysql_ Download and installation of Linux version
Getting started with vcpkg
How to virtualize the video frame and background is realized in a few simple steps
【科普】CRC校验(一)什么是CRC校验?
使用晨曦记账本,分析某个时间段每个账户收支结余
随机推荐
Practice of Druid SQL and security in meituan review
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
Résolution: cnpm: impossible de charger le fichier... Cnpm. PS1 parce que l'exécution de scripts est désactivée sur ce système
ESP32 LVGL8. 1 - bar progress bar (bar 21)
Zlib realizes streaming decompression
12个例子夯实promise基础
【科普】CRC校验(一)什么是CRC校验?
【历史上的今天】4 月 23 日:YouTube 上传第一个视频;网易云音乐正式上线;数字音频播放器的发明者出生
Machine learning theory (7): kernel function kernels -- a way to help SVM realize nonlinear decision boundary
22 year flying Book manpower Kit
Introduction to ROS learning notes (I)
After opening the original normal project, the dependency package displays red and does not exist.
Usage of functions decode() and replace() in SQL
Sword finger offer II 116 Number of provinces - spatial complexity O (n), time complexity O (n)
RPM包管理
Seata处理分布式事务
Xlslib use
回路-通路
Use of kotlin collaboration in the project
MVVM model