PowerBroker2 / pySerialTransfer

Python package to transfer data in a fast, reliable, and packetized form
MIT License
143 stars 35 forks source link

Error when tryinf to receive integer #56

Closed cujhg closed 2 years ago

cujhg commented 2 years ago

Hi!

I want to transfer an Integer from my Arduino to my Computer. The Arduino script is based on: https://github.com/PowerBroker2/pySerialTransfer/blob/master/examples/datum/Arduino/tx_datum/tx_datum.ino

#include "SerialTransfer.h"

SerialTransfer myTransfer;

int y;

void setup()
{
  Serial.begin(115200);
  myTransfer.begin(Serial);

  y = 4;
}

void loop()
{
  myTransfer.sendDatum(y);
  delay(500);
}

My Python Code is based on: https://github.com/PowerBroker2/pySerialTransfer/blob/master/examples/datum/Python/rx_datum.py

from time import sleep
from pySerialTransfer import pySerialTransfer as txfer

y = 0

if __name__ == '__main__':
    try:
        link = txfer.SerialTransfer('/dev/ttyACM0')

        link.open()
        sleep(5)

        while True:
            if link.available():
                y = link.rx_obj(obj_type='i')
                print(y)

            elif link.status < 0:
                if link.status == txfer.CRC_ERROR:
                    print('ERROR: CRC_ERROR')
                elif link.status == txfer.PAYLOAD_ERROR:
                    print('ERROR: PAYLOAD_ERROR')
                elif link.status == txfer.STOP_BYTE_ERROR:
                    print('ERROR: STOP_BYTE_ERROR')
                else:
                    print('ERROR: {}'.format(link.status))

    except KeyboardInterrupt:
        link.close()

When I try to run the Python Code I get the error:

Traceback (most recent call last): File "b.py", line 17, in y = link.rx_obj(obj_type='i') File "/home/clemens/.local/lib/python3.8/site-packages/pySerialTransfer/pySerialTransfer.py", line 346, in rx_obj buff = bytes(self.rxBuff[start_pos:(start_pos + STRUCT_FORMAT_LENGTHS[obj_type])]) TypeError: 'str' object cannot be interpreted as an integer

Is this a bug or am I missing something?

Best Regards, Clemens

PowerBroker2 commented 2 years ago

You can't use int, you have to use int32_t