Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过pyserial问题发送负值_Python_Python 3.x_Arduino_Pyserial_Atmega32 - Fatal编程技术网

Python 通过pyserial问题发送负值

Python 通过pyserial问题发送负值,python,python-3.x,arduino,pyserial,atmega32,Python,Python 3.x,Arduino,Pyserial,Atmega32,我需要将鼠标坐标从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

我需要将鼠标坐标从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();
  }
}
#包括
字节bf[2];
无效设置(){
Serial.begin(9600);
鼠标。开始();
}
void循环(){
如果(Serial.available()>0){
Serial.readBytes(bf,2);
鼠标移动(bf[0],bf[1],0);
Serial.read();
}
}

您需要发送更多字节来表示每个数字。 假设每个数字使用4个字节。 请注意,此代码需要适应arduino endianess。 在python方面,您必须执行以下操作:

def mouse_move(x, y):
    bytes = x.to_bytes(4, byteorder = 'big') + y.to_bytes(4, byteorder = 'big')
    arduino.write(bytes)

    print(pax)
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();
  }
}
在接收器端,您需要从字节组成部分重构数字 比如:

def mouse_move(x, y):
    bytes = x.to_bytes(4, byteorder = 'big') + y.to_bytes(4, byteorder = 'big')
    arduino.write(bytes)

    print(pax)
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();
  }
}
字节[4]
void循环(){
int x,y;/*使用大小为4字节的arduino int类型*/
如果(Serial.available()>0){
Serial.readBytes(字节,4);

x=字节[0]是否足以以足够的精度传输鼠标坐标?我猜您必须决定以更多字节发送坐标(例如每个方向4个字节)并实现您自己的否定约定,例如为签名保留最重要的位谢谢您的评论,实际上我无法将您的代码编译到我的代码中。要了解此主题的基本知识,您能告诉我应该在web上搜索哪些关键字吗?您好。您可以在这里查看,了解数字是如何在内存或yo中编码的在这种情况下,它们如何通过串行链路传输: