我跟随this guide尝试学习串行通信,但代码似乎无法正常工作
#Arduino Code
String InBytes;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(4, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
InBytes = Serial.readStringUntil('\n');
if (InBytes == "on") {
digitalWrite(4, HIGH);
Serial.write("LED ON");
}
if (InBytes == "off") {
digitalWrite(LED_BUILTIN, LOW);
Serial.write("LED OFF");
}
else {
Serial.write("invalid information");
}
}
}
#Python Code
import serial
import time
serialcomm = serial.Serial('COM3', 115200)
serialcomm.timeout = 1
def main():
while True:
i = input("input(on/off): ").strip()
if i == 'done':
print('finished program')
break
serialcomm.write(i.encode())
time.sleep(0.5)
print(serialcomm.readline().decode('ascii'))
serialcomm.close()
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
main()
我只更改了pin码和COM端口
当我同时运行Arduino代码和Python代码时,我能够看到Python端的输出,但是Arduino代码没有向Python终端输出任何内容。我也没有收到任何错误消息,所以我不知道原因是什么
如果这很重要,我将使用PyCharm作为python编辑器和ESP8266的Arduino IDE
您的Arduino代码正在将串行线的速度设置为9600
Python代码将其设置为115200
你需要选择一个并保持一致。尝试将Python代码更改为使用9600
相关问题 更多 >
编程相关推荐