Every Other data point is a 0 when received by Serial

I have been banging my head against the wall for the past two days trying to figure this out.

I'm trying to send a array of integers, from python using pyserialtransfer package and receive it on the arduino mega with serialtranfer.h.
The packages have no commenting or documentation, which is a bit annoying.
I think what i'm failing to completely understand is how the line
"recSize = myTransfer.rxObj(arr, recSize);" is working, How does this actually assign the received bytes?

I've read the guide on advanced serial communication by the author of the package Power_Broker and also the basic communication tutorial by Robin2. I understood it all ( I Thought!) which makes this more annoying.
I'm getting a successful transfer, kind of, but every second data point is coming out as a 0 .
It seems to me that the arduino thinks it's receiving a different data type than it is?
or do the 0s represent a delimiter or a stop byte? I thought from reading power_brokers threads this package doesnt use delimiters? and, shouldn't the package automatically deal with start stop COBS and CRC bytes?

Here is my python code:

from time import sleep
from pySerialTransfer import pySerialTransfer as txfer
port = "/dev/ttyACM0"




arr = [9,7,3]


if __name__ == '__main__':
    try:
        
        link = txfer.SerialTransfer(port)
        
        link.open()
        sleep(5)
    
        while True:
            sendSize = 0            
            
            sendSize = link.tx_obj(arr, start_pos=sendSize)
            
            link.send(sendSize)
        
    except KeyboardInterrupt:
        link.close()

And here is my arduino code:

#include "SerialTransfer.h"
#include "FastLED.h"

SerialTransfer myTransfer;


int arr[6];


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


void loop()
{
  check_serial();
  for (int i = 0; i < 6; i++) {
  Serial.print(arr[i]);
  }recSize = myTransfer.rxObj(arr, recSize);
  Serial.println();
  Serial.println("-----");
}




void check_serial() {
  
  if(myTransfer.available())
  {
    // use this variable to keep track of how many
    // bytes we've processed from the receive buffer
    uint8_t recSize = 0;
    
    recSize = myTransfer.rxObj(arr, recSize);
  }

  
}

Obviously if I open the serial monitor to view the prints, it blocks the python code. But If I run the python code and then open serial monitor after, it has received the data, but it looks like this:

"907030"

So theres an extra 0 being inserted there, and I'm not sure why.

My final use case is to send an array of 60 8-bit integers (will never be > 255) to the arduino several times per second ( data points from a pose estimation algorithm).
So to speed things up, i'd appreciate advice on how make this more efficient and send just 8bit ints.

The Arduino only receives bytes, so look at what you are sending.

I don't know anything about python but I am wondering what it considers to be an integer. Does anything in your python code limit the size of an integer to 8 bits? I'm thinking maybe it uses 16 bits, 8 of which will always be equal to 0x00, which would explain what you receive.

As Mike says, all the receiving code sees is bytes, they do not arrive with a data type associated with them, your code needs to take care of allocating them to the right variables in the right way. Try just doing a simple read of your incoming data and mirroring it to the serial port.

I just had to go get my OLD Python book. It says integers are stored as strings until a computation is needed. Then the string is converted to a LONG. And that is whatever the C compiler used when the the Python interpreter was compiled. Seems to be at least 32 bits. For a PC, anyway.
The only other spec is for float.

Where is the variable 'recSize' declared?!? There is a local one in 'check_serial()' but that won't be visible in loop(). Is there a magical global defined in the mysterious "SerialTransfer" library?

@johnwasser I don't know! This is how they do it in the example code. ( EDIT: actually I created check_serial() and moved some code there from loop(), so that ones on me, not the package.)

You are right to call the SerialTransfer library mysterious. I tried to dive into it and look, several of the files were just completely commented out! The whole thing's a bit of a mess. I couldn't for the life of me figure out what it's actually doing.

In the end i've just written my own implementation.
I included start, stop , COBS and a checksum.
I didn't really look at anyone elses code for the COBS or checksums just made it up based on my understanding, so maybe not the most efficient and could have bugs. Although I tested by sending a load of start and stop bytes and seems to work OK.
Some of the arduino code is based on an example I found online, and then i've added my own COBS_decode and Checksum functions.
I posed below in case anyone is interested or has any tips

( note, this will only send 8-bit ints, I will add some logic to check the inputs are actually < 255 when i put it in my final implementation)

Python Code

import serial
import time


port = "/dev/ttyACM0"

array_a = [21,21,21,
            21,211,21,
            77,35,99,
            22,88,3
            21,21,21,
            21,21,21,
            21,21,21,
            21,20,20,
            21,21,21,
            21,21,44,
]

SerialObj = serial.Serial(port) # COMxx   format on Windows
SerialObj.baudrate = 115200  # set Baud rate to 9600
SerialObj.bytesize = 8     # Number of data bits = 8
SerialObj.parity   ='N'    # No parity
SerialObj.stopbits = 1    # Number of Stop bits = 1



def send_serial(data_array):
    
    start_tag = 20
    end_tag = 21
    COBS_start = 255
    COBS_end = 255
    sum_check = (sum(data_array))%256
    print(sum_check)  
   
    data_array, COBS_start = COBS(data_array, start_tag, start_tag, end_tag)
    data_array, COBS_end = COBS(data_array, end_tag, start_tag, end_tag)  
    
    print(COBS_start)
    print(COBS_end)

    data_array.insert(0,start_tag)
    data_array.insert(1,COBS_start)
    data_array.insert(2,COBS_end)
    data_array.insert(3,sum_check)
    data_array.append(end_tag)
    

    arr_bytes = []
    for a in array_a:
        arr_bytes.append((a).to_bytes(1, byteorder = "little"))
    
    
    for b in arr_bytes:
        SerialObj.write(b)
    


def COBS(in_array, search_t,start_tag,end_tag):
    
    preceding_bytes = 3  # Number of bytes before the first payload
    COBS_byte = 255
    i = 0
    j = 0 
    k = 0
    out_array = in_array
    COBS_pos = []
    
    while i < len(out_array):
        if (out_array[i] == search_t):
             COBS_pos.append(i)
        i += 1
    
    print(COBS_pos)

    if (len(COBS_pos) !=  0):
        COBS_byte = COBS_pos[0]+preceding_bytes
        if (COBS_byte == start_tag):
            COBS_byte = 0 
        if (COBS_byte == end_tag):
            COBS_byte = 1 
        
    while j < (len(COBS_pos)):                
        if (j == ((len(COBS_pos))-1)):
            out_array[(COBS_pos[j])] = 255
            j+= 1
        else: 
            out_array[(COBS_pos[j])] = COBS_pos[j+1]+preceding_bytes 
            if (out_array[(COBS_pos[j])] == start_tag):
                out_array[(COBS_pos[j])] = 0 
            if (out_array[(COBS_pos[j])] == end_tag):
                out_array[(COBS_pos[j])] = 1 
            
            j += 1

    
    return(out_array, COBS_byte)



send_serial(array_a)

Arduino Code

// Example 6 - Receiving binary data

const byte numBytes = 64;
byte receivedBytes[numBytes];
byte numReceived = 0;
int received_data[64];
int decoded_data[64];
int COBS_start = 255;
int COBS_end = 255;
static boolean recvInProgress = false;
boolean newData = false;

void setup() {
    Serial1.begin(9600);
    Serial.begin(115200);
    Serial1.println("<Arduino is ready>");
    received_data[0] = 255;
    received_data[1] = 255;
}

void loop() {
    recvBytesWithStartEndMarkers();
    showNewData();
    
}

void recvBytesWithStartEndMarkers() {
    
    static byte ndx = 0;
    byte startMarker = 0x14;
    byte endMarker = 0x15;
    byte rb;
   

    while (Serial.available() > 0 && newData == false) {
        rb = Serial.read();

        if (recvInProgress == true) {
            if (rb != endMarker) {
                receivedBytes[ndx] = rb;
                ndx++;
                if (ndx >= numBytes) {
                    ndx = numBytes - 1;
                }
            }
            else {
                //receivedBytes[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                numReceived = ndx;  // save the number for use when printing
                ndx = 0;
                newData = true;
                COBS_decode();
            }
        }

        else if (rb == startMarker) {
            recvInProgress = true;
        }
    }

    for (int i = 0; i < numReceived; i++) {
        received_data[i] = receivedBytes[i];      
    }

    
    
   
}

void showNewData() {
    if (newData == true) {
        Serial1.print("This just in (HEX values)... ");
        for (byte n = 0; n < numReceived; n++) {
            Serial1.print(decoded_data[n]);
            Serial1.print(' ');
        }
        Serial1.println();
        newData = false;
    }
}


void COBS_decode() {
    
    int len_rec = (sizeof(received_data)/sizeof(received_data[0]))-3;    
    uint8_t sum_check_rec = received_data[2];
    uint8_t sum_check_calc = 0;
    int start_tag = 20;
    int end_tag = 21;
    for (int i = 0; i < numReceived; i++) {
      Serial1.print(received_data[i]);
      Serial1.print(' ');      
    }
    Serial1.println();    
    int COBS_start = received_data[0];
    int COBS_end = received_data[1];

    
    for (int i = 3; i < len_rec; i++) {
      
      int current = received_data[i];              

      if (COBS_start == 0) {
        COBS_start = start_tag;      
      }
      else if (COBS_start == 1) {
        COBS_start = end_tag;
      }
      if (COBS_end == 1) {
        COBS_end = end_tag;        
       }
      else if (COBS_end == 0) {
        COBS_end = start_tag;        
       }
      if (i == COBS_start) {
        decoded_data[i-3] = start_tag;
        COBS_start = current;       
      } 
      
      else if (i == COBS_end) {
        decoded_data[i-3] = end_tag;
        COBS_end = current;        
      }

      else {

        decoded_data[i-3] = current;
        
      }
     }

     
      
      for (int i = 0; i < len_rec; i++) {
        sum_check_calc += decoded_data[i];
      }
    
      if (recvInProgress == false && (sum_check_rec != sum_check_calc)) {
        Serial1.println("SUM CHECK ERROR");
        Serial1.println(sum_check_calc);
        Serial1.println(sum_check_rec);

    
    }

  
}

Thanks @Paul_KD7HB
I ended up writing my own implementation in the end ( see comment below), but this will be useful as I think at some point I'll extend that to send ints.

Just using the serial module on a Raspberry Pi this is all you need to send a string as a byte array.

import serial, time
# set up the serial port
sampleInput = serial.Serial("/dev/ttyUSB0",115200, timeout = 5) # For Mega or nano
#sampleInput = serial.Serial("/dev/ttyACM0",115200, timeout = 5) # For Uno

sampleInput.write(b'1') # write a single byte of '1' in other words 0x31 as it id the ASCII of '1'

Hi Mike,

It's an Nvidia jetson nano, but I guess it's the same.
I will need to convert from integers though, not character hence using the conversion function.
I added the start stop cobs and suncheck to increase reliability as I'll be sending on further signals based on just one dataset, so any mistakes or noise could cause glitches. Bearing in mind they only add 4 bytes to what would be 60 bytes anyway, I think it's worth it.

Same as what? Certainly not an Arduino nano.

OK but all data must be sent as a byte array, so convert your numbers into strings and send that.