user39098: I am a bit new to both Arduino and Python. My aim is to send commands to Arduino to run a stepper motor. I need to send an array containing the parameters. After consulting many earlier posts like:
https://stackoverflow.com/questions/27639605/send-a-list-of-data-from-python-to-arduino-using-pyserial
Send multiple int values from Python to Arduino using pySerial
I wrote my Arduino code:
long  incoming[3];

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600); 
  Serial.println("Ready");
}

void loop() {
  if(Serial.available()) {
    for (int i = 0; i < 3; i++) {
      incoming[i] = Serial.read();
    }
    Serial.println(incoming[0]);
    Serial.println(incoming[0]);
    Serial.println(incoming[2]);
  }
}

And the Python(2.7) code:
from time import sleep
import serial
import struct
ser = serial.Serial('COM6', 9600) 

while True:
     ser.write(struct.pack('>iii',2000,10,220))
     print ser.readline() 
     sleep(.5)

Now coming to the problem:

The values printed on the Python shell are completely wrong, including negative values.
If I use >BBB instead of >iii, I am getting the correct result, but I can't go beyond 255, the integer limit. There is a small catch here also, the initial two sets are giving minus values and then it becomes alright.
Further, is there any way to interrupt a running void loop?

Any help is deeply appreciated.

user13425: If I use >BBB instead of >iii... That's because "B" is a byte. So you end up sending 3 bytes:
struct.pack("BBB", 1, 2, 3)
\x01\x02\x03

"i" is an int and has a size of 4, so you send 4*3 = 12 bytes:
struct.pack("iii", 1, 2, 3)
\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00

Serial.read() reads 1 byte at a time, so you need to read 4 bytes for each value. 

Here's a function to read a 32-bit int. The drawbacks of this method are that a) it will block until 4 bytes have been read. And b) there's no way to tell if some data has been lost. There are more robust ways to send data with Start-of-Message, End-of-Message, and Check-sum chars. Also, the function assumes the data is sent with the LSB first. But I'll just put this here as an example.
// Helper macro to merge 4 bytes
#define TO_INT32(a,b,c,d) (((d)<<24)|((c)<<16)|((b)<<8)|(a))
int32_t read_int()
{
  uint32_t result[4];
  for (int i = 0; i < 4; i++) {
    // Wait for data at the port
    while (!Serial.available());
    result[i] = Serial.read();
  }
  return TO_INT32(result[0], result[1], result[2], result[3]);
}


39098: <p>I am a bit new to both Arduino and Python. My aim is to send commands to Arduino to run a stepper motor. I need to send an array containing the parameters. After consulting many earlier posts like:</p>

<p><a href="https://stackoverflow.com/questions/27639605/send-a-list-of-data-from-python-to-arduino-using-pyserial">https://stackoverflow.com/questions/27639605/send-a-list-of-data-from-python-to-arduino-using-pyserial</a></p>

<p><a href="https://arduino.stackexchange.com/questions/9627/send-multiple-int-values-from-python-to-arduino-using-pyserial">Send multiple int values from Python to Arduino using pySerial</a></p>

<p>I wrote my Arduino code:</p>

<pre><code>long  incoming[3];

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600); 
  Serial.println("Ready");
}

void loop() {
  if(Serial.available()) {
    for (int i = 0; i &lt; 3; i++) {
      incoming[i] = Serial.read();
    }
    Serial.println(incoming[0]);
    Serial.println(incoming[0]);
    Serial.println(incoming[2]);
  }
}
</code></pre>

<p>And the Python(2.7) code:</p>

<pre><code>from time import sleep
import serial
import struct
ser = serial.Serial('COM6', 9600) 

while True:
     ser.write(struct.pack('&gt;iii',2000,10,220))
     print ser.readline() 
     sleep(.5)
</code></pre>

<p>Now coming to the problem:</p>

<ol>
<li>The values printed on the Python shell are completely wrong, including negative values.</li>
<li>If I use >BBB instead of >iii, I am getting the correct result, but I can't go beyond 255, the integer limit. There is a small catch here also, the initial two sets are giving minus values and then it becomes alright.</li>
<li>Further, is there any way to interrupt a running void loop?</li>
</ol>

<p>Any help is deeply appreciated.</p>
 13425: <p><em>If I use >BBB instead of >iii...</em> That's because "B" is a byte. So you end up sending 3 bytes:</p>

<pre><code>struct.pack("BBB", 1, 2, 3)
\x01\x02\x03
</code></pre>

<p>"i" is an int and has a size of 4, so you send 4*3 = 12 bytes:</p>

<pre><code>struct.pack("iii", 1, 2, 3)
\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00
</code></pre>

<p><code>Serial.read()</code> reads 1 byte at a time, so you need to read 4 bytes for each value. </p>

<hr>

<p>Here's a function to read a 32-bit int. The drawbacks of this method are that a) it will block until 4 bytes have been read. And b) there's no way to tell if some data has been lost. There are more robust ways to send data with Start-of-Message, End-of-Message, and Check-sum chars. Also, the function assumes the data is sent with the LSB first. But I'll just put this here as an example.</p>

<pre><code>// Helper macro to merge 4 bytes
#define TO_INT32(a,b,c,d) (((d)&lt;&lt;24)|((c)&lt;&lt;16)|((b)&lt;&lt;8)|(a))
int32_t read_int()
{
  uint32_t result[4];
  for (int i = 0; i &lt; 4; i++) {
    // Wait for data at the port
    while (!Serial.available());
    result[i] = Serial.read();
  }
  return TO_INT32(result[0], result[1], result[2], result[3]);
}
</code></pre>
