I am using Arduino and Python 3.4 with pySerial and am trying to send multiple bytes through serial communication, but am having issues since my code is running, but the print statements I have in certain places for debugging purposes are outputting strings which I am not expecting. I have tried writing a simplified version of this code and it works correctly, so I am not sure what I am doing wrong! Also, in an attempt to fix this, I put in an if statement in which has slowed down the code a lot. Any suggestions on how to streamline the code to be faster and/or how to send and receive the right data would be much appreciated!
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())
Initially I had the code start and go back through the whole while statement after using this code, however I took it out because I think it was making the flow of code mess up the outputs a little:
#starts the process over again, clears so that have time to get information
firstContact = False
inByte = ser.read(1)
print(inByte)
Arduino code:
//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);
}
}
Output:
bytearray('b\xb2\x00')
b'A'
b'0K\xfe'
b'-\x13j10k\xfe'
b'-\x13j10k\xfe'
My motor is running each time a print statement occurs, so I am not sure if it is just my print statements that are messing up, rather than the actual serial communication?
One additional thing I am unsure of, is in my switch-case statement in the arduino code, I am using the ascii code for 'a' (97) instead of actually putting 'a'. Should both work?