ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • serial_asyncio
    Python/Raspberry Pi 2021. 10. 8. 14:41

    Overview

    Serial transports, protocols and streams

    This module layers asyncio support onto pySerial. It provides support for working with serial ports through asyncio Transports, Protocols, and Streams.

    Transports are a low-level abstraction, provided by this package in the form of an asyncio.Transport implementation called SerialTransport, which manages the asynchronous transmission of data through an underlying pySerial Serial instance. Transports are concerned with how bytes are transmitted through the serial port.

    Protocols are a callback-based abstraction which determine which bytes are transmitted through an underlying transport. You can implement a subclass of asyncio.Protocol which reads from, and/or writes to, a SerialTransport. When a serial connection is established your protocol will be handed a transport, to which your protocol implementation can write data as necessary. Incoming data and other serial connection lifecycle events cause callbacks on your protocol to be invoked, so it can take action as necessary.

    Usually, you will not create a SerialTransport directly. Rather, you will define a Protocol class and pass that protocol to a function such as create_serial_connection() which will instantiate your Protocol and connect it to a SerialTransport.

    Streams are a coroutine-based alternative to callback-based protocols. This package provides a function open_serial_connection() which returns asyncio.StreamReader and asyncio.StreamWriter objects for interacting with underlying protocol and transport objects, which this library will create for you.

    Protocol Example

    This example defines a very simple Protocol which sends a greeting message through the serial port and displays to the console any data received through the serial port, until a newline byte is received.

    A call is made to create_serial_connection(), to which the protocol class (not an instance) is passed, together with arguments destined for the Serial constructor. This call returns a coroutine object. When passed to run_until_complete() the coroutine is scheduled to run as an asyncio.Task by the asyncio library, and the result of the coroutine, which is a tuple containing the transport and protocol instances, return to the caller.

    While the event loop is running (run_forever()), or until the protocol closes the transport itself, the protocol will process data received through the serial port asynchronously:

     

     

     

    출처: https://pyserial-asyncio.readthedocs.io/en/latest/shortintro.html

    728x90

    'Python > Raspberry Pi' 카테고리의 다른 글

    /dev/tty  (0) 2021.10.20
    baudrate  (0) 2021.10.20
    터미널 명령어 return값 불러오기  (0) 2021.10.14
    Raspberry pi wifi change without reboot  (0) 2021.10.14
    라즈베리 파이 WIFI 및 고정 IP할당  (0) 2021.10.13

    댓글

Designed by Tistory.