【pySerial3.4官方文档】6、示例

示例

Miniterm 

Miniterm现在可用作模块而不是示例。有关详细信息,请参阅serial.tools.miniterm

miniterm.py

miniterm计划。

setup-miniterm-py2exe.py

这是Windows的py2exe安装脚本。它可用于创建独立的miniterm.exe

TCP / IP - 串行桥

该程序打开一个TCP / IP端口。当与该端口建立连接时(例如,使用telnet),它会将所有数据转发到串行端口,反之亦然。

此示例仅导出原始套接字连接。下面的下一个示例为客户端提供了对远程串行端口的更多控制。

  • 启动程序时,在命令行上设置串行端口设置。
  • 无法从远程更改设置。
  • 所有数据都按原样传递。
usage: tcp_serial_redirect.py [-h] [-q] [--parity {N,E,O,S,M}] [--rtscts]
                              [--xonxoff] [--rts RTS] [--dtr DTR]
                              [-P LOCALPORT]
                              SERIALPORT [BAUDRATE]

Simple Serial to Network (TCP/IP) redirector.

positional arguments:
  SERIALPORT            serial port name
  BAUDRATE              set baud rate, default: 9600

optional arguments:
  -h, --help            show this help message and exit
  -q, --quiet           suppress non error messages

serial port:
  --parity {N,E,O,S,M}  set parity, one of {N E O S M}, default: N
  --rtscts              enable RTS/CTS flow control (default off)
  --xonxoff             enable software flow control (default off)
  --rts RTS             set initial RTS line state (possible values: 0, 1)
  --dtr DTR             set initial DTR line state (possible values: 0, 1)

network settings:
  -P LOCALPORT, --localport LOCALPORT
                        local TCP port

NOTE: no security measures are implemented. Anyone can remotely connect to
this service over the network. Only one connection at once is supported. When
the connection is terminated it waits for the next connect.

tcp_serial_redirect.py

主程序。

单端口TCP / IP - 串行桥(RFC 2217)

简单的跨平台 RFC 2217串口服务器。它使用线程并且是可移植的(在POSIX,Windows等上运行)。

  • 可以随时使用更改端口设置和控制线(RTS / DTR) RFC 2217请求。每秒轮询状态行(DSR / CTS / RI / CD)并将通知发送到客户端。
  • Telnet字符IAC(0xff)需要在数据流中加倍。IAC后跟另一个值被解释为Telnet命令序列。
  • 连接服务器时会发送Telnet协商命令。
  • RTS / DTR在客户端连接时激活,在断开连接时禁用。
  • 客户端断开连接时,将再次设置默认端口设置。
usage: rfc2217_server.py [-h] [-p TCPPORT] [-v] SERIALPORT

RFC 2217 Serial to Network (TCP/IP) redirector.

positional arguments:
  SERIALPORT

optional arguments:
  -h, --help            show this help message and exit
  -p TCPPORT, --localport TCPPORT
                        local TCP port, default: 2217
  -v, --verbose         print more diagnostic messages (option can be given
                        multiple times)

NOTE: no security measures are implemented. Anyone can remotely connect to
this service over the network. Only one connection at once is supported. When
the connection is terminated it waits for the next connect.

2.5版中的新功能。

扫描二维码关注公众号,回复: 4303718 查看本文章

rfc2217_server.py

主程序。

setup-rfc2217_server-py2exe.py

这是Windows的py2exe安装脚本。它可用于创建独立的rfc2217_server.exe

多端口TCP / IP - 串行桥(RFC 2217)

此示例实现了一次使用多个端口的TCP / IP到串行端口服务。它使用select,无线程,用于串行端口和网络套接字,因此仅在POSIX系统上运行。

  • 完全控制串口 RFC 2217
  • 检查是否存在/tty/USB0...8。这是每5秒钟使用一次 os.path.exists
  • 当端口出现或消失时发送zeroconf公告(使用python-avahi和dbus)。服务名称:_serial_port._tcp
  • 每个串行端口都可用作一个TCP / IP服务器。例如 /dev/ttyUSB0,可到达<host>:7000
  • 所有端口和套接字的单个进程(不是每个端口)。
  • 该脚本可以作为守护进程启动。
  • 记录到stdout或作为守护进程运行到syslog。
  • 客户端断开连接时,将再次设置默认端口设置。
  • 调制解调器状态行(CTS / DSR / RI / CD)不会定期轮询,因此服务器不会自行发送NOTIFY_MODEMSTATE。但是它响应来自客户端的请求(即poll_modem在使用pySerial客户端时使用URL中的选项。)
usage: port_publisher.py [options]

Announce the existence of devices using zeroconf and provide
a TCP/IP <-> serial port gateway (implements RFC 2217).

If running as daemon, write to syslog. Otherwise write to stdout.

optional arguments:
  -h, --help            show this help message and exit

serial port settings:
  --ports-regex REGEX   specify a regex to search against the serial devices
                        and their descriptions (default: /dev/ttyUSB[0-9]+)

network settings:
  --tcp-port PORT       specify lowest TCP port number (default: 7000)

daemon:
  -d, --daemon          start as daemon
  --pidfile FILE        specify a name for the PID file

diagnostics:
  -o FILE, --logfile FILE
                        write messages file instead of stdout
  -q, --quiet           suppress most diagnostic messages
  -v, --verbose         increase diagnostic messages

NOTE: no security measures are implemented. Anyone can remotely connect to
this service over the network. Only one connection at once, per port, is
supported. When the connection is terminated, it waits for the next connect.

要求:

  • Python(> = 2.4)
  • Python-的avahi
  • Python-DBUS
  • python-serial(> = 2.5)

作为守护进程安装:

  • 将脚本复制port_publisher.py/usr/local/bin
  • 将脚本复制port_publisher.sh/etc/init.d
  • 使用添加指向运行级别的链接 update-rc.d port_publisher.sh defaults 99
  • 就是这样:-)服务将在下次重启时启动。或者以root身份运行。invoke-rc.d port_publisher.sh start

版本2.5中的新功能新示例

port_publisher.py

用于POSIX环境的多端口TCP / IP串行转换器(RFC 2217)。

port_publisher.sh

示例init.d脚本。

wxPython示例

这里显示了一个简单的wxPython终端应用程序和一个灵活的串口配置对话框。

wxTerminal.py

简单的终端应用程序。请注意,缓冲区的长度受到wx的限制,并且可能会突然停止显示新输入。

wxTerminal.wxg

终端应用程序的wxGlade设计文件。

wxSerialConfigDialog.py

灵活的串行端口配置对话框。

wxSerialConfigDialog.wxg

配置对话框的wxGlade设计文件。

setup-wxTerminal-py2exe.py

用于打包终端应用程序的py2exe安装脚本。

单元测试

该项目使用多个单元测试来验证功能。他们都需要一个环回连接器。脚本本身包含更多信息。所有测试脚本都包含在目录中test

loop://除非在命令行(sys.argv[1])上给出不同的设备名称或URL,否则将在端口上执行单元测试。例如,可以使用连接的USB串行转换器上的测试hwgrep://USB或实际名称,如/dev/ttyUSB0COM1(取决于平台)。

run_all_tests.py

从所有test*文件中收集所有测试并运行它们。默认情况下,使用 loop://设备。

test.py

基本测试(二进制功能,超时,控制线)。

test_advanced.py

测试更高级的功能(属性)。

test_high_load.py

测试涉及发送大量数据。

test_readline.py

涉及的测试readline

test_iolib.py

涉及io图书馆的测试。仅适用于Python 2.6及更高版本。

test_url.py

涉及URL功能的测试。

猜你喜欢

转载自blog.csdn.net/Yuyh131/article/details/84168580