Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Float problem #42

Closed
MarcinKlima opened this issue Oct 6, 2021 · 6 comments
Closed

Float problem #42

MarcinKlima opened this issue Oct 6, 2021 · 6 comments
Assignees
Labels
question Further information is requested

Comments

@MarcinKlima
Copy link

Hi im got a problem with float on python side (RX), got a this kind of output: b'P'2.1579996350602183e-43
When im sending from uC:
testStruct.z = 'P';
testStruct.y = 4.55;
Betwen uC is no problem work like a harm, on python floats crazy.
9600 baud / Python 3.9.2 / Debian

@PowerBroker2 PowerBroker2 self-assigned this Oct 6, 2021
@PowerBroker2 PowerBroker2 added the question Further information is requested label Oct 6, 2021
@PowerBroker2
Copy link
Owner

Without looking at your code, you're most likely not using 32 bit floats on the Arduino side. If you replace float with double, you should be good to go.

@MarcinKlima
Copy link
Author

Yes, my mistake, is fact.
But when im try examples from GIT, nothing working, only CHAR variable has expected value, in y have 0.0, arr has "nothing".

Raw example generate like this:
unpacked_response = unpacked_response.decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xeb in position 3: invalid continuation byte


Arduino:

#include <Arduino.h>
#include <Wire.h>
#include <SerialTransfer.h>

HardwareSerial Serial2(PA3,PA2);

SerialTransfer myTransfer;

struct STRUCT {
char z;
double y;
} testStruct;

char arr[] = "hello";

void setup() {
Serial2.begin(9600);
myTransfer.begin(Serial2);

analogWriteResolution(12);

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);

testStruct.z = '$';
testStruct.y = 5.25;

}

void loop() {

// use this variable to keep track of how many
// bytes we're stuffing in the transmit buffer
uint16_t sendSize = 0;

///////////////////////////////////////// Stuff buffer with struct
sendSize = myTransfer.txObj(testStruct, sendSize);

///////////////////////////////////////// Stuff buffer with array
sendSize = myTransfer.txObj(arr, sendSize);

///////////////////////////////////////// Send buffer
myTransfer.sendData(sendSize);

testStruct.y = testStruct.y + 0.1;

digitalWrite(LED_BUILTIN, LOW);
delay(100);
digitalWrite(LED_BUILTIN, HIGH);
delay(100);

}

Python:

from time import sleep
from pySerialTransfer import pySerialTransfer as txfer

class struct(object):
z = ''
y = 0.0

arr = ''

if name == 'main':
try:
testStruct = struct
link = txfer.SerialTransfer('/dev/ttyUSB0', baud=9600)

    link.open()
    sleep(5)

    while True:
        if link.available():
            recSize = 0
            
            testStruct.z = link.rx_obj(obj_type='c', start_pos=recSize)
            recSize += txfer.STRUCT_FORMAT_LENGTHS['c']
            
            testStruct.y = link.rx_obj(obj_type='f', start_pos=recSize)
            recSize += txfer.STRUCT_FORMAT_LENGTHS['f']
            
            arr = link.rx_obj(obj_type=str,
                              start_pos=recSize,
                              obj_byte_size=6)
            recSize += len(arr)
            
            print('{}{} | {}'.format(testStruct.z, testStruct.y, arr))
            
        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()

@PowerBroker2
Copy link
Owner

I've fixed the examples and will tag out a new version soon.

@MarcinKlima
Copy link
Author

Ok, find where is problem, im don`t know is the platform STM32/ARDUINO or python issue - but difference bet wen sides is
arduino - recsize is 12... on python showing 8. when im add(4) to be 12 on python, everything work ok. Why?

@PowerBroker2
Copy link
Owner

The issue isn't on either side, it's how you use the libraries. Python's normal float size is 32 bits and Arduino's normal float size is 16 bits. If you use doubles on the Arduino side, or 16 bit floats on the Python side, you'll be good to go

@MarcinKlima
Copy link
Author

Yes im lost 2 days :) for that, mega good library for me.
And numpy and its data types save my life :)
y = np.float16(0.0) etc.
Thank U for support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants