1

I am trying to send six integers at a time through Serial.write() on arduino and send them to python. so far, I have managed to turn the Integers into byte pointers (I think thats the name) and send them over serial. however, when I try to reassemble them on the python side, I get some strange numbers that I did not intend to see.

Arduino Code:

int myInts[6]= {1455, 1446, 6766, 974, 365, 455};

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

  byte *p = (byte)myInts;
  for(byte i = 0; i < sizeof(myInts); i++){
    Serial.write(p[i]);
  }
}

Python Code

import argparse
import numpy
import time
import serial 
import struct

ser = serial.Serial('/dev/ttyACM0', 9600)
while True:
    b = ser.read(4)

    i = struct.unpack('i' ,b)
    print(i)

Output:

(-1090650113,) (285341695,) (10683141,) (-1090650177,) (285341695,) (10683141,)

Thanks!

Vik

1 Answer 1

1

I will offer several alternatives. Easiest one is listed last.

First thing you should realize is that int is a different size on the two platforms. Arduino int is a signed 16-bit int, -32768 -> 32767. Your python int is a 32-bit int. Certainly the numbers being returned are far outside the range of any 16-bit number.

I would recommend changing your int in the Arduino to unsigned int and then change your python unpack format string to H (unsigned short). Or, leave the Arduino as int and change your python unpack format string to 'h' (short) and see if that makes a difference for you. Oh and of course you should change to b = ser.read(2) because these are now all 2-byte integers.

Next, Arduino's Serial.write() method supports a single byte, a string, or a buffer with a length. Not a byte pointer. So while I don't have an Arduino here in front of me to test, I think you should be using the Serial.write(buf,len) form of the command and get rid of the "byte pointers", which I've never seen mentioned in any Arduino documentation:

Serial.write(myInts, 12);

Finally, the easiest thing to do is simply to use Serial.print(str) or Serial.println(str) and send a string across. Then on the python side simply convert the string to an integer type of your choosing.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much! This was a perfect reply! In the past, I was using the Serial.println() from arduino, but I was getting some weird strings on the python side, such as "/xff" or "/xfe". I also read that Serial.write() is better for sending data, which is why I wanted to use bytes, since I feared that using Strings would cause the problem, like it did with Serial.print() . What do you think?
@VikramB I think strings are easiest on Arduino. I'm glade to have helped, and hope you'll click the check mark to "Accept" my answer if I feel my answer is best.
I have tried sending a string through the Serial.write by casting the integer I want to send to a string, but I get:"no matching function for call to 'HardwareSerial::write(String&)' "
@VikramB Indeed. I misread the documentation: "write() Description Writes binary data to the serial port. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead. So use print() or println(). Sorry for the confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.