Skip to main content

serial2udp 收发器

项目描述

一般的

这是串口到 UDP 数据包的收发器。如果线路匹配给定的选择标准,则将串行端口读取为 ascii 并将文本作为 UDP 数据报发送,或者通过 UDP 数据报提供的命令与设备通信。

安装

从 pypi 运行安装

pip install serial2udp

从来源

git clone https://github.com/bslew/serial2udp.git serial2udp
cd serial2udp
python3 -m venv venv
source venv/bin/activate
python setup.py build install

使用示例

示例 1

假设您从设备的串行端口读取数据,/dev/device1并希望通过 UDP 包将该数据分发到本地网络。

假设设备生成 ascii 文本行:

line1 word1 word2 ...

line2 word3 word4 ...

 ...

udp key1=val1, key2=val2, ...

lineN  ...

udp key3=val3, key4=val4, ...

如果我们希望通过 UDP 分发只有以“udp”开头的行执行:

(venv) $ serial2udp.py --serport /dev/device1 --host xxx.xxx.xxx.xxx -p port --ifstarts_with 'udp '

或者干脆

(venv) $ serial2udp --serport /dev/device1 --host xxx.xxx.xxx.xxx -p port --ifstarts_with 'udp '

对于上面的示例,该命令将生成两个 UDP 数据报,其中包含

key1=val1, key2=val2, ...

key3=val3, key4=val4, ...

该程序不会以任何方式解析数据,但如果--ifstarts_with使用选项,则从该选项中给出的值(在这种情况下为“udp”)剥离行字符串。串行通信选项是可定制的。

示例 2

假设您要执行读取和分发从设备读取的值,并通过 UDP 接收简单命令并将它们发送到设备,例如更改某些继电器的状态。扩展示例 1,让我们分发序列化字典并在 localhost 的端口 10001 上侦听传入命令。执行:

(venv) $ serial2udp.py --serport /dev/device1 --host xxx.xxx.xxx.xxx -p port --ifstarts_with 'udp ' --srvport 10001 --srvhost 127.0.0.1

现在,如果您将 UDP 数据报发送到 127.0.0.1:10001,它将直接写入串行端口。

用法

$ serial2udp.py --help
usage: serial2udp [-h] [-v] [-V] [--serport SERPORT]
                  [--baudrate BAUDRATE] [--parity PARITY]
                  [--bytesize BYTESIZE] [--stopbits STOPBITS]
                  [--host HOST] [--srvport SRVPORT] [-p PORT]
                  [--dummy DUMMY] [--dummy_wait DUMMY_WAIT]
                  [--ifstarts_with IFSTARTS_WITH] [--read_test]

  Created by Bartosz Lew on 2020-10-06.
  Copyright 2020 Bartosz Lew. All rights reserved.

  Licensed under the Apache License 2.0
  http://www.apache.org/licenses/LICENSE-2.0

  Distributed on an "AS IS" basis without warranties
  or conditions of any kind, either express or implied.

USAGE

optional arguments:
  -h, --help            show this help message and exit
  -v, --verbose         set verbosity level [default: 0]
  -V, --version         show program's version number and exit
  --serport SERPORT     serial port [default: /dev/ttyACM1]
  --baudrate BAUDRATE   serial communication baudrate [default: 9600]
  --parity PARITY       serial communication parity [default: None]
  --bytesize BYTESIZE   serial communication bytesize [default: 8]
  --stopbits STOPBITS   serial communication stopbits [default: 1]
  --host HOST           UDP datagram destination host [default: 127.0.0.1]
  --srvhost SRVHOST     UDP to serial command server address [default: 127.0.0.1]
  --srvport SRVPORT     UDP to serial command server port [default: 10001]
  -p PORT, --port PORT  UDP destination port [default: 10000]
  --dummy DUMMY         String that should be sent. No serial port is read. [default: ]
  --dummy_wait DUMMY_WAIT
                        Wait time [s] between two dummy sends. [default: 1]
  --ifstarts_with IFSTARTS_WITH
                        send UDP only if the serial line starts with this string [default: ]
  --read_test           triggers read send read mode

    

附加脚本

简单的 UDP 阅读器

该软件包包含一个简单的 UDP 数据报读取器和写入器。serial2udp程序分发的读取UDP数据报使用:

(venv) $ readUDP.py 10001 127.0.0.1 0

简单的 UDP 编写器

为了发送测试 UDP 数据报,请使用:

(venv) $ sendUDP.py "example datagram body" 10001 

作者

Bartosz Lew < bartosz.lew@protonmail.com >

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

serial2udp-1.0.13.tar.gz (8.4 kB 查看哈希

已上传 source