Arduino to Raspberry Pi Pyserial Communication not working on Python

Electronics Computer Programming Q&A
Post Reply
Shaare
Posts: 2
Joined: Sun Nov 14, 2021 1:21 pm
Contact:

Arduino to Raspberry Pi Pyserial Communication not working on Python

Post by Shaare »

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.

Code: Select all

import serial

ser = serial.Serial('/dev/ttyACM1', 9600, timeout=1)
ser.flush()


ser.write(bytes("90X90Y", encoding='utf-8'))
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:

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()
I also tried fully updating the raspberry pi to no avail.

typing

Code: Select all

dmesg | grep -i "tty.*enabled"
tells me that tty1 and tty1AMA0 are enabled

Any help and what I should do? Are there any alternatives? :eek:

StackOverflow post: https://stackoverflow.com/questions/699 ... -on-python
Shaare
Posts: 2
Joined: Sun Nov 14, 2021 1:21 pm
Contact:

Re: Arduino to Raspberry Pi Pyserial Communication not working on Python

Post by Shaare »

Btw, is this the right place to send this? Or should I change to the robotics section?
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 15 guests