PowerBroker2 / pySerialTransfer

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

Typo in example file and logical error #55

Closed mickeyvanolst closed 2 years ago

mickeyvanolst commented 2 years ago

Hi @PowerBroker2,

First off, amazing work, your library is quite impressive!

I'm trying to use it to send a large text file via Python over UART to a Teensy.

What I noticed is that there is a typo in your example file on this line, the ')' at the end prevents it from compiling.

https://github.com/PowerBroker2/pySerialTransfer/blob/949a7e2197f7bc9c889fcd7f9ad140f79082d444/examples/file/Arduino/rx_file/rx_file.ino#L34

A bigger issue though, is that combined with the Python example file, I can't get any data through. What I noticed through some tests is that the condition myTransfer.currentPacketID() == 1 never seems to be true.

Just to give you a bit of insight, I'm trying to store the incoming file onto an SD card, below you can see my code.

#include <SD.h>
#include <SPI.h>
#include "SerialTransfer.h"

const int fileSize = 2000;
char file[fileSize];
uint16_t fileIndex = 0;
char fileName[10];

SerialTransfer myTransfer;
const int chipSelect = BUILTIN_SDCARD;

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

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    while (1) {
      // No SD card, so don't do anything more - stay stuck here
    }
  }
  Serial.println("card initialized.");

  pinMode(13, OUTPUT);
}

void loop()
{
  if (myTransfer.available())
  {

    // open the file.
    File dataFile = SD.open("datalog.txt", FILE_WRITE);
    if (!myTransfer.currentPacketID())
    {
      myTransfer.rxObj(fileName);
    }
    else if (myTransfer.currentPacketID() == 1)
    {
      myTransfer.rxObj(fileIndex);
      digitalWrite(13, HIGH);

      for (uint8_t i = sizeof(fileIndex); i < myTransfer.bytesRead; i++)
      {
        file[fileIndex] = (char)myTransfer.packet.rxBuff[i];
        if (dataFile) {
          dataFile.print(file[fileIndex]);
        } else {
          // if the file isn't open, pop up an error:
          Serial.println("error opening datalog.txt");
        }
        fileIndex++;
      }
    }
    dataFile.close();
  }
}

Any clue what could be going on here?

PS: The Python script is basically just like the one from the example, all I did was add some print logs

from time import sleep
from pySerialTransfer import pySerialTransfer as txfer

file = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Fusce vulputate eleifend sapien. Vestibulum purus quam, scelerisque ut, mollis sed, nonummy id, metus. Nullam accumsan lorem in dui. Cras ultricies mi eu turpis hendrerit fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac dui quis mi consectetuer lacinia. Nam pretium turpis et arcu. Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Sed aliquam ultrices mauris. Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Praesent adipiscing. Phasellus ullamcorper ipsum rutrum nunc. Nunc nonummy metus. Vestib'
fileSize = len(file)
fileName = 'test.txt'

if __name__ == '__main__':
    try:
        link = txfer.SerialTransfer('COM13')

        link.open()
        sleep(5)
        print('start')
        while True:
            link.send(link.tx_obj(fileName))

            numPackets = int(fileSize / (txfer.MAX_PACKET_SIZE - 2))

            if numPackets % txfer.MAX_PACKET_SIZE:
                numPackets += 1

            for i in range(numPackets):
                fileIndex = i * txfer.MAX_PACKET_SIZE
                dataLen = txfer.MAX_PACKET_SIZE - 2

                if (fileIndex + (txfer.MAX_PACKET_SIZE - 2)) > fileSize:
                    dataLen = fileSize - fileIndex

                dataStr = file[fileIndex:dataLen]

                sendSize = link.tx_obj(fileIndex, val_type_override='h')
                sendSize = link.tx_obj(dataStr, start_pos=sendSize)
                link.send(sendSize)

                sleep(1)

            print('sleep')
            sleep(10)

    except KeyboardInterrupt:
        link.close()
mickeyvanolst commented 2 years ago

After looking a bit closer at the examples in the Arduino library, I found that adding a 1 to python line 38. Does make the initial portion of the string occur on the SD card.

link.send(sendSize, 1)

at this point I'm just a bit puzzled about the logic which keeps the rest of the data coming in. I noticed someone posted a similar issue about file transfer in the Arduino version of the library

https://github.com/PowerBroker2/SerialTransfer/issues/89

mickeyvanolst commented 2 years ago

Another error in the python script is that the substring isn't created in the right way.

dataStr = file[fileIndex:fileIndex+dataLen]

adding +dataLen makes the script work again (along with sending the ID number)

PowerBroker2 commented 2 years ago

Thanks for your eyes on the example code - I made the bug fix. Can you confirm it's working for you?

PowerBroker2 commented 2 years ago

Assuming it's fixed