pySerialTransfer


NamepySerialTransfer JSON
Version 2.6.9 PyPI version JSON
download
home_pagehttps://github.com/PowerBroker2/pySerialTransfer
SummaryPython package used to transmit and receive low overhead byte packets - especially useful for PC<-->Arduino USB communication (compatible with https://github.com/PowerBroker2/SerialTransfer)
upload_time2023-11-29 22:18:13
maintainer
docs_urlNone
authorPower_Broker
requires_python
license
keywords arduino serial usb protocol communication
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pySerialTransfer
[![GitHub version](https://badge.fury.io/gh/PowerBroker2%2FpySerialTransfer.svg)](https://badge.fury.io/gh/PowerBroker2%2FpySerialTransfer) [![PyPI version](https://badge.fury.io/py/pySerialTransfer.svg)](https://badge.fury.io/py/pySerialTransfer)<br /><br />
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
```python
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 == txfer.CRC_ERROR:
                        print('ERROR: CRC_ERROR')
                    elif link.status == txfer.PAYLOAD_ERROR:
                        print('ERROR: PAYLOAD_ERROR')
                    elif link.status == txfer.STOP_BYTE_ERROR:
                        print('ERROR: STOP_BYTE_ERROR')
                    else:
                        print('ERROR: {}'.format(link.status))
            
            ###################################################################
            # 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
```C++
#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
```Python
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()
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/PowerBroker2/pySerialTransfer",
    "name": "pySerialTransfer",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "Arduino,serial,usb,protocol,communication",
    "author": "Power_Broker",
    "author_email": "gitstuff2@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/cd/97/6845a58f1450f2dbdd58178d7f939429dbc5a06db622cff167742c3a792b/pySerialTransfer-2.6.9.tar.gz",
    "platform": null,
    "description": "# pySerialTransfer\r\n[![GitHub version](https://badge.fury.io/gh/PowerBroker2%2FpySerialTransfer.svg)](https://badge.fury.io/gh/PowerBroker2%2FpySerialTransfer) [![PyPI version](https://badge.fury.io/py/pySerialTransfer.svg)](https://badge.fury.io/py/pySerialTransfer)<br /><br />\r\nPython package to transfer data in a fast, reliable, and packetized form.\r\n\r\nIf 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).\r\n\r\n# To Install\r\n```\r\npip install pySerialTransfer\r\n```\r\n\r\n# Example Python Script\r\n```python\r\nimport time\r\nfrom pySerialTransfer import pySerialTransfer as txfer\r\n\r\n\r\nif __name__ == '__main__':\r\n    try:\r\n        link = txfer.SerialTransfer('COM17')\r\n        \r\n        link.open()\r\n        time.sleep(2) # allow some time for the Arduino to completely reset\r\n        \r\n        while True:\r\n            send_size = 0\r\n            \r\n            ###################################################################\r\n            # Send a list\r\n            ###################################################################\r\n            list_ = [1, 3]\r\n            list_size = link.tx_obj(list_)\r\n            send_size += list_size\r\n            \r\n            ###################################################################\r\n            # Send a string\r\n            ###################################################################\r\n            str_ = 'hello'\r\n            str_size = link.tx_obj(str_, send_size) - send_size\r\n            send_size += str_size\r\n            \r\n            ###################################################################\r\n            # Send a float\r\n            ###################################################################\r\n            float_ = 5.234\r\n            float_size = link.tx_obj(float_, send_size) - send_size\r\n            send_size += float_size\r\n            \r\n            ###################################################################\r\n            # Transmit all the data to send in a single packet\r\n            ###################################################################\r\n            link.send(send_size)\r\n            \r\n            ###################################################################\r\n            # Wait for a response and report any errors while receiving packets\r\n            ###################################################################\r\n            while not link.available():\r\n                if link.status < 0:\r\n                    if link.status == txfer.CRC_ERROR:\r\n                        print('ERROR: CRC_ERROR')\r\n                    elif link.status == txfer.PAYLOAD_ERROR:\r\n                        print('ERROR: PAYLOAD_ERROR')\r\n                    elif link.status == txfer.STOP_BYTE_ERROR:\r\n                        print('ERROR: STOP_BYTE_ERROR')\r\n                    else:\r\n                        print('ERROR: {}'.format(link.status))\r\n            \r\n            ###################################################################\r\n            # Parse response list\r\n            ###################################################################\r\n            rec_list_  = link.rx_obj(obj_type=type(list_),\r\n                                     obj_byte_size=list_size,\r\n                                     list_format='i')\r\n            \r\n            ###################################################################\r\n            # Parse response string\r\n            ###################################################################\r\n            rec_str_   = link.rx_obj(obj_type=type(str_),\r\n                                     obj_byte_size=str_size,\r\n                                     start_pos=list_size)\r\n            \r\n            ###################################################################\r\n            # Parse response float\r\n            ###################################################################\r\n            rec_float_ = link.rx_obj(obj_type=type(float_),\r\n                                     obj_byte_size=float_size,\r\n                                     start_pos=(list_size + str_size))\r\n            \r\n            ###################################################################\r\n            # Display the received data\r\n            ###################################################################\r\n            print('SENT: {} {} {}'.format(list_, str_, float_))\r\n            print('RCVD: {} {} {}'.format(rec_list_, rec_str_, rec_float_))\r\n            print(' ')\r\n    \r\n    except KeyboardInterrupt:\r\n        try:\r\n            link.close()\r\n        except:\r\n            pass\r\n    \r\n    except:\r\n        import traceback\r\n        traceback.print_exc()\r\n        \r\n        try:\r\n            link.close()\r\n        except:\r\n            pass\r\n```\r\n\r\n# Example Arduino Sketch\r\n```C++\r\n#include \"SerialTransfer.h\"\r\n\r\n\r\nSerialTransfer myTransfer;\r\n\r\n\r\nvoid setup()\r\n{\r\n  Serial.begin(115200);\r\n  myTransfer.begin(Serial);\r\n}\r\n\r\n\r\nvoid loop()\r\n{\r\n  if(myTransfer.available())\r\n  {\r\n    // send all received data back to Python\r\n    for(uint16_t i=0; i < myTransfer.bytesRead; i++)\r\n      myTransfer.packet.txBuff[i] = myTransfer.packet.rxBuff[i];\r\n    \r\n    myTransfer.sendData(myTransfer.bytesRead);\r\n  }\r\n}\r\n```\r\n\r\n# Example Python Script with Callback Functionality\r\nNote that you can specify many callbacks, but only one per packet ID\r\n```Python\r\nimport time\r\nfrom pySerialTransfer import pySerialTransfer as txfer\r\n\r\n\r\ndef hi():\r\n    '''\r\n    Callback function that will automatically be called by link.tick() whenever\r\n    a packet with ID of 0 is successfully parsed.\r\n    '''\r\n    \r\n    print(\"hi\")\r\n    \r\n'''\r\nlist of callback functions to be called during tick. The index of the function\r\nreference within this list must correspond to the packet ID. For instance, if\r\nyou want to call the function hi() when you parse a packet with an ID of 0, you\r\nwould write the callback list with \"hi\" being in the 0th place of the list:\r\n'''\r\ncallback_list = [ hi ]\r\n\r\n\r\nif __name__ == '__main__':\r\n    try:\r\n        link = txfer.SerialTransfer('COM17')\r\n        \r\n        link.set_callbacks(callback_list)\r\n        link.open()\r\n        time.sleep(2) # allow some time for the Arduino to completely reset\r\n        \r\n        while True:\r\n            link.tick()\r\n    \r\n    except KeyboardInterrupt:\r\n        link.close()\r\n    \r\n    except:\r\n        import traceback\r\n        traceback.print_exc()\r\n        \r\n        link.close()\r\n```\r\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Python package used to transmit and receive low overhead byte packets - especially useful for PC<-->Arduino USB communication (compatible with https://github.com/PowerBroker2/SerialTransfer)",
    "version": "2.6.9",
    "project_urls": {
        "Download": "https://github.com/PowerBroker2/pySerialTransfer/archive/2.6.9.tar.gz",
        "Homepage": "https://github.com/PowerBroker2/pySerialTransfer"
    },
    "split_keywords": [
        "arduino",
        "serial",
        "usb",
        "protocol",
        "communication"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cd976845a58f1450f2dbdd58178d7f939429dbc5a06db622cff167742c3a792b",
                "md5": "632ddbd151416898baa929dd7816712b",
                "sha256": "42cc8faa65d58bf4ca640575fcceca601e33aeffe1c8d771c9768c952d7a2b02"
            },
            "downloads": -1,
            "filename": "pySerialTransfer-2.6.9.tar.gz",
            "has_sig": false,
            "md5_digest": "632ddbd151416898baa929dd7816712b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 10184,
            "upload_time": "2023-11-29T22:18:13",
            "upload_time_iso_8601": "2023-11-29T22:18:13.657731Z",
            "url": "https://files.pythonhosted.org/packages/cd/97/6845a58f1450f2dbdd58178d7f939429dbc5a06db622cff167742c3a792b/pySerialTransfer-2.6.9.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-29 22:18:13",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "PowerBroker2",
    "github_project": "pySerialTransfer",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pyserialtransfer"
}
        
Elapsed time: 0.15987s