Mon réseau

Mon réseau d’entreprise

29.1 La communication série en python

Précédent   Suivant

Documentation

https://pythonhosted.org/pyserial/

Tutoriels

https://devtut.github.io/python/python-serial-communication-pyserial.html

Single-port TCP/IP - serial bridge (RFC 2217)

Simple cross platform RFC 2217 serial port server. It uses threads and is portable (runs on POSIX, Windows, etc).

   The port settings and control lines (RTS/DTR) can be changed at any time using RFC 2217 requests. The status lines (DSR/CTS/RI/CD) are polled every second and notifications are sent to the client.
   Telnet character IAC (0xff) needs to be doubled in data stream. IAC followed by an other value is interpreted as Telnet command sequence.
   Telnet negotiation commands are sent when connecting to the server.
   RTS/DTR are activated on client connect and deactivated on disconnect.
   Default port settings are set again when client disconnects.

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.

New in version 2.5.

rfc2217_server.py

29.1.1 Exemples de communication série python

Pour lister les ports série (UART), on peut utiliser la commande suivante :

python3 -m serial.tools.list_ports

from serial.tools import list_ports
list_ports.comports() # Outputs list of available serial ports

initialisation du device

import serial
#Serial takes these two parameters: serial device and baudrate
ser = serial.Serial('/dev/ttyUSB0', 9600)

Précédent   Suivant