I'm getting this 'TypeError: object of type 'int' has no len()' for a data conversion function that I am trying to integrating into a class. Outside of the class it works with no errors, put it into the class, or try to access it inside of the class, and I'm getting the same error.
The program I'm writing sends int/float type data between two rf/lora nodes. (This is just a portion of a larger telemetry program) I am converting the data to byte arrays using struct.pack() in order to send them. Code is written in CircuitPython using Adafruit hardware (Feather m4 & lora breakout board) and libraries.
Any suggestion on how to fix the issue or what's causing it would be greatly appreciated!
Code Below:
class TransmitState(State):
def __init__(self):
super().__init__()
self.entered = time.monotonic()
self.rf = adafruit_rfm9x.RFM9x(spi, cs, reset, radio_freq)
self.rf.tx_power = 23
self.rf.enable_crc = True
self.rf.node = 1
self.rf.destination = 2
led.value = True
# converts data to byte array - line error on ba = ...
def pack_data(self, data_to_pack):
# First encode the number of data items, then the actual items
ba = struct.pack("!I" + "d" * len(data_to_pack), # <--- Line that's causing the error
len(data_to_pack), *data_to_pack)
return ba
# sendData sends the converted data to our 2nd node
def sendData(self, data):
# checks to see if data is null
if data:
ts = self.pack_data(data) # <--- Call to convert fcn
self.rf.send(ts)
@property
def name(self):
return 'transmit'
def enter(self, machine):
State.enter(self, machine)
self.entered = time.monotonic()
led.value = True
print("Entering Transmit")
def exit(self, machine):
print("Exiting Transmit")
State.exit(self, machine)
def update(self, machine):
if State.update(self, machine):
now = time.monotonic()
if now - self.entered >= 1.0:
led.value = False
print("Transmitting...")
self.sendData(3) # <-- FCN Call, 3 is arbitrary
machine.go_to_state('idle')
Note: Import and pin assignments not shown
That error is because you are trying to get the length of an
int
.len()
attribute is only for iterable types like dict, list, strings,sets and tuples.Try :
What this will do is convert the data into string and then get the length.