如何使用 Python 将 int 发送到 arduino

How to send int to arduino with Python

我试图用 Python 来控制伺服,但我只有 Arduino 可以控制伺服。我发现如何通过写入数字来控制伺服,但我无法将 int 发送到 arduino.I 正在尝试发送此

first = 680/ a
last = 180/ first

我想发送 'last' 但我做不到。请帮助我,抱歉我的英语不好。

这是连接到 Arduino 并向其发送数据的代码:

import serial
port = serial.Serial("/path/to/your/arduino/port", 9600)
port.write(bytes("180", "utf-8")) # This is just an example; you can use any string

# Close the port when you're done talking to the Arduino
port.close()

注意在发送之前需要将字符串转换为bytes,并且需要指定编码。只需使用您的程序使用的任何编码。在这里,它是 UTF-8。