xreef / EByte_LoRa_E32_python_raspberrypi_library

Raspberry Pi LoRa EBYTE E32 device library complete and tested. sx1278/sx1276
https://www.mischianti.org/category/my-libraries/lora-e32-devices/
Other
4 stars 0 forks source link

Sending array of bytes #3

Open JMartinezEco opened 4 days ago

JMartinezEco commented 4 days ago

Hello!

First of all I want to thank you for the work you have done porting this library to Python for Raspberry Pi. You have done a great job.

I was already using your library for Arduino, which worked great, and I had to make one for Raspberry Pi. As soon as I saw this version I decided to make the effort to transfer everything I had to this library because I think it will be worth it.

The problem or doubt that I am having is the following:

My goal is to send an array of bytes from a Raspberry Pi to an Arduino and vice versa. The second part, from Arduino to Raspberry Pi was already working previously, and I used your library as follows.

//C++
// Send message via Lora
LoRa_Module.sendFixedMessage(addH, addL, chn, (uint8_t *)bytes_response, len_response);

where bytes_response is declared as:

byte bytes_response[70];

My target is the same on the Raspberry Pi, but I see that there are two methods implemented:

lora.send_fixed_message(add_h, add_l, ch, message). y lora.send_fixed_dict(ADDH, ADDL, CHAN, dict_message)

I understand that the dictionary version is not the one I am looking for, but the message version.

Is it possible to send an array of bytes from the Raspberry without String as it seems to be needed?

My objective in Python is something like:

# Python
full_request = [0x01, 0x034, 0x04]
response_code = lora.send_fixed_message(0x1, 0x9F, 0x06, full_request)

Thank you so much in advance.

JMartinezEco commented 4 days ago

Well. I managed to get it work.

My code is like the following:

response_code = lora.send_fixed_message(add_h, add_l, channel, bytearray(full_request))

The problem comes inside the library:

`def _send_message(self, message, ADDH=None, ADDL=None, CHAN=None) -> ResponseStatusCode: result = ResponseStatusCode.E32_SUCCESS

    size_ = len(message.encode('utf-8'))
    if size_ > MAX_SIZE_TX_PACKET:
        return ResponseStatusCode.ERR_E32_PACKET_TOO_BIG

Here, you can't encode as utf-8 a bytearray in "size_ = len(message.encode('utf-8'))"

If I just put the following:

      # If message is a byte array, we need to check the size
        if isinstance(message, bytearray):
            size_ = len(message)
        else:
            size_ = len(message.encode('utf-8'))

Do you consider this optimal? Could be any other possible way to do it better or outside your library?