Skip to content

PySerial

This module encapsulates the access for the serial port. It provides backends for Python running on Windows, OSX, Linux, BSD (possibly any POSIX compliant system) and IronPython. The module named “serial” automatically selects the appropriate backend.

CLI Usage

목록 출력:

python -m serial.tools.list_ports

시리얼 연결:

sudo -s
cd /home/your/.pyenv/versions/opy-your-3.9.11/bin
./python -m serial.tools.miniterm --encoding Latin1 /dev/ttyUSB0 115200 | tee /home/your/Downloads/logging

위와 같이 사용하는 이유는, pyenv를 사용할 경우 sudo를 사용하면 전역 python을 사용하게 되므로 pyenv 환경이 설치된 디렉토리에 직접 이동해서 실행해야 한다

시리얼 권한 추가 방법

Serial Communication#리눅스 권한 추가 항목 참조. 간단히, sudo adduser $USER dialout

Opening serial ports

Open port at “9600,8,N,1”, no timeout:

>>> import serial
>>> ser = serial.Serial('/dev/ttyUSB0')  # open serial port
>>> print(ser.name)         # check which port was really used
>>> ser.write(b'hello')     # write a string
>>> ser.close()             # close port

APIs

serial_for_url

/dev/ttyUSB0 같은 시리얼 연결시 사용되는 kwargs#Serial의 __init__함수 참조.

Serial

Parameters:

  • port – Device name or None.
  • baudrate (int) – Baud rate such as 9600 or 115200 etc.
  • bytesize – Number of data bits. Possible values: FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS
  • parity – Enable parity checking. Possible values: PARITY_NONE, PARITY_EVEN, PARITY_ODD PARITY_MARK, PARITY_SPACE
  • stopbits – Number of stop bits. Possible values: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
  • timeout (float) – Set a read timeout value in seconds.
  • xonxoff (bool) – Enable software flow control.
  • rtscts (bool) – Enable hardware (RTS/CTS) flow control.
  • dsrdtr (bool) – Enable hardware (DSR/DTR) flow control.
  • write_timeout (float) – Set a write timeout value in seconds.
  • inter_byte_timeout (float) – Inter-character timeout, None to disable (default).
  • exclusive (bool) – Set exclusive access mode (POSIX only). A port cannot be opened in exclusive access mode if it is already open in exclusive access mode.

See also

Favorite site