user8083: 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);
  }
}


user8083: 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]);
  }
}


8083: <p>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).</p>

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

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

<p>Python Code</p>



<pre class="lang-python prettyprint-override"><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('&gt;B', 45))
</code></pre>

<p>Arduino code</p>



<pre class="lang-C++ prettyprint-override"><code>#include &lt;Servo.h&gt;
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);
  }
}
</code></pre>
 8083: <p>I guess I'll answer my own question for anyone else having this issue. What I did was:</p>

<p>Python:</p>

<pre><code># write to arduino as raw binary
bge.arduino.write(struct.pack('&gt;BBB',45,90,180))
</code></pre>

<p>Arduino:</p>

<pre><code>#include &lt;Servo.h&gt;

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() &gt;= 3){
    // fill array
    for (int i = 0; i &lt; 3; i++){
      incoming[i] = Serial.read();
    }
    // use the values
    servo0.write(incoming[0]);
    servo1.write(incoming[1]);
    servo2.write(incoming[2]);
  }
}
</code></pre>
