我在pySerial中使用了Arduino和python3.4,并试图通过串行通信发送多个字节,但是由于我的代码正在运行,所以出现了一些问题,但是出于调试目的,我在某些地方使用的print语句输出的字符串是我不期望的。我已经试着写了这个代码的一个简化版本,它可以正常工作,所以我不确定我做错了什么! 另外,为了解决这个问题,我添加了一个if语句,在该语句中,代码的速度大大减慢了。对于如何简化代码以提高速度和/或如何发送和接收正确的数据,我们将不胜感激!在
Python:
#sources:
#http://stackoverflow.com/questions/676172/full-examples-of-using-pyserial-package for more info
#http://pyserial.sourceforge.net/pyserial_api.html
import serial
import time
#sets the connection parameters, relook at when know more
ser = serial.Serial(
port ='COM4',
baudrate = 9600,
parity = serial.PARITY_ODD,
stopbits = serial.STOPBITS_TWO,
bytesize = serial.EIGHTBITS,
timeout = 5
)
def decToPercent (decimal):
#maps value from 0-1 to 0-255 and makes into an interger, then to hex
intPercent = round(decimal*255)
return intPercent
ser.isOpen() #returns true?
firstContact = False
inByte = 0
wrist = 0.9 #setup of variables which I will get from dictionary of other code once integrate
elbow = 0 #assuming variables will be included in (0-1)
wristPerc = decToPercent(wrist)
elbowPerc = decToPercent(elbow)
forceBytes = bytearray([wristPerc, elbowPerc]) #puts the motor values into an array as hex
print(forceBytes)
#set up initial contact with the arduino
inByte = ser.read(1)
print(inByte)
while True:
if (firstContact == False):
if inByte == b'A' :
firstContact = True
ser.write('a'.encode())
inByte = ser.read(1)
else:
ser.write(forceBytes)
time.sleep(0.05)
print(ser.readline())
最初,我让代码开始,并在使用了这段代码之后返回整个while语句,但是我把它去掉了,因为我认为这会使代码流稍微扰乱输出:
^{pr2}$Arduino代码:
//See Arduino Cookbook chapter 4 for info on sending and recieving multiple messages in one
//See example of serial call response for strategy on how to process information faster
int motorPinLeft = 10;
int motorPinRight = 11;
int motorSpeedLeft = 0;
int motorSpeedRight = 0;
int fieldIndex = 1;
char values[2]; //array holding values for the fields we expect
char newByte = 0;
void setup() {
pinMode(motorPinLeft, OUTPUT);
pinMode(motorPinRight, OUTPUT);
Serial.begin(9600);
while(!Serial){}; //waits for arduino to be ready, not neaded for duo
establishContact();
}
void loop()
{
while (Serial.available()>0) //checks to see if anything needs to be sent,
{ //denoted by recieveing signal from pySerial
char inByte = Serial.read();
switch (inByte)
{
case 97:
inByte = 0;
break;
default:
values[0] = inByte;
//could just call Serial.read and asign 2nd byte, but loop allows to send more variables later
if (fieldIndex < 2)
{
newByte = Serial.read();
values[fieldIndex] = newByte;
fieldIndex++;
}
else
{
Serial.print(values[0], DEC); //debugging purposes
Serial.print(values[1], DEC);
motorSpeedLeft = values[0];
motorSpeedRight = values[1];
//Serial.print(motorSpeedLeft);
//Serial.print(motorSpeedRight);
analogWrite(motorPinLeft, motorSpeedLeft);
analogWrite(motorPinRight, motorSpeedRight);
delay(3000); // to make the motor vibrate for 1 second
motorSpeedRight = 0;
motorSpeedLeft = 0;
analogWrite(motorPinLeft, motorSpeedLeft);
analogWrite(motorPinRight, motorSpeedRight);
delay(1000);
}
}
//Serial.print('A');
}
}
//allows arduino to be ready before pySerial sends any messages
void establishContact()
{
while (Serial.available()<=0)
{
Serial.print('A');
delay(300);
}
}
输出:
bytearray('b\xb2\x00')
b'A'
b'0K\xfe'
b'-\x13j10k\xfe'
b'-\x13j10k\xfe'
每次打印语句发生时,我的电机都在运行,所以我不确定是不是我的打印语句出错了,而不是实际的串行通信?在
另外一件我不确定的事情是,在arduino代码中的switch case语句中,我使用ascii代码表示“a”(97),而不是实际地将“a”。两者都能用吗?在
在经历了更多的混乱之后,我通过从连续打印()至串行写入()在arduino代码中,由于我非常确定这是主要问题,所以我回到了原来的代码,没有使用if语句来减少运行代码所需的时间。我在打印两个输出时确实有点困难,但是通过输入:
介于串行写入声明,它修复了这个问题。在
相关问题 更多 >
编程相关推荐