我有一个问题,我正在尝试通过 ESP32 读取 Arduino IDE 上编写的代码,但打印的数据显示为 b'' 我已尝试使用 Arduino UNO 执行相同的代码ESP32 的并且运行良好。这是代码:
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 5 20:28:19 2021
@author: Andres
"""
import serial
arduino = serial.Serial('COM8', baudrate=115200, timeout=1.0)
while(True):
line = arduino.readline().decode('utf-8')
print(line)
这是ESP32代码(只是通过串口发送数字):
int16_t x=33;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(x);
delay(100);
x=x+1;
if(x==97)
{
x=32;
}
}
我必须说我已经尝试过波特率 9600 和 115200。 我尝试在其他计算机上更改端口名称,但没有成功。
最佳答案
在打开端口之前,您需要将RTS和DTR设置为False
:
import serial
ser = serial.Serial()
ser.port = 'COM11'
ser.baudrate = 115200
ser.setDTR(False)
ser.setRTS(False)
ser.open()
while True:
b = ser.readline()
print(b)
关于python - 从 ESP32 读取数据到 Python 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66502267/