我遇到了一些问题,试图控制一个从python连接到arduino板的伺服系统。在程序中,我编写了一个值0- 180,并将其发送到Arduino,Arduino应该将伺服转到所选的位置。问题是从python发送的数据没有正确读取是接缝。(或书写正确)。在谷歌搜索了很多次之后,尝试和失败,我仍然有同样的问题。当从python发送数据时,te伺服从起始位置移动到几乎中心,而不是回到起点,
我现在更改了代码,所以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和字节,但是结果不是完全相同的,而是几乎相同的。
感谢你的帮助。
我使用:Python3.4、pyserial和Windows 10。
发布于 2016-03-07 16:13:49
Dave帮助我意识到,这是与arduino代码有关的东西,它只读了一个字符的字符串。在谷歌上搜索后,我发现要正确读取数据,我必须使用‘
Serial.parseInt()`
这个
if (Serial.available() > 0)
当伺服移回起始位置时,确实解决了问题;谢谢戴夫!
我的arduino代码no看起来像:
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度之间的数字来控制软件中的伺服:
发布于 2016-03-07 13:05:59
我认为你的问题在于代码的arduino方面。当您通过串行协议发送数据时,所有纯文本(包括整数)都会转换为ASCII。例如,小写的"a“被转换为97,所以当您说:
byte incomingByte = Serial.read();
pos = incomingByte;
myservo.write(pos);
你想把ASCII数字写到伺服系统上,所以如果你写42,你可能得到5250而不是42。解决这一问题的一种可能方法是将字节转换为char:
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
https://stackoverflow.com/questions/35852440
复制相似问题