Arduino to Raspberry Pi Pyserial Communication not working on Python
Posted: Wed Nov 17, 2021 5:08 pm
I have a Python script that takes in input from a camera. It then gets the position of an object in the image and sends it to the Arduino via PySerial library and USB with the following format. "Xpos-XYPos-Y" demo: 90X90Y would move the servos I have connected to 90 degrees.
What should happen when I run below should be that it moves the motors.
But what really happens... Nothing?
So I thought okay, it might just be that my code is wrong so I tried many different variations. I would list them but then it would take way too long to read. Basically, I changed the encoding and how I turn it into bytes.
I know the problem isn't my hardware (or at least I think) because I can download the Arduino IDE onto the pi and send serial info through there. And it works!
Here is my Arduino code:
I also tried fully updating the raspberry pi to no avail.
typing
tells me that tty1 and tty1AMA0 are enabled
Any help and what I should do? Are there any alternatives?
StackOverflow post: https://stackoverflow.com/questions/699 ... -on-python
What should happen when I run below should be that it moves the motors.
Code: Select all
import serial
ser = serial.Serial('/dev/ttyACM1', 9600, timeout=1)
ser.flush()
ser.write(bytes("90X90Y", encoding='utf-8'))
So I thought okay, it might just be that my code is wrong so I tried many different variations. I would list them but then it would take way too long to read. Basically, I changed the encoding and how I turn it into bytes.
I know the problem isn't my hardware (or at least I think) because I can download the Arduino IDE onto the pi and send serial info through there. And it works!
Here is my Arduino code:
Code: Select all
#include <Servo.h>
// Define Objects
Servo X;
Servo Y;
// Create varibles
String learndata;
String receiveX;
String receiveY;
int moveX;
int moveY;
// Straight forward
int defX = 95;
int defY = 5;
void setup(){
Serial.begin(9600);
//Attatch servos
X.attach(6);
Y.attach(7);
}
void loop(){
if (Serial.available() > 0){
// Parse servo input
receiveX = Serial.readStringUntil('X');
receiveY = Serial.readStringUntil('Y');
moveX = receiveX.toInt();
moveY = receiveY.toInt();
X.write(moveX);
Y.write(moveY);
}//if (Serial.available() > 0){
}//void loop()
typing
Code: Select all
dmesg | grep -i "tty.*enabled"
Any help and what I should do? Are there any alternatives?
StackOverflow post: https://stackoverflow.com/questions/699 ... -on-python