Serial communication between Arduino and Python, question about using hexadecimal values
I am trying to start a motor from a computer by code in Python 3.4 using pySerial to communicate with an Arduino Uno. I have packed the value I am sending to hexidecimal so I only have one byte at a time, but I have a problem getting the correct number on the Arduino side when I submit on the Python side.
PYTHON CODE:
import serial
import struct
ser = serial.Serial(
port ='COM4',
baudrate = 9600,
parity = serial.PARITY_ODD,
stopbits = serial.STOPBITS_TWO,
bytesize = serial.EIGHTBITS
)
#ser.open() #opens port
ser.isOpen() #returns true?
motorState = 0
wristBend = 'Left'
while True:
#need to create options to send to arduino
if wristBend == 'Left':
motorState = 2
elif wristBend == 'Right':
motorState = 3
else:
motorState = 1
motorChar = struct.pack('<B', motorState) #returns the value as a character interger
#motorChar = str(hex(motorState))
print(motorChar)
ser.write(motorChar)
print(ser.read())
break
ARDUINO CODE:
int motorPinLeft = 10;
int motorPinRight = 11;
int motorSpeedLeft = 0;
int motorSpeedRight = 0;
void setup() {
pinMode(motorPinLeft, OUTPUT);
pinMode(motorPinRight, OUTPUT);
Serial.begin(9600);
while(!Serial); //waits for arduino to be ready, not neaded for duo
Serial.println("Speed 0 - 255");
}
void loop()
{
if (Serial.available())
{
int ch = Serial.read();
switch (ch)
{
case 1:
motorSpeedLeft = 0;
motorSpeedRight = 0;
break;
case 2:
motorSpeedLeft = 127;
motorSpeedRight = 0;
break;
case 3:
motorSpeedLeft = 0;
motorSpeedRight = 127;
break;
}
Serial.write(ch);
analogWrite(motorPinLeft, motorSpeedLeft);
analogWrite(motorPinRight, motorSpeedRight);
delay(2500); //wait for 2.5 seconds to make the motor vibrate
motorSpeedRight = 0;
motorSpeedLeft = 0;
analogWrite(motorPinLeft, motorSpeedLeft);
analogWrite(motorPinRight, motorSpeedRight);
Not only is nothing communicating with my schematic, but the output from my Python code where I print what is sent to the Arduino and what is sent from the Arduino is:
b'\x02'
b'S'
If I change the switch case code to 83 (ASCII S) or change the type of the variable to byte, int, uint8_t, I get the exact result. What am I doing wrong here? Sorry if it's obvious, I'm pretty new to python and arduino. Thanks in advance for your help!
source to share
its not difficult here - simple example, it is usually a good idea to start simple and then increase the functionality to whatever you want.
test_serial.py
import serial
ser = serial.Serial("COM4",timeout=5) # everything else is default
ser.write("\x45")
print "RECIEVED BACK:",repr(ser.read(5000))
test_serial.ino
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
source to share