我需要将鼠标坐标从 python 发送到 arduino。如您所知,有 X 轴和 Y 轴,这些轴上有一些负值,例如 -15 或 -10 等。Arduino 的串口只接受字节,因此字节限制为 0 到 256。我的问题就从这里开始。我无法将负值从 python 发送到 arduino。这是我的 python 代码:
def mouse_move(x, y):
pax = [x,y]
arduino.write(pax)
print(pax)
例如,当 x 或 y 为负值(如 -5 )时,程序会崩溃,因为字节数组为 0-256 。
这是我的arduino的代码:
#include <Mouse.h>
byte bf[2];
void setup() {
Serial.begin(9600);
Mouse.begin();
}
void loop() {
if (Serial.available() > 0) {
Serial.readBytes(bf, 2);
Mouse.move(bf[0], bf[1], 0);
Serial.read();
}
}
弑天下
相关分类