Hello friends,
I'm using the PySerialTransfer library GitHub - PowerBroker2/pySerialTransfer: Python package to transfer data in a fast, reliable, and packetized form
I'm trying to trigger some action in the Arduino Leonardo side when a specific char is received from python. I could have use int instead of char message but I take this as an exercice
All the manipulation I tried failed... I'm starting to be tired of this and probably missing some perspective from this point.
So, here my Python:
X1 = [0, 0, 0, 0, 0, ... , 0]
X2 = [1, 1, 1, 1, 1, .... , 1] ---> X1 and X2 are long array
try:
link = txfer.SerialTransfer('COM8', 115200, debug=True)
link.open()
start = 0
step = 10
counter = 0
while counter < len(X1):
sendSize = 0
X1_= X1[start:start+step] ---> I split it in order to get under 254 bytes per packet.
X2_= X2[start:start+step]
message = "notes"
messageSize = link.tx_obj(message)
sendSize += messageSize
X1Size = link.tx_obj(X1_, sendSize) - sendSize
sendSize += X1Size
X2Size = link.tx_obj(X2_, sendSize) - sendSize
sendSize += X2Size
link.send(sendSize, packet_id=0) ---> I recently tried to add packet_id information to don't messing up the receiving part.
counter += len(X1_)
#print('SENT: {} {} {}'.format(message, X1_, X2_))
while not link.available():
pass
rec_message = link.rx_obj(obj_type=type(message), obj_byte_size=messageSize)
rec_X1 = link.rx_obj(obj_type=type(X1_), obj_byte_size=X1Size, list_format='i', start_pos = messageSize)
rec_X2 = link.rx_obj(obj_type=type(X2_), obj_byte_size=X2Size, list_format='i', start_pos = messageSize + X1Size)
start += step
print("Recu:\t{} - {} et {}".format(rec_message, rec_X1, rec_X2))
while True:
sendSize2 = 0
message2 = "click"
messageSize = link.tx_obj(message2)
sendSize2 += messageSize
print("Ready to click...")
link.send(sendSize2, packet_id=0)
#print('SENT: {}'.format(message2))
while not link.available():
pass
rec_message = link.rx_obj(obj_type=type(message2), obj_byte_size=messageSize)
print("Recu:\t{}".format(rec_message))
raise KeyboardInterrupt
except KeyboardInterrupt:
try:
link.close()
except:
pass
except:
import traceback
traceback.print_exc()
try:
link.close()
except:
pass
And my Arduino:
#include "SerialTransfer.h"
#include "Keyboard.h"
#include "Mouse.h"
SerialTransfer myTransfer;
int32_t X1[10];
int32_t X2[10];
int32_t X1G[] = {};
int32_t X2G[] = {};
char message[5];
#Stuff I tried....
char notes[] = "notes";
char clickOk[] = "click";
void setup()
{
Keyboard.begin();
Mouse.begin();
Serial.begin(115200);
myTransfer.begin(Serial, true);
}
void loop()
{
if(myTransfer.available())
{
uint16_t recSize = 0;
recSize = myTransfer.rxObj(message, recSize);
if (strcmp(message, clickOk)) ---> Here my problem. How to check the received message and do action?
{
recSize = myTransfer.rxObj(X1, recSize);
recSize = myTransfer.rxObj(X2, recSize);
memmove(X1G + sizeof(X1G), noteTime, sizeof(X1));
memmove(X2G + sizeof(X2G), noteType, sizeof(X2));
uint16_t sendSize = 0;
sendSize = myTransfer.txObj(message, sendSize);
sendSize = myTransfer.txObj(X1, sendSize);
sendSize = myTransfer.txObj(X2, sendSize);
myTransfer.sendData(sendSize);
delay(50);
}
else if (strcmp(message, clickOk))
{
uint16_t sendSize2 = 0;
sendSize2 = myTransfer.txObj(message, sendSize2);
myTransfer.sendDatum(sendSize2);
Mouse.click();
}
}
}
My FTDI card will be coming soon, it should be easier for debugging, but in the meantime any help will be appreciated
I'm a beginner in this wide world. Maybe my mechanism is completely broken, so feel free to suggest another solution to get what I want.
Bye,
Vouncat