Arduino与Python的串行通信

2024-09-28 17:24:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在Arduino和Python之间建立通信。我想从arduino以字节的形式写int值并用Python读取它们,但我需要一些帮助。你知道吗

这是我的Arduino代码:

long prev = millis();

void setup(){
Serial.begin(115200); 
}

void loop(){
 long current = millis(); 
  if(current - prev > 5000){
     Serial.write(32760); 
     prev = millis();
  }
}

下面是我的Python代码:

import serial

arduino = serial.Serial('/dev/ttyUSB1', 115200)
while True:
    data = arduino.read(); 
    print("number: ", int(data.encode('hex'),16));

Python端的输出是('number: ', 63736),这与预期不同。你知道吗

我曾尝试使用ord()函数打印它,但没有任何效果。 事先谢谢,如有任何帮助将不胜感激。你知道吗


Tags: 代码numberdata字节serialcurrent形式arduino