Skip to content

Commit 7cda50a

Browse files
committed
lib/base: Add RS232 and RS485 support.
Signed-off-by: Tinyu-Zhao <[email protected]>
1 parent 8fe6685 commit 7cda50a

File tree

11 files changed

+1878
-4
lines changed

11 files changed

+1878
-4
lines changed

docs/en/base/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ Base
88
atom_socket.rst
99
echo.rst
1010
motion.rst
11+
rs232.rst
12+
rs485.rst
1113
speaker.rst

docs/en/base/rs232.rst

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
Atomic RS232 Base
2+
==================
3+
4+
.. sku: K046/A136
5+
6+
.. include:: ../refs/base.rs232.ref
7+
8+
AtomRS232 Class provides a set of methods to control the RS232 module. Through the
9+
UART interface, the module can transmit and receive data, supporting various baud rates
10+
and flow control configurations.
11+
12+
Support the following products:
13+
14+
================== ==================
15+
|RS232 Base| |RS232|
16+
================== ==================
17+
18+
UiFlow2 Example
19+
---------------
20+
21+
TX Example
22+
^^^^^^^^^^
23+
24+
Open the |core_rs232_tx_example.m5f2| project in UiFlow2.
25+
26+
This example demonstrates how to send data using the RS232 module via the UART interface.
27+
28+
UiFlow2 Code Block:
29+
30+
|tx_example.png|
31+
32+
Example output:
33+
34+
None
35+
36+
RX Example
37+
^^^^^^^^^^
38+
39+
Open the |cores3_rs232_rx_example.m5f2| project in UiFlow2.
40+
41+
This example demonstrates how to receive data using the RS232 module via the UART interface.
42+
43+
UiFlow2 Code Block:
44+
45+
|rx_example.png|
46+
47+
Example output:
48+
49+
None
50+
51+
MicroPython Example
52+
-------------------
53+
54+
TX Example
55+
^^^^^^^^^^
56+
57+
This example demonstrates how to send data using the RS232 module via the UART interface.
58+
59+
MicroPython Code Block:
60+
61+
.. literalinclude:: ../../../examples/module/rs232/core_rs232_tx_example.py
62+
:language: python
63+
:linenos:
64+
65+
Example output:
66+
67+
None
68+
69+
RX Example
70+
^^^^^^^^^^
71+
72+
This example demonstrates how to receive data using the RS232 module via the UART interface.
73+
74+
MicroPython Code Block:
75+
76+
.. literalinclude:: ../../../examples/module/rs232/cores3_rs232_rx_example.py
77+
:language: python
78+
:linenos:
79+
80+
Example output:
81+
82+
None
83+
84+
**API**
85+
-------
86+
87+
AtomRS232
88+
^^^^^^^^^
89+
90+
.. autoclass:: base.rs232.AtomRS232
91+
:members:
92+
93+
.. py:method:: init(baudrate=9600, bits=8, parity=None, stop=1, *, ...)
94+
95+
Initialize the UART bus with the given parameters.
96+
97+
:param int baudrate: The clock rate for the UART communication.
98+
:param int bits: Number of bits per character (7, 8, or 9).
99+
:param int parity: Parity setting, either None, 0 (even), or 1 (odd).
100+
:param int stop: Number of stop bits (1 or 2).
101+
102+
UiFlow2 Code Block:
103+
104+
|setup.png|
105+
106+
MicroPython Code Block:
107+
108+
.. code-block:: python
109+
110+
rs232_0.init(baudrate=115200, bits=8, parity=None, stop=1)
111+
112+
.. py:method:: deinit()
113+
114+
Turn off the UART bus.
115+
116+
UiFlow2 Code Block:
117+
118+
|deinit.png|
119+
120+
MicroPython Code Block:
121+
122+
.. code-block:: python
123+
124+
rs232_0.deinit()
125+
126+
.. py:method:: any()
127+
128+
Returns the number of characters that can be read without blocking.
129+
130+
:return: The number of available bytes.
131+
:rtype: int
132+
133+
UiFlow2 Code Block:
134+
135+
|any.png|
136+
137+
MicroPython Code Block:
138+
139+
.. code-block:: python
140+
141+
rs232_0.any()
142+
143+
.. py:method:: read([nbytes])
144+
145+
Read characters from the UART buffer.
146+
147+
:param int nbytes: The maximum number of bytes to read (optional).
148+
:return: A bytes object containing the data read.
149+
:rtype: bytes
150+
151+
UiFlow2 Code Block:
152+
153+
|read_all.png|
154+
155+
|read_bytes.png|
156+
157+
|read_raw_data.png|
158+
159+
MicroPython Code Block:
160+
161+
.. code-block:: python
162+
163+
data = rs232_0.read()
164+
165+
.. py:method:: AtomRS232.readinto(buf[, nbytes])
166+
167+
Read bytes into the ``buf``. If ``nbytes`` is specified, read at most that many bytes; otherwise, read at most ``len(buf)`` bytes. It may return sooner if a timeout is reached. The timeout is configurable in the constructor.
168+
169+
:param bytearray buf: The buffer into which the bytes will be read.
170+
:param int nbytes: (Optional) The maximum number of bytes to read.
171+
172+
:return: The number of bytes read and stored into ``buf``, or ``None`` if a timeout occurs.
173+
:rtype: int or None
174+
175+
UiFlow2 Code Block:
176+
177+
|readinto.png|
178+
179+
MicroPython Code Block:
180+
181+
.. code-block:: python
182+
183+
data = bytearray(10)
184+
num_bytes = rs232_0.readinto(data)
185+
num_bytes = rs232_0.readinto(data, 5)
186+
187+
.. py:method:: AtomRS232.readline()
188+
189+
Read a line ending in a newline character. It may return sooner if a timeout is reached. The timeout is configurable in the constructor.
190+
191+
:return: The line read as bytes, or ``None`` if a timeout occurs.
192+
:rtype: bytes or None
193+
194+
UiFlow2 Code Block:
195+
196+
|readline.png|
197+
198+
MicroPython Code Block:
199+
200+
.. code-block:: python
201+
202+
line = rs232_0.readline()
203+
204+
205+
.. py:method:: write(buf)
206+
207+
Write data to the UART interface.
208+
209+
:param bytes buf: The data to be written.
210+
:return: The number of bytes written.
211+
:rtype: int
212+
213+
UiFlow2 Code Block:
214+
215+
|write.png|
216+
217+
|write1.png|
218+
219+
|write_line.png|
220+
221+
|write_list.png|
222+
223+
|write_raw_data.png|
224+
225+
|write_raw_data_list.png|
226+
227+
228+
MicroPython Code Block:
229+
230+
.. code-block:: python
231+
232+
rs232_0.write(b'Hello')
233+
234+
.. py:method:: AtomRS232.sendbreak()
235+
236+
Send a break condition on the bus. This drives the bus low for a duration longer than required for a normal transmission of a character.
237+
238+
:return: None
239+
240+
UiFlow2 Code Block:
241+
242+
|sendbreak.png|
243+
244+
MicroPython Code Block:
245+
246+
.. code-block:: python
247+
248+
rs232_0.sendbreak()
249+
250+
.. py:method:: AtomRS232.flush()
251+
252+
Waits until all data has been sent. In case of a timeout, an exception is raised. The timeout duration depends on the TX buffer size and the baud rate. Unless flow control is enabled, a timeout should not occur.
253+
254+
:return: None
255+
256+
UiFlow2 Code Block:
257+
258+
|flush.png|
259+
260+
MicroPython Code Block:
261+
262+
.. code-block:: python
263+
264+
rs232_0.flush()
265+
266+
.. py:method:: AtomRS232.txdone()
267+
268+
Check whether all data has been sent or no data transfer is happening. Returns ``True`` if no transmission is ongoing, otherwise returns ``False``.
269+
270+
:return: ``True`` if no data transfer is happening, otherwise ``False``.
271+
:rtype: bool
272+
273+
UiFlow2 Code Block:
274+
275+
|txdone.png|
276+
277+
MicroPython Code Block:
278+
279+
.. code-block:: python
280+
281+
status = rs232_0.txdone()
282+

0 commit comments

Comments
 (0)