Python 和 Arduino 问题
Python and Arduino issues
我遇到了一些问题,试图从 python 控制连接到 arduino 板的伺服系统。
在程序中我写了一个值 0 – 180 并将它发送到 Arduino,Arduino 应该将伺服器转到选定的位置。问题是从 python 发送的数据没有被正确读取。 (或写得正确)。经过多次谷歌搜索、尝试和失败后,我仍然遇到同样的问题。
当从 python 发送数据时,伺服从起始位置移动到几乎中心,然后回到起始点,
我现在更改了代码,所以 Arduino 现在回复 python 它收到的数据,我不明白发生了什么。我输入值 1。Arduino 回复:b'5。如果我写 1。Arduino 回复 b'2。 .. 如果我写 2,它会以 b'5 响应,如果我写 5 :S(并且它并不总是相同的)
我使用的Python代码:
import serial
def sendSerialData():
global set_ser, run
set_ser = serial.Serial()
set_ser.port="COM4"
set_ser.baudrate=9600
print('**********************')
print('* Serial comunicator *')
print('**********************\n')
run = 0
while run==0:
print('type \'open\'to open serial port. \ntype \'close\' anyplace in the procram to close serial port:')
openSerial = input(': ').lower()
if (openSerial == "open"):
set_ser.close()
set_ser.open()
while set_ser.isOpen():
output = input('\nType what you want to send, hit enter\n: ')
set_ser.write(output.encode())
print('Arduino is retriving: ')
print(set_ser.read())
if (output == "close"):
set_ser.close()
print ('Closed')
a = 1
elif (openSerial == "close"):
set_ser.close()
a = 1
else:
print ('open or close')
sendSerialData()
Arduino代码:
int pos = 0; // variable to store the servo position
int incomingByte= 180;
void setup() {
myservo.attach(5); // attaches the servo on pin 5 to the servo object
Serial.begin(9600);
}
void loop() {
byte incomingByte = Serial.read();
pos = incomingByte;
myservo.write(pos);
Serial.print(pos);
delay(500);
这是程序的输出:
type 'open'to open serial port.
type 'close' anyplace in the procram to close serial port:
: open
Type what you want to send, hit enter
: 100
Arduino is retriving:
b'\xff'
Type what you want to send, hit enter
: 1
Arduino is retriving:
b'2'
Type what you want to send, hit enter
: 5
Arduino is retriving:
b'5'
Type what you want to send, hit enter
: 2
Arduino is retriving:
b'5'
Type what you want to send, hit enter
有人知道如何解决这个问题吗?我需要将二进制转换为十进制吗?我尝试将 incommingByte 声明为 int 和 byte,但结果并不完全相同,但几乎相同。
感谢您的帮助。
我使用:
Python 3.4,pyserial 和 Windows 10。
我认为你的问题出在代码的arduino端。当您通过串行协议发送数据时,所有纯文本(包括整数)都会转换为 ASCII。例如,小写字母 "a" 会转换为 97,因此当您说:
byte incomingByte = Serial.read();
pos = incomingByte;
myservo.write(pos);
您正在尝试将 ASCII 数字写入伺服系统,因此如果您写入 42,您可能会得到 5250 而不是 42。解决此问题的一种可能方法是将字节转换为字符:
byte incomingByte = Serial.read();
pos = char(incomingByte);
myservo.write(pos);
您可能还想仅在接收数据时执行此操作:
if (Serial.available() > 0) {
byte incomingByte = Serial.read();
pos = char(incomingByte);
myservo.write(pos);
希望对您有所帮助!
-戴夫
Dave 帮助我意识到这是 arduino 代码的问题,它只读取字符串的 1 个字符。经过一些谷歌搜索后,我发现要正确读取数据,我必须使用`
Serial.parseInt()`
if (Serial.available() > 0)
确实解决了舵机回到起始位置的问题;谢谢戴夫!
我的 arduino 代码看起来像:
int pos; // variable to store the servo position
int incomingByte;
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
void setup() {
myservo.attach(5); // attaches the servo on pin 5 to the servo object
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
int incomingByte = Serial.parseInt();
myservo.write(incomingByte);
}
delay(500);
}
我现在可以通过输入 0 到 180 度之间的数字从软件控制我的伺服:
我遇到了一些问题,试图从 python 控制连接到 arduino 板的伺服系统。 在程序中我写了一个值 0 – 180 并将它发送到 Arduino,Arduino 应该将伺服器转到选定的位置。问题是从 python 发送的数据没有被正确读取。 (或写得正确)。经过多次谷歌搜索、尝试和失败后,我仍然遇到同样的问题。 当从 python 发送数据时,伺服从起始位置移动到几乎中心,然后回到起始点,
我现在更改了代码,所以 Arduino 现在回复 python 它收到的数据,我不明白发生了什么。我输入值 1。Arduino 回复:b'5。如果我写 1。Arduino 回复 b'2。 .. 如果我写 2,它会以 b'5 响应,如果我写 5 :S(并且它并不总是相同的)
我使用的Python代码:
import serial
def sendSerialData():
global set_ser, run
set_ser = serial.Serial()
set_ser.port="COM4"
set_ser.baudrate=9600
print('**********************')
print('* Serial comunicator *')
print('**********************\n')
run = 0
while run==0:
print('type \'open\'to open serial port. \ntype \'close\' anyplace in the procram to close serial port:')
openSerial = input(': ').lower()
if (openSerial == "open"):
set_ser.close()
set_ser.open()
while set_ser.isOpen():
output = input('\nType what you want to send, hit enter\n: ')
set_ser.write(output.encode())
print('Arduino is retriving: ')
print(set_ser.read())
if (output == "close"):
set_ser.close()
print ('Closed')
a = 1
elif (openSerial == "close"):
set_ser.close()
a = 1
else:
print ('open or close')
sendSerialData()
Arduino代码:
int pos = 0; // variable to store the servo position
int incomingByte= 180;
void setup() {
myservo.attach(5); // attaches the servo on pin 5 to the servo object
Serial.begin(9600);
}
void loop() {
byte incomingByte = Serial.read();
pos = incomingByte;
myservo.write(pos);
Serial.print(pos);
delay(500);
这是程序的输出:
type 'open'to open serial port.
type 'close' anyplace in the procram to close serial port:
: open
Type what you want to send, hit enter
: 100
Arduino is retriving:
b'\xff'
Type what you want to send, hit enter
: 1
Arduino is retriving:
b'2'
Type what you want to send, hit enter
: 5
Arduino is retriving:
b'5'
Type what you want to send, hit enter
: 2
Arduino is retriving:
b'5'
Type what you want to send, hit enter
有人知道如何解决这个问题吗?我需要将二进制转换为十进制吗?我尝试将 incommingByte 声明为 int 和 byte,但结果并不完全相同,但几乎相同。
感谢您的帮助。
我使用: Python 3.4,pyserial 和 Windows 10。
我认为你的问题出在代码的arduino端。当您通过串行协议发送数据时,所有纯文本(包括整数)都会转换为 ASCII。例如,小写字母 "a" 会转换为 97,因此当您说:
byte incomingByte = Serial.read();
pos = incomingByte;
myservo.write(pos);
您正在尝试将 ASCII 数字写入伺服系统,因此如果您写入 42,您可能会得到 5250 而不是 42。解决此问题的一种可能方法是将字节转换为字符:
byte incomingByte = Serial.read();
pos = char(incomingByte);
myservo.write(pos);
您可能还想仅在接收数据时执行此操作:
if (Serial.available() > 0) {
byte incomingByte = Serial.read();
pos = char(incomingByte);
myservo.write(pos);
希望对您有所帮助!
-戴夫
Dave 帮助我意识到这是 arduino 代码的问题,它只读取字符串的 1 个字符。经过一些谷歌搜索后,我发现要正确读取数据,我必须使用`
Serial.parseInt()`
if (Serial.available() > 0)
确实解决了舵机回到起始位置的问题;谢谢戴夫!
我的 arduino 代码看起来像:
int pos; // variable to store the servo position
int incomingByte;
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
void setup() {
myservo.attach(5); // attaches the servo on pin 5 to the servo object
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
int incomingByte = Serial.parseInt();
myservo.write(incomingByte);
}
delay(500);
}
我现在可以通过输入 0 到 180 度之间的数字从软件控制我的伺服: