Problem with the example scripts
eppane opened this issue · 7 comments
Hello!
The Example Python Script works well with Arduino Nano 33 BLE Sense.
However, when I try to modify it to send an array of floats, using the "Send a list" way, the tx_obj() -method returns the following error:
Traceback (most recent call last):
File "C:\Users\Klupi\AppData\Local\Temp\ipykernel_25544\2144615584.py", line 19, in <module>
list_size = link.tx_obj(list_)
File "C:\Users\Klupi\miniconda3\envs\deeplearning\lib\site-packages\pySerialTransfer\pySerialTransfer.py", line 272, in tx_obj
start_pos = self.tx_obj(el, start_pos)
File "C:\Users\Klupi\miniconda3\envs\deeplearning\lib\site-packages\pySerialTransfer\pySerialTransfer.py", line 289, in tx_obj
self.txBuff[index + start_pos] = val_bytes[index]
IndexError: list assignment index out of range
Why is this, what am I doing wrong? My sample array is as follows:
[-0.27273, -0.33333, 0.066667, -0.10638, -0.27273, -0.33333, -0.066667, -0.10638, -0.045455, -0.28889, -0.022222, 0.021277, -0.045455, -0.46667, -0.2, 0.10638, -0.045455, -0.2, 0.066667, 0.14894, -0.045455, -0.33333, 0.42222, 0.14894, -0.59091, -0.33333, 0.33333, 0.021277, -0.59091, -0.28889, 0.066667, -0.31915, -0.59091, -0.28889, -0.55556, 0.021277, -0.59091, -0.24444, -0.066667, 0.021277, -0.18182, 0.066667, 0.11111, 0.14894, -0.18182, 0.066667, 0.066667, 0.14894, -0.18182, 0.022222, -0.022222, -0.021277, -0.18182, 0.066667, 0.066667, -0.021277, 0.13636, 0.066667, -0.022222, -0.021277, 0.36364, -0.33333, 0.022222, -0.021277, 0.090909, -0.066667, -0.33333, -0.44681, 0.090909, -0.42222, -0.2, -0.19149, 0.090909, -0.11111, -0.11111, 0.10638, 0.090909, -0.24444, 0.022222, -0.021277, 0.090909, -0.066667, -0.066667, -0.021277, 0.090909, 0.066667, 0.066667, -0.021277, 0.090909, -0.022222, -0.2, 0.021277, 0.090909, -0.46667, -0.33333, 0.021277, 0.090909, -0.066667, -0.33333, -0.3617, 0.090909, -0.066667, -0.2, -0.3617, 0.090909, -0.066667, -0.33333, -0.48936, 0.090909, -0.24444, -0.2, -0.48936, -0.18182, -0.68889, -0.55556, -0.3617, -0.18182, -0.28889, -0.33333, -0.3617, -0.090909, -0.022222, -0.42222, -0.3617, 0.0, -0.24444, -0.86667, -0.3617, -0.045455, -0.2, -0.2, -0.10638, -0.045455, -0.066667, -0.33333, -0.10638, 0.090909, -0.11111, -0.33333, -0.65957, 0.090909, 0.2, -0.73333, -0.3617, 0.090909, 0.33333, -0.73333, -0.3617, 0.13636, 0.066667, -0.91111, -0.3617, 0.22727, -0.15556, -0.68889, -0.44681, 0.22727, 0.24444, -0.55556, -0.44681, 0.090909, 0.28889, -0.6, -0.44681, 0.63636, 0.11111, -0.42222, -0.74468, 0.63636, 0.066667, -0.6, -0.74468, 0.72727, 0.33333, -0.37778, -0.61702, 0.72727, -0.066667, -0.6, -0.61702, 0.81818, 0.2, -0.77778, -0.61702, 0.81818, 0.2, -0.73333, -0.61702, 0.77273, 0.066667, -0.95556, -0.61702, 0.77273, -0.2, -0.82222, -0.61702, 0.81818, 0.022222, -0.95556, -0.61702, 0.77273, 0.066667, -0.6, -0.82979, 0.77273, 0.33333, -0.73333, -0.82979, 0.77273, -0.37778, -0.86667, -0.82979, 0.27273, -0.066667, -0.68889, -0.3617, 0.27273, -0.066667, -0.68889, -0.70213, 0.27273, 0.15556, -0.6, -0.70213, 0.27273, 0.33333, -1.0, -0.70213, 0.77273, -0.15556, -0.82222, -0.61702, 0.77273, 0.6, -1.0, -0.61702, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Interestingly, sending a smaller array such as [1.2433333333334, 2.34222222221, 3.4255555555555]
works fine. How would you proceed on sending the bigger array to Arduino?
Many thanks!
You can only send up to 254 bytes per packet - that array is way too big.
Hello,
I have gotten this far with sending an array from Python and returning a float in Arduino:
Python:
import time
from pySerialTransfer import pySerialTransfer as txfer
try:
link = txfer.SerialTransfer('COM6', 115200) ########## replace COM port number ##########
link.open()
time.sleep(5)
idx = 0
data = [-0.2727299928665161,-0.3333300054073334,0.06666699796915054,-0.10638000071048737,-0.2727299928665161,-0.3333300054073334,-0.06666699796915054,-0.10638000071048737,-0.045455001294612885,-0.28889000415802,-0.022221999242901802,0.02127699926495552,-0.045455001294612885,-0.4666700065135956,-0.20000000298023224,0.10638000071048737,-0.045455001294612885,-0.20000000298023224,0.06666699796915054,0.14893999695777893,-0.045455001294612885,-0.3333300054073334,0.42221999168395996,0.14893999695777893,-0.590910017490387,-0.3333300054073334,0.3333300054073334,0.02127699926495552,-0.590910017490387,-0.28889000415802,0.06666699796915054,-0.31915000081062317,-0.590910017490387,-0.28889000415802,-0.5555599927902222,0.02127699926495552,-0.590910017490387,-0.2444400042295456,-0.06666699796915054,0.02127699926495552,-0.18182000517845154,0.06666699796915054,0.11111000180244446,0.14893999695777893,-0.18182000517845154,0.06666699796915054,0.06666699796915054,0.14893999695777893,-0.18182000517845154,0.022221999242901802,-0.022221999242901802,-0.02127699926495552,-0.18182000517845154,0.06666699796915054,0.06666699796915054,-0.02127699926495552,0.13636000454425812,0.06666699796915054,-0.022221999242901802,-0.02127699926495552,0.3636400103569031,-0.3333300054073334,0.022221999242901802,-0.02127699926495552,0.09090899676084518,-0.06666699796915054,-0.3333300054073334,-0.4468100070953369,0.09090899676084518,-0.42221999168395996,-0.20000000298023224,-0.19148999452590942,0.09090899676084518,-0.11111000180244446,-0.11111000180244446,0.10638000071048737,0.09090899676084518,-0.2444400042295456,0.022221999242901802,-0.02127699926495552,0.09090899676084518,-0.06666699796915054,-0.06666699796915054,-0.02127699926495552,0.09090899676084518,0.06666699796915054,0.06666699796915054,-0.02127699926495552,0.09090899676084518,-0.022221999242901802,-0.20000000298023224,0.02127699926495552,0.09090899676084518,-0.4666700065135956,-0.3333300054073334,0.02127699926495552,0.09090899676084518,-0.06666699796915054,-0.3333300054073334,-0.36169999837875366,0.09090899676084518,-0.06666699796915054,-0.20000000298023224,-0.36169999837875366,0.09090899676084518,-0.06666699796915054,-0.3333300054073334,-0.4893600046634674,0.09090899676084518,-0.2444400042295456,-0.20000000298023224,-0.4893600046634674,-0.18182000517845154,-0.6888899803161621,-0.5555599927902222,-0.36169999837875366,-0.18182000517845154,-0.28889000415802,-0.3333300054073334,-0.36169999837875366,-0.09090899676084518,-0.022221999242901802,-0.42221999168395996,-0.36169999837875366,0.0,-0.2444400042295456,-0.8666700124740601,-0.36169999837875366,-0.045455001294612885,-0.20000000298023224,-0.20000000298023224,-0.10638000071048737,-0.045455001294612885,-0.06666699796915054,-0.3333300054073334,-0.10638000071048737,0.09090899676084518,-0.11111000180244446,-0.3333300054073334,-0.6595699787139893,0.09090899676084518,0.20000000298023224,-0.7333300113677979,-0.36169999837875366,0.09090899676084518,0.3333300054073334,-0.7333300113677979,-0.36169999837875366,0.13636000454425812,0.06666699796915054,-0.911109983921051,-0.36169999837875366,0.2272700071334839,-0.15556000173091888,-0.6888899803161621,-0.4468100070953369,0.2272700071334839,0.2444400042295456,-0.5555599927902222,-0.4468100070953369,0.09090899676084518,0.28889000415802,-0.6000000238418579,-0.4468100070953369,0.6363599896430969,0.11111000180244446,-0.42221999168395996,-0.7446799874305725,0.6363599896430969,0.06666699796915054,-0.6000000238418579,-0.7446799874305725,0.7272700071334839,0.3333300054073334,-0.3777799904346466,-0.6170200109481812,0.7272700071334839,-0.06666699796915054,-0.6000000238418579,-0.6170200109481812,0.8181800246238708,0.20000000298023224,-0.7777799963951111,-0.6170200109481812,0.8181800246238708,0.20000000298023224,-0.7333300113677979,-0.6170200109481812,0.7727299928665161,0.06666699796915054,-0.955560028553009,-0.6170200109481812,0.7727299928665161,-0.20000000298023224,-0.8222200274467468,-0.6170200109481812,0.8181800246238708,0.022221999242901802,-0.955560028553009,-0.6170200109481812,0.7727299928665161,0.06666699796915054,-0.6000000238418579,-0.8297899961471558,0.7727299928665161,0.3333300054073334,-0.7333300113677979,-0.8297899961471558,0.7727299928665161,-0.3777799904346466,-0.8666700124740601,-0.8297899961471558,0.2727299928665161,-0.06666699796915054,-0.6888899803161621,-0.36169999837875366,0.2727299928665161,-0.06666699796915054,-0.6888899803161621,-0.7021300196647644,0.2727299928665161,0.15556000173091888,-0.6000000238418579,-0.7021300196647644,0.2727299928665161,0.3333300054073334,-1.0,-0.7021300196647644,0.7727299928665161,-0.15556000173091888,-0.8222200274467468,-0.6170200109481812,0.7727299928665161,0.6000000238418579,-1.0,-0.6170200109481812,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
while True:
send_size = link.tx_obj(data[idx])
link.send(send_size)
print("Sent:\t\t{}: {}".format(idx, data[idx]))
time.sleep(0.001)
if idx+1 == len(data):
time.sleep(1)
rec_float_ = link.rx_obj(obj_type='f')
print("Received:\t{}".format(rec_float_))
raise KeyboardInterrupt
idx += 1
except KeyboardInterrupt:
try:
link.close()
except:
pass
except:
import traceback
traceback.print_exc()
try:
link.close()
except:
pass
Arduino:
// SerialTransfer
#include "SerialTransfer.h"
constexpr int N_INPUTS = 65*4;
constexpr int N_OUTPUTS = 1;
float X_test[N_INPUTS] = { -1.0 };
float y[N_OUTPUTS] = { -1.0 };
int process_count = 0;
int counter = 0;
float buff;
SerialTransfer myTransfer;
void setup() {
Serial.begin(115200);
myTransfer.begin(Serial);
delay(4000);
}
void loop() {
receiveData();
if (counter == N_INPUTS) {
processData();
process_count += 1;
counter = 0;
showResults();
delay(3000);
}
}
void receiveData() {
if (myTransfer.available()) {
myTransfer.rxObj(buff);
X_test[counter] = buff;
counter += 1;
}
}
void processData() {
y[0] = random(1, 100) / 100.0;
}
void showResults() {
myTransfer.sendDatum(y[0]);
}
Python seems to be able to send the floats one by one to Arduino, but I keep receiving the following error from rx_obj() -method:
Traceback (most recent call last):
File "C:\Users\Klupi\AppData\Local\Temp\ipykernel_16700\2863769408.py", line 20, in <module>
rec_float_ = link.rx_obj(obj_type='f')
File "C:\Users\Klupi\miniconda3\envs\deeplearning\lib\site-packages\pySerialTransfer\pySerialTransfer.py", line 346, in rx_obj
buff = bytes(self.rxBuff[start_pos:(start_pos + STRUCT_FORMAT_LENGTHS[obj_type])])
TypeError: 'str' object cannot be interpreted as an integer
Any thoughts here?
I am trying the one-by-one -approach as it is more simpler, but eventually I would like to send the data in chunks for improved speed.
Many thanks!
Use int32_t instead
Use int32_t instead
Thanks for the suggestion! That was a bit cryptic for me though (sorry for my uninformedness), but I gave it a go. I changed all int -definitions to int32_t. It did not change the outcome.
I tried to follow the examples too by changing floats into doubles, but no difference either.
How would you proceed from here?
Thank you for all the help!
I have also tried the following modifications:
1st:
Arduino:
void showResults() {
//myTransfer.sendDatum(y[0]);
uint32_t sendSize = 0;
sendSize = myTransfer.txObj(y, sendSize);
myTransfer.sendData(sendSize);
}
Python:
rec_float_ = link.rx_obj(obj_type=type(data),
obj_byte_size=32,
list_format='f')
2nd:
Arduino:
void showResults() {
//myTransfer.sendDatum(y[0]);
uint32_t sendSize = 0;
sendSize = myTransfer.txObj(y[0], sendSize);
myTransfer.sendData(sendSize);
}
Python:
rec_float_ = link.rx_obj(obj_type='f')
But they all result in the same error, that the bytes()-function is receiving a string instead of an integer.
I managed to achieve some progress after all. I was reading some posts from other issues and I ran into this (#32 (comment)):
"In order to receive data before parsing, you need to call available() (both Arduino and Python). This function is what actually parses packets."
So I added:
if idx+1 == len(data):
# NEW
while not link.available():
pass
# NEW
time.sleep(1)
And I have been able to receive data.
The following seems to be working, now sending floats in chunks:
Python:
import time
from pySerialTransfer import pySerialTransfer as txfer
try:
link = txfer.SerialTransfer('COM6', 115200) ########## replace COM port number ##########
link.open()
time.sleep(5)
start = 0
step = 26
counter = 0
data = [-0.272705078125,-0.333251953125,0.066650390625,-0.10638427734375,-0.272705078125,-0.333251953125,-0.066650390625,-0.10638427734375,-0.045440673828125,-0.288818359375,-0.022216796875,0.021270751953125,-0.045440673828125,-0.466552734375,-0.199951171875,0.10638427734375,-0.045440673828125,-0.199951171875,0.066650390625,0.14892578125,-0.045440673828125,-0.333251953125,0.422119140625,0.14892578125,-0.5908203125,-0.333251953125,0.333251953125,0.021270751953125,-0.5908203125,-0.288818359375,0.066650390625,-0.319091796875,-0.5908203125,-0.288818359375,-0.5556640625,0.021270751953125,-0.5908203125,-0.244384765625,-0.066650390625,0.021270751953125,-0.1817626953125,0.066650390625,0.111083984375,0.14892578125,-0.1817626953125,0.066650390625,0.066650390625,0.14892578125,-0.1817626953125,0.022216796875,-0.022216796875,-0.021270751953125,-0.1817626953125,0.066650390625,0.066650390625,-0.021270751953125,0.1363525390625,0.066650390625,-0.022216796875,-0.021270751953125,0.363525390625,-0.333251953125,0.022216796875,-0.021270751953125,0.09088134765625,-0.066650390625,-0.333251953125,-0.44677734375,0.09088134765625,-0.422119140625,-0.199951171875,-0.1915283203125,0.09088134765625,-0.111083984375,-0.111083984375,0.10638427734375,0.09088134765625,-0.244384765625,0.022216796875,-0.021270751953125,0.09088134765625,-0.066650390625,-0.066650390625,-0.021270751953125,0.09088134765625,0.066650390625,0.066650390625,-0.021270751953125,0.09088134765625,-0.022216796875,-0.199951171875,0.021270751953125,0.09088134765625,-0.466552734375,-0.333251953125,0.021270751953125,0.09088134765625,-0.066650390625,-0.333251953125,-0.36181640625,0.09088134765625,-0.066650390625,-0.199951171875,-0.36181640625,0.09088134765625,-0.066650390625,-0.333251953125,-0.4892578125,0.09088134765625,-0.244384765625,-0.199951171875,-0.4892578125,-0.1817626953125,-0.68896484375,-0.5556640625,-0.36181640625,-0.1817626953125,-0.288818359375,-0.333251953125,-0.36181640625,-0.09088134765625,-0.022216796875,-0.422119140625,-0.36181640625,0.0,-0.244384765625,-0.86669921875,-0.36181640625,-0.045440673828125,-0.199951171875,-0.199951171875,-0.10638427734375,-0.045440673828125,-0.066650390625,-0.333251953125,-0.10638427734375,0.09088134765625,-0.111083984375,-0.333251953125,-0.65966796875,0.09088134765625,0.199951171875,-0.7333984375,-0.36181640625,0.09088134765625,0.333251953125,-0.7333984375,-0.36181640625,0.1363525390625,0.066650390625,-0.9111328125,-0.36181640625,0.227294921875,-0.155517578125,-0.68896484375,-0.44677734375,0.227294921875,0.244384765625,-0.5556640625,-0.44677734375,0.09088134765625,0.288818359375,-0.60009765625,-0.44677734375,0.63623046875,0.111083984375,-0.422119140625,-0.74462890625,0.63623046875,0.066650390625,-0.60009765625,-0.74462890625,0.72705078125,0.333251953125,-0.377685546875,-0.6171875,0.72705078125,-0.066650390625,-0.60009765625,-0.6171875,0.818359375,0.199951171875,-0.77783203125,-0.6171875,0.818359375,0.199951171875,-0.7333984375,-0.6171875,0.77294921875,0.066650390625,-0.95556640625,-0.6171875,0.77294921875,-0.199951171875,-0.822265625,-0.6171875,0.818359375,0.022216796875,-0.95556640625,-0.6171875,0.77294921875,0.066650390625,-0.60009765625,-0.82958984375,0.77294921875,0.333251953125,-0.7333984375,-0.82958984375,0.77294921875,-0.377685546875,-0.86669921875,-0.82958984375,0.272705078125,-0.066650390625,-0.68896484375,-0.36181640625,0.272705078125,-0.066650390625,-0.68896484375,-0.7021484375,0.272705078125,0.155517578125,-0.60009765625,-0.7021484375,0.272705078125,0.333251953125,-1.0,-0.7021484375,0.77294921875,-0.155517578125,-0.822265625,-0.6171875,0.77294921875,0.60009765625,-1.0,-0.6171875,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
while True:
list_ = data[start:start+step]
send_size = link.tx_obj(list_)
link.send(send_size)
counter += len(list_)
print("Sent:\t\t{}:{} == {}".format(start, counter, list_))
time.sleep(0.01)
if counter >= len(data):
while not link.available():
pass
rec_float_ = link.rx_obj(obj_type='f')
print("Received:\t{}".format(rec_float_))
raise KeyboardInterrupt
start += step
except KeyboardInterrupt:
try:
link.close()
except:
pass
except:
import traceback
traceback.print_exc()
try:
link.close()
except:
pass
Arduino:
// SerialTransfer
#include "SerialTransfer.h"
constexpr int N_INPUTS = 65*4;
constexpr int N_OUTPUTS = 1;
constexpr int BUFF_SIZE = N_INPUTS/10;
float X_test[N_INPUTS] = { -1.0 };
float y[N_OUTPUTS] = { -1.0 };
int process_count = 0;
int counter = 0;
float buff[BUFF_SIZE];
SerialTransfer myTransfer;
void setup() {
Serial.begin(115200);
myTransfer.begin(Serial);
delay(4000);
}
void loop() {
receiveData();
if (counter == N_INPUTS) {
processData();
process_count += 1;
counter = 0;
showResults();
delay(3000);
}
}
void receiveData() {
if (myTransfer.available()) {
myTransfer.rxObj(buff);
for (int i = counter; i < counter+BUFF_SIZE; i++) {
X_test[i] = buff[i];
}
counter += BUFF_SIZE;
}
}
void processData() {
y[0] = random(1, 100) / 100.0;
}
void showResults() {
myTransfer.sendDatum(y[0]);
}