You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here's a simple example of how to use `async-pyserial`:
93
90
94
91
.. code-block:: python
@@ -102,7 +99,7 @@ Here's a simple example of how to use `async-pyserial`:
102
99
options.baudrate =9600
103
100
options.bytesize =8
104
101
options.stopbits =1
105
-
options.parity = SerialPortParity.NONE# NONE, ODD, EVEN
102
+
options.parity = SerialPortParity.NONE# NONE, ODD, EVEN
106
103
107
104
serial_port = SerialPort('/dev/ttyUSB0', options)
108
105
serial_port.on(SerialPortEvent.ON_DATA, on_data)
@@ -119,21 +116,23 @@ Here's a simple example of how to use `async-pyserial`:
119
116
120
117
API
121
118
---
122
-
123
119
### SerialPort
124
-
125
120
A class for serial communication.
126
121
127
122
#### Methods
128
123
129
124
- `__init__(self, port: str, options: SerialPortOptions)`: Initializes the serial port with the specified parameters.
130
-
- `def write(self, data: bytes)`: Writes `data` to the serial port (blocking operation).
125
+
- `def write(self, data: bytes, callback: Callable | None = None)`: Writes `data` to the serial port. Can be blocking or non-blocking. If a callback is provided, the write will be asynchronous. Supports `gevent`, `eventlet`, `asyncio`, `callback`, and synchronous operations.
126
+
- `def read(self, bufsize: int = 512, callback: Callable | None = None)`: Reads data from the serial port. Can be blocking or non-blocking. If a callback is provided, the read will be asynchronous. Supports `gevent`, `eventlet`, `asyncio`, `callback`, and synchronous operations.
131
127
- `def open(self)`: Opens the serial port.
132
128
- `def close(self)`: Closes the serial port.
133
129
- `def on(self, event: SerialPortEvent, callback: Callable[[bytes], None])`: Registers a callback for the specified event.
130
+
- `def emit(self, evt: str, *args, **kwargs)`: Emits an event, triggering all registered callbacks for that event.
131
+
- `def remove_all_listeners(self, evt: str)`: Removes all listeners for the specified event.
132
+
- `def remove_listener(self, evt: str, listener: Callable)`: Removes a specific listener for the specified event.
133
+
- `def off(self, evt: str, listener: Callable)`: Alias for `remove_listener`.
134
134
135
135
### SerialPortOptions
136
-
137
136
A class for specifying serial port options.
138
137
139
138
#### Attributes
@@ -144,21 +143,77 @@ A class for specifying serial port options.
- `read_timeout: int`: The read timeout in milliseconds.
146
145
- `write_timeout: int`: The write timeout in milliseconds.
146
+
- `read_bufsize: int`: The read buffer size. Default is 0. When `read_bufsize` is 0, the internal buffer is not used, and only data received after the read call will be returned. If `read_bufsize` is not 0, both buffered and new data will be returned.
147
147
148
148
### SerialPortEvent
149
-
150
149
An enumeration for serial port events.
151
150
152
151
- `ON_DATA`: Event triggered when data is received.
153
152
153
+
### SerialPortError
154
+
An exception class for handling serial port errors.
155
+
156
+
- `__init__(self, *args: object)`: Initializes the SerialPortError with the specified arguments.
157
+
158
+
### PlatformNotSupported
159
+
An exception class for handling unsupported platforms.
160
+
161
+
- `__init__(self, *args: object)`: Initializes the PlatformNotSupported exception with the specified arguments.
162
+
163
+
### set_async_worker
164
+
A function for setting the asynchronous worker.
165
+
166
+
- `def set_async_worker(w: str, loop = None)`: Sets the asynchronous worker to `gevent`, `eventlet`, or `asyncio`. Optionally, an event loop can be provided for `asyncio`.
167
+
168
+
Examples
169
+
--------
170
+
171
+
The `examples` directory contains sample scripts demonstrating how to use `async-pyserial` for various operations. Below are a few examples to help you get started.
172
+
173
+
- Basic read and write operations.
174
+
- Non-blocking read with asyncio.
175
+
- Using gevent and eventlet for asynchronous operations.
176
+
177
+
Example scripts included in the `examples` directory:
178
+
179
+
- `interval_write.py`: Demonstrates periodic writing to the serial port.
180
+
- `serialport_read.py`: Demonstrates reading from the serial port with different async workers.
181
+
- `serialport_terminal.py`: A terminal interface for interacting with the serial port.
0 commit comments