1 回答

TA貢獻(xiàn)1818條經(jīng)驗 獲得超8個贊
您需要發(fā)送更多字節(jié)來表示每個數(shù)字。假設(shè)每個數(shù)字使用 4 個字節(jié)。請注意,此代碼需要適應(yīng) arduino 字節(jié)順序。在 python 方面,你必須執(zhí)行以下操作:
def mouse_move(x, y):
bytes = x.to_bytes(4, byteorder = 'big') + y.to_bytes(4, byteorder = 'big')
arduino.write(bytes)
print(pax)
在接收方,您需要從字節(jié)組成部分重建數(shù)字,如下所示:
byte bytes[4]
void loop() {
int x,y; /* use arduino int type of size 4 bytes */
if (Serial.available() > 0) {
Serial.readBytes(bytes, 4);
x = bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[0]
Serial.readBytes(bytes, 4);
y = bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[0]
Mouse.move(x, y, 0);
Serial.read();
}
}
添加回答
舉報