Power_Broker:
It would be much faster and reliable to interface Python with your Arduino using the compatible libraries: pySerialTransfer and SerialTransfer.h.
pySerialTransfer is pip-installable and cross-platform compatible. SerialTransfer.h runs on the Arduino platform and can be installed through the Arduino IDE's Libraries Manager.
Both of these libraries have highly efficient and robust packetizing/parsing algorithms with easy to use APIs.
Example Python Script:
import time
from pySerialTransfer import pySerialTransfer as txfer
if name == 'main':
try:
link = txfer.SerialTransfer('COM17')
link.open()
time.sleep(2) # allow some time for the Arduino to completely reset
while True:
send_size = 0
###################################################################
# Send a list
###################################################################
list_ = [1, 3]
list_size = link.tx_obj(list_)
send_size += list_size
###################################################################
# Send a string
###################################################################
str_ = 'hello'
str_size = link.tx_obj(str_, send_size) - send_size
send_size += str_size
###################################################################
# Send a float
###################################################################
float_ = 5.234
float_size = link.tx_obj(float_, send_size) - send_size
send_size += float_size
###################################################################
# Transmit all the data to send in a single packet
###################################################################
link.send(send_size)
###################################################################
# Wait for a response and report any errors while receiving packets
###################################################################
while not link.available():
if link.status < 0:
if link.status == -1:
print('ERROR: CRC_ERROR')
elif link.status == -2:
print('ERROR: PAYLOAD_ERROR')
elif link.status == -3:
print('ERROR: STOP_BYTE_ERROR')
###################################################################
# Parse response list
###################################################################
rec_list_ = link.rx_obj(obj_type=type(list_),
obj_byte_size=list_size,
list_format='i')
###################################################################
# Parse response string
###################################################################
rec_str_ = link.rx_obj(obj_type=type(str_),
obj_byte_size=str_size,
start_pos=list_size)
###################################################################
# Parse response float
###################################################################
rec_float_ = link.rx_obj(obj_type=type(float_),
obj_byte_size=float_size,
start_pos=(list_size + str_size))
###################################################################
# Display the received data
###################################################################
print('SENT: {} {} {}'.format(list_, str_, float_))
print('RCVD: {} {} {}'.format(rec_list_, rec_str_, rec_float_))
print(' ')
except KeyboardInterrupt:
link.close()
except:
import traceback
traceback.print_exc()
link.close()
**Example Arduino Sketch:**
#include "SerialTransfer.h"
SerialTransfer myTransfer;
void setup()
{
Serial.begin(115200);
myTransfer.begin(Serial);
}
void loop()
{
if(myTransfer.available())
{
// send all received data back to Python
for(uint16_t i=0; i < myTransfer.bytesRead; i++)
myTransfer.txBuff[i] = myTransfer.rxBuff[i];
myTransfer.sendData(myTransfer.bytesRead);
}
}
On the Arduino side, you can use `myTransfer.txObj()` and `myTransfer.rxObj()` to copy values to the library's RX buffer and parse multi-byte variables out of the library's TX buffer.
For theory behind robust serial communication, check out the tutorials [Serial Input Basics](https://forum.arduino.cc/index.php?topic=396450.0) and [Serial Input Advanced](https://forum.arduino.cc/index.php?topic=662346.0).
I already tried to use this library, however, I didn't find much reference about it. I think you could help me with that.
I don't speak English very well, it makes me a little difficult to understand some things that I only find in English.
Well, I tried to start again by sending information from Arduino to Python when the button was pressed on the interrupt pin. It worked, however, python does not read the data correctly, it is empty data.
Arduino
#include "SerialTransfer.h"
SerialTransfer myTransfer;
int Botao = 3;
int modo = 0;
void setup()
{
Serial.begin(115200);
myTransfer.begin(Serial);
attachInterrupt(digitalPinToInterrupt(Botao), botaoAcionado , RISING);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}
void loop()
{
if (modo == 1){
digitalWrite(LED_BUILTIN, HIGH);
myTransfer.rxBuff[0] = '2';
myTransfer.sendData(1);
delay(100);
modo = 0;
}
}
void botaoAcionado(){
modo = 1;
}
Python
import time
from pySerialTransfer import pySerialTransfer as txfer
try:
link = txfer.SerialTransfer('COM12')
link.open()
time.sleep(2)
while True:
while not link.available():
if link.status < 0:
print('ERROR: {}'.format(link.status))
response = ''
for index in range(link.bytesRead):
response += chr(link.rxBuff[index])
print(f'Response received:{response}')
except KeyboardInterrupt:
link.close()
Python - sometimes send more than one data when the button is pressed
Response received: