2

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:

  1. The values printed on the Python shell are completely wrong, including negative values.
  2. 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.
  3. Further, is there any way to interrupt a running void loop?

Any help is deeply appreciated.

  • Are you aware that Serial.read() returns the first byte in the input buffer? You need to reconstruct your values byte by byte. – user31481 Oct 24 '17 at 17:14
  • 2
    This may help clear up some misconceptions: hackingmajenkoblog.wordpress.com/2016/02/01/… – Majenko Oct 24 '17 at 17:31
  • Also you print incoming[0] twice – user31481 Oct 24 '17 at 17:32
6

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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