英文:
ESP32 Consistently Restarting after Closing Serial Connection
问题
我对微控制器相关的知识相对较新,而且编程经验有限,但我无法理解为什么在我从我的Python脚本(PC)传输一些数据到我的ESP32后,它会重新启动。
我使用了Serial Transfer库来创建一个非常简单的程序,用于发送(PySerialTransfer)和接收(SerialTransfer)数据。这两个库是兼容的。我使用我的LCD来告诉我何时重新启动(因为我不能使用串行监视器进行调试,串口已被占用)。
ESP 代码
#include "SerialTransfer.h"
#include "SPI.h"
#include <TFT_eSPI.h> // 硬件特定库
TFT_eSPI tft = TFT_eSPI();
SerialTransfer myTransfer;
void setup()
{
Serial.begin(115200);
myTransfer.begin(Serial);
tft.begin();
tft.fillScreen(TFT_BLACK);
}
void loop()
{
if (myTransfer.available())
{
tft.fillScreen(TFT_BLUE);
}
}
Python 代码
import time
from pySerialTransfer import pySerialTransfer as txfer
if __name__ == '__main__':
link = txfer.SerialTransfer('COM4')
link.open()
float_val = 5.234
float_size = link.tx_obj(float_val) # 将浮点数打包到输出缓冲区
link.send(float_size)
time.sleep(2) # 等待2秒以查看颜色变化后再重新启动
link.close()
问题
当我运行我的Python代码时,我期望屏幕从黑色变为蓝色并保持蓝色,即使连接关闭后也是如此。但实际上,我得到的是屏幕在蓝色显示2秒后立即重新启动(屏幕闪烁白色然后变回黑色)。如果我将以下代码从我的Python脚本中放入一个while True:
循环中,屏幕将永远保持蓝色。我认为问题出在我的Python脚本中的 link.close()
。如果我删除它,即使在Python脚本终止后,它仍然会重新启动。我不知道这是否是预期行为。我的目标是在ESP运行时偶尔连接到它,从我的PC发送大量数据并在屏幕上显示,而不会发生重新启动。
这种行为在我具有相同性质的更大脚本中也是一样的。我的ESP将成功接收所有数据并成功执行其操作,但一旦我的Python脚本关闭串行连接,ESP就会重新启动。
另外,我不知道是否使用USB对于这种目标是理想的,还是应该使用蓝牙或WiFi。我打算将其连接到我的PC并通过USB供电24/7,所以我认为使用USB传输数据可能是最简单的方式。
英文:
I'm pretty new to microcontroller stuff and have some limited programming experience, but I cannot figure out why my ESP is restarting after I finish transferring some data over USB from my python script (PC) to my ESP32.
I made a very simple program using Serial Transfer library for both sending (PySerialTransfer) and recieving (SerialTransfer). These two libraries are compatible with each other. I am using my LCD to tell when it restarts (since I can't use Serial monitor for debugging, the com port is being used).
ESP CODE
#include "SerialTransfer.h"
#include "SPI.h"
#include <TFT_eSPI.h> // Hardware-specific library
TFT_eSPI tft = TFT_eSPI();
SerialTransfer myTransfer;
void setup()
{
Serial.begin(115200);
myTransfer.begin(Serial);
tft.begin();
tft.fillScreen(TFT_BLACK);
}
void loop()
{
if(myTransfer.available())
{
tft.fillScreen(TFT_BLUE);
}
}
PYTHON CODE
import time
from pySerialTransfer import pySerialTransfer as txfer
if __name__ == '__main__':
link = txfer.SerialTransfer('COM4')
link.open()
float = 5.234
float_size = link.tx_obj(float) # pack float into output buffer
link.send(float_size)
time.sleep(2) # sleep allows time to see color change before restart
link.close()
Problem
When I run my Python code, I expect my screen to turn from black to blue and stay blue, even after the connection is closed. Instead what I get is my screen turning blue for 2 seconds and then my ESP immediately restarting (screen flashes white and returns to black). If I place the code
float = 5.234
float_size = link.tx_obj(float) # pack float into output buffer
link.send(float_size)
from my python script into a while True: loop, the screen remains blue forever. It seems to me that the failing point is link.close()
in my python script. If I remove that, it still restarts at the termination of the python script anyways. I don't understand if this is intended behavior or not. My goal is to occasionally connect to my ESP while it is running, send it a large list of data from my PC, and display it on screen, without any restarts.
This kind of behavior is the same in my larger scripts of the same nature. My ESP will recieve all of the data fine and perform its operations successfully, but once my python script closes the serial connection, the ESP restarts.
Also sidenote, I don't know if using USB is ideal for this kind of goal, or if I should use bluetooth or wifi. I intend for this to be connected to my PC and powered 24/7 via USB so I assume USB would be easiest to transfer data.
答案1
得分: 0
事实证明,我需要在我的PC Python脚本中禁用DTR/RTS,像这样(使用pyserial),以阻止我的ESP重新启动。我不能再使用SerialTransfer库来实现这个功能,所以我将不得不重新编写一切,但至少它可以工作。
link = serial.Serial('COM4', 115200, dsrdtr=None)
link.setRTS(False)
link.setDTR(False)
英文:
Turns out I needed to disable DTR/RTS in my PC python script like so (using pyserial) in order to stop my ESP from restarting, I can't use the SerialTransfer library for this anymore so I will have to rewrite everything but at least it works
link = serial.Serial('COM4', 115200, dsrdtr=None)
link.setRTS(False)
link.setDTR(False)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论