Package: ser2sock
ser2sockA single-threaded, multi serial line to TCP bridge server.Can run under python 2.6 up to 3.x (tested 3.8).InstallationFrom within your favorite python environment:pip install ser2sockUsageser2sock -c <configuration file>ConfigurationIn order to provide flexibility, configuration is written in python.The only requirement is to have abridgesmember which consists of a
sequence of bridges. A bridge is a dictionary with mandatory keysserialandtcp.Example:bridges=[{'serial':{'port':'/dev/ttyS0'},'tcp':{'address':("0",18500)}},{'serial':{'port':'/dev/ttyS1','baudrate':19200},'tcp':{'address':("0",18501),'no_delay':False}}]serial:portmandatory. Supports any keyword supported byserial.serial_for_url(orserial.Serialifserial_for_urldoes not
existtcp:addressmandatory (must be a pair bind host and port).reuse_addr: (default: True) TCP reuse addressno_delay: (default: True) disable Nagle's algorithmtos: (default:0x10, meaning low delay) type of service.tcpandserialhelpers are automatically loaded to the config namespace.
Here is the equivalent above config using helpers:bridges=[[serial(port="/dev/ttyS0"),tcp(address=("0",18500))],[serial(port="/dev/ttyS1",baudrate=19200),tcp(address=("0",18501),no_delay=False)],]You are free to put any code in your python configuration file.
Here is an example setting up logging:importlogginglogging.basicConfig(level=logging.DEBUG,format='%(asctime)s%(levelname)s%(message)s')bridges=[[serial(port="/dev/ttyS0"),tcp(address=("0",18500))],[serial(port="/dev/ttyS1",baudrate=19200),tcp(address=("0",18501),no_delay=False)],]Web UIThe active configuration can be changed online through a web UI.To enable web you need to install the extra package:$pipinstallser2sock[web]...and enable the web app in the configuration with:bridges=[...]web=':8000'ser2sock should now be visiblehere.You should see something like this:Note that changes made with the web interface only affect the
active ser2sock instance and never the original configuration file.TestsTests should be performed within a python 3.5 or higher environment.$pythonsetup.pytestrunning pytestSearching for pytest-asyncioBest match: pytest-asyncio 0.14.0Processing pytest_asyncio-0.14.0-py3.7.eggUsing /home/tcoutinho/workspace/ser2sock/.eggs/pytest_asyncio-0.14.0-py3.7.eggSearching for pytest-covBest match: pytest-cov 2.10.0Processing pytest_cov-2.10.0-py3.7.eggUsing /home/tcoutinho/workspace/ser2sock/.eggs/pytest_cov-2.10.0-py3.7.eggrunning egg_infowriting ser2sock.egg-info/PKG-INFOwriting dependency_links to ser2sock.egg-info/dependency_links.txtwriting entry points to ser2sock.egg-info/entry_points.txtwriting requirements to ser2sock.egg-info/requires.txtwriting top-level names to ser2sock.egg-info/top_level.txtreading manifest file 'ser2sock.egg-info/SOURCES.txt'writing manifest file 'ser2sock.egg-info/SOURCES.txt'running build_ext==================================== test session starts =====================================platform linux -- Python 3.7.7, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 -- /home/tcoutinho/miniconda/envs/py37/bin/pythoncachedir: .pytest_cacherootdir: /home/tcoutinho/workspace/ser2sock, inifile: setup.cfgplugins: cov-2.10.0, asyncio-0.14.0collected 8 itemstests/test_ser2sock.py::test_load_config PASSED                                        [ 12%]tests/test_ser2sock.py::test_web_server PASSED                                         [ 25%]tests/test_ser2sock.py::test_server PASSED                                             [ 37%]tests/test_ser2sock.py::test_server_no_serial PASSED                                   [ 50%]tests/test_ser2sock.py::test_server_serial_close_after_success PASSED                  [ 62%]tests/test_ser2sock.py::test_server_no_client PASSED                                   [ 75%]tests/test_ser2sock.py::test_server_missing_argument PASSED                            [ 87%]tests/test_ser2sock.py::test_2_clients_to_1_serial PASSED                              [100%]----------- coverage: platform linux, python 3.7.7-final-0 -----------Name                   Stmts   Miss  Cover------------------------------------------ser2sock/__init__.py       1      0   100%ser2sock/server.py       321     56    83%------------------------------------------TOTAL                    322     56    83%Coverage HTML written to dir htmlcov================================== slowest 2 test durations ==================================0.02s call     tests/test_ser2sock.py::test_server_no_client0.02s setup    tests/test_ser2sock.py::test_web_server===================================== 8 passed in 0.30s ======================================That's all folks!