Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to send 3 ints in the range of 0-180 from Python to the Arduino Uno device using pySerial (py3K). I have managed to send 1 int by using python's struct lib (not sure if it's the best or fastest way but it works).

However I'm failing to send more than 1 and every example online seems to stop at 1.

Here's the simplified code. The task is to send servo0-servo4 to the Arduino and apply those values to the corresponding servos.

Python Code

import serial
import struct
import time

bge.arduino = serial.Serial('/dev/ttyACM0', 9600)

# let it initialize
time.sleep(2)

# send the first int in binary format
bge.arduino.write(struct.pack('>B', 45))

Arduino code

#include <Servo.h>
Servo servo0;
Servo servo1;
Servo servo2;

void setup(){
  Serial.begin(9600);
  servo0.attach(3);
  servo1.attach(5);
  servo2.attach(6);
}

void loop(){
  if(Serial.available()){
    int message = Serial.read();
    // control the servo
    servo0.write(message);
  }
}
share|improve this question
up vote 2 down vote accepted

I guess I'll answer my own question for anyone else having this issue. What I did was:

Python:

# write to arduino as raw binary
bge.arduino.write(struct.pack('>BBB',45,90,180))

Arduino:

#include <Servo.h>

Servo servo0;
Servo servo1;
Servo servo2;

// create array
int incoming[2];

void setup(){
  Serial.begin(9600);

  servo0.attach(3);
  servo1.attach(5);
  servo2.attach(6);
}

void loop(){
  while(Serial.available() >= 3){
    // fill array
    for (int i = 0; i < 3; i++){
      incoming[i] = Serial.read();
    }
    // use the values
    servo0.write(incoming[0]);
    servo1.write(incoming[1]);
    servo2.write(incoming[2]);
  }
}
share|improve this answer
    
You're not verifying if Serial.available() has 3 values available. So it's going to get filled with -1's if there aren't. Check to see how many bytes are in the receive buffer. – baldengineer Mar 27 '15 at 23:28
void loop(){
  if(Serial.available() >= 3){
    // fill array
    for (int i = 0; i < 3; i++){
      incoming[i] = Serial.read();
    }
    // use the values
    servo0.write(incoming[0]);
    servo1.write(incoming[1]);
    servo2.write(incoming[2]);
  }
}
share|improve this answer
    
Thank you @baldengineer. I'm now verifying like you proposed. However after a little while of playing with the program it completely messes up and I have to reboot the Arduino for it to work again. I suspect the ordering of the bytes might be shuffling? – ZanQdo Mar 29 '15 at 0:15
    
Holly cow, I think I solved the problem by replacing if(Serial.available().. with while(Serial.available().. Does that make sense to you? – ZanQdo Mar 30 '15 at 3:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.