pySerialTransfer
Python package to transfer data in a fast, reliable, and packetized form.
If using this package to communicate with Arduinos, see https://github.com/PowerBroker2/SerialTransfer for the corresponding and compatible library (also available through the Arduino IDE's Libraries Manager).
To Install
pip install pySerialTransfer
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():
# A negative value for status indicates an error
if link.status.value < 0:
if link.status == txfer.Status.CRC_ERROR:
print('ERROR: CRC_ERROR')
elif link.status == txfer.Status.PAYLOAD_ERROR:
print('ERROR: PAYLOAD_ERROR')
elif link.status == txfer.Status.STOP_BYTE_ERROR:
print('ERROR: STOP_BYTE_ERROR')
else:
print('ERROR: {}'.format(link.status.name))
###################################################################
# 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:
try:
link.close()
except:
pass
except:
import traceback
traceback.print_exc()
try:
link.close()
except:
pass
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.packet.txBuff[i] = myTransfer.packet.rxBuff[i];
myTransfer.sendData(myTransfer.bytesRead);
}
}
Example Python Script with Callback Functionality
Note that you can specify many callbacks, but only one per packet ID
import time
from pySerialTransfer import pySerialTransfer as txfer
def hi():
'''
Callback function that will automatically be called by link.tick() whenever
a packet with ID of 0 is successfully parsed.
'''
print("hi")
'''
list of callback functions to be called during tick. The index of the function
reference within this list must correspond to the packet ID. For instance, if
you want to call the function hi() when you parse a packet with an ID of 0, you
would write the callback list with "hi" being in the 0th place of the list:
'''
callback_list = [ hi ]
if __name__ == '__main__':
try:
link = txfer.SerialTransfer('COM17')
link.set_callbacks(callback_list)
link.open()
time.sleep(2) # allow some time for the Arduino to completely reset
while True:
link.tick()
except KeyboardInterrupt:
link.close()
except:
import traceback
traceback.print_exc()
link.close()
Metadata
Release files for pySerialTransfer 2.6.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyserialtransfer-2.6.11.tar.gz | 16.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pySerialTransfer-2.6.11-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 26.1 kB
Release files / pyserialtransfer-2.6.11.tar.gz
| Download URL | pyserialtransfer-2.6.11.tar.gz |
|---|---|
| Size | 16.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4adb618867a0965d1c9c76941614fdc7d6b71ce46c87a813ff74f35b20bbc88a
|
|
BLAKE2b-256 checksum How to use checksums |
ebb809b7d6d5da7da6dcfac3a3aef0d061f50cc9fa64de2bafaf56f8cc7d03c9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.7
|
Release files / pySerialTransfer-2.6.11-py3-none-any.whl
| Download URL | pySerialTransfer-2.6.11-py3-none-any.whl |
|---|---|
| Size | 9.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a4dc041fdd6ecca2e870c499d707091c3f327cefc0031c5731ec188edff56197
|
|
BLAKE2b-256 checksum How to use checksums |
dfbc3493c9014c9581f72b9501485c53f3e562af9ffb9e1c0a4cfd509724f0a8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.7
|