第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過pyserial問題發(fā)送負(fù)值

通過pyserial問題發(fā)送負(fù)值

富國滬深 2023-08-22 14:44:40
我需要將鼠標(biāo)坐標(biāo)從 python 發(fā)送到 arduino。如您所知,有 X 軸和 Y 軸,這些軸上有一些負(fù)值,例如 -15 或 -10 等。Arduino 的串口只接受字節(jié),因此字節(jié)限制為 0 到 256。我的問題就從這里開始。我無法將負(fù)值從 python 發(fā)送到 arduino。這是我的 python 代碼:def mouse_move(x, y):    pax = [x,y]    arduino.write(pax)    print(pax)例如,當(dāng) x 或 y 為負(fù)值(如 -5 )時,程序會崩潰,因為字節(jié)數(shù)組為 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();  }}
查看完整描述

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();

  }

}


查看完整回答
反對 回復(fù) 2023-08-22
  • 1 回答
  • 0 關(guān)注
  • 1606 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號