Skip to content

Commit ddf668a

Browse files
committed
docs: Add tcp/udp docs.
Signed-off-by: lbuque <[email protected]>
1 parent 02d33c9 commit ddf668a

25 files changed

+2823
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
.. |close.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/client/close.png
3+
.. |init.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/client/init.png
4+
.. |read.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/client/read.png
5+
.. |recv.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/client/recv.png
6+
.. |send.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/client/send.png
7+
.. |setsockopt.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/client/setsockopt.png
8+
.. |write.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/client/write.png
9+
.. |readline.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/client/readline.png
10+
.. |setblocking.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/client/setblocking.png
11+
.. |settimeout.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/client/settimeout.png
12+
13+
.. |cores3_tcp_client_example.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/client/cores3_tcp_client_example.png
14+
15+
.. |cores3_tcp_client_example.m5f2| raw:: html
16+
17+
<a
18+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/softwave/tcp/cores3_tcp_client_example.m5f2"
19+
target="_blank"
20+
>
21+
cores3_tcp_client_example.m5f2
22+
</a>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
.. |close.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/server/close.png
3+
.. |close_client.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/server/close_client.png
4+
.. |accept.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/server/accept.png
5+
.. |init.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/server/init.png
6+
.. |read.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/server/read.png
7+
.. |recv.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/server/recv.png
8+
.. |recvfrom.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/server/recvfrom.png
9+
.. |send.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/server/send.png
10+
.. |sendto.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/server/sendto.png
11+
.. |setsockopt.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/server/setsockopt.png
12+
.. |write.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/server/write.png
13+
.. |readline.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/server/readline.png
14+
.. |setblocking.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/server/setblocking.png
15+
.. |settimeout.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/server/settimeout.png
16+
17+
18+
.. |cores3_tcp_server_example.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/tcp/server/cores3_tcp_server_example.png
19+
20+
.. |cores3_tcp_server_example.m5f2| raw:: html
21+
22+
<a
23+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/softwave/tcp/cores3_tcp_server_example.m5f2"
24+
target="_blank"
25+
>
26+
cores3_tcp_server_example.m5f2
27+
</a>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
.. |close.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/client/close.png
3+
.. |init.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/client/init.png
4+
.. |read.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/client/read.png
5+
.. |recv.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/client/recv.png
6+
.. |send.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/client/send.png
7+
.. |setsockopt.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/client/setsockopt.png
8+
.. |write.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/client/write.png
9+
10+
.. |cores3_udp_client_example.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/client/cores3_udp_client_example.png
11+
12+
.. |cores3_udp_client_example.m5f2| raw:: html
13+
14+
<a
15+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/softwave/udp/cores3_udp_client_example.m5f2"
16+
target="_blank"
17+
>
18+
cores3_udp_client_example.m5f2
19+
</a>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
.. |close.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/server/close.png
3+
.. |init.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/server/init.png
4+
.. |read.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/server/read.png
5+
.. |recv.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/server/recv.png
6+
.. |recvfrom.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/server/recvfrom.png
7+
.. |send.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/server/send.png
8+
.. |sendto.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/server/sendto.png
9+
.. |setsockopt.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/server/setsockopt.png
10+
.. |write.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/server/write.png
11+
12+
.. |cores3_udp_server_example.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/udp/server/cores3_udp_server_example.png
13+
14+
.. |cores3_udp_server_example.m5f2| raw:: html
15+
16+
<a
17+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/softwave/udp/cores3_udp_server_example.m5f2"
18+
target="_blank"
19+
>
20+
cores3_udp_server_example.m5f2
21+
</a>

docs/en/software/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ Software
99
modbus.rst
1010
requests2.rst
1111
umqtt.rst
12+
tcp.rst
13+
udp.rst

docs/en/software/tcp.client.rst

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
TCP Client
2+
==========
3+
4+
.. include:: ../refs/software.tcp.client.ref
5+
6+
UiFlow2 Example
7+
---------------
8+
9+
simple client
10+
^^^^^^^^^^^^^
11+
12+
Open the |cores3_tcp_client_example.m5f2| project in UiFlow2.
13+
14+
This example creates a TCP client that sends data to a server.
15+
16+
UiFlow2 Code Block:
17+
18+
|cores3_tcp_client_example.png|
19+
20+
Example output:
21+
22+
None
23+
24+
MicroPython Example
25+
-------------------
26+
27+
simple client
28+
^^^^^^^^^^^^^
29+
30+
This example creates a TCP client that sends data to a server.
31+
32+
MicroPython Code Block:
33+
34+
.. literalinclude:: ../../../examples/softwave/tcp/cores3_tcp_client_example.py
35+
:language: python
36+
:linenos:
37+
38+
Example output:
39+
40+
None
41+
42+
43+
**API**
44+
-------
45+
46+
.. method:: socket.socket()
47+
socket.connect(address)
48+
:no-index:
49+
50+
Create a socket object and connect to the server.
51+
52+
:param tuple address: A tuple containing the server IP address and port number.
53+
:return: A socket object.
54+
:rtype: socket.socket
55+
56+
UiFlow2 Code Block:
57+
58+
|init.png|
59+
60+
MicroPython Code Block:
61+
62+
.. code-block:: python
63+
64+
import socket
65+
66+
tcpc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
67+
tcpc.connect(('192.168.8.236', 8001))
68+
69+
70+
.. method:: socket.close()
71+
:no-index:
72+
73+
Close the socket connection.
74+
75+
UiFlow2 Code Block:
76+
77+
|close.png|
78+
79+
MicroPython Code Block:
80+
81+
.. code-block:: python
82+
83+
tcpc.close()
84+
85+
.. method:: socket.recv(bufsize)
86+
:no-index:
87+
88+
Receive data from the socket.
89+
90+
:param int bufsize: The maximum amount of data to be received at once.
91+
:return: The received data.
92+
:rtype: bytes
93+
94+
UiFlow2 Code Block:
95+
96+
|recv.png|
97+
98+
MicroPython Code Block:
99+
100+
.. code-block:: python
101+
102+
data = tcpc.recv(1024)
103+
print(data)
104+
105+
106+
.. method:: socket.read()
107+
:no-index:
108+
109+
Read data from the socket.
110+
111+
:return: The received data.
112+
:rtype: bytes
113+
114+
UiFlow2 Code Block:
115+
116+
|read.png|
117+
118+
MicroPython Code Block:
119+
120+
.. code-block:: python
121+
122+
data = tcpc.read()
123+
print(data)
124+
125+
.. method:: socket.readline()
126+
:no-index:
127+
128+
Read a line, ending in a newline character.
129+
130+
:return: the line read.
131+
:rtype: str
132+
133+
UiFlow2 Code Block:
134+
135+
|readline.png|
136+
137+
MicroPython Code Block:
138+
139+
.. code-block:: python
140+
141+
data = tcpc.readline()
142+
print(data)
143+
144+
145+
.. method:: socket.send(data)
146+
:no-index:
147+
148+
Send data to the socket. The socket must be connected to a remote socket.
149+
150+
:param data: The data to be sent.
151+
:type data: bytes | str
152+
:return: The number of bytes sent.
153+
:rtype: int
154+
155+
UiFlow2 Code Block:
156+
157+
|send.png|
158+
159+
MicroPython Code Block:
160+
161+
.. code-block:: python
162+
163+
tcpc.send(b'Hello, World!')
164+
tcpc.send('Hello, World!')
165+
166+
167+
.. method:: socket.write(data)
168+
:no-index:
169+
170+
Write the buffer of bytes to the socket. This function will try to write all
171+
data to a socket (no “short writes”). This may be not possible with a
172+
non-blocking socket though, and returned value will be less than the length
173+
of buf.
174+
175+
:param data: The data to be sent.
176+
:type data: bytes | str
177+
:return: The number of bytes sent.
178+
:rtype: int
179+
180+
UiFlow2 Code Block:
181+
182+
|write.png|
183+
184+
MicroPython Code Block:
185+
186+
.. code-block:: python
187+
188+
tcpc.write(b'Hello, World!')
189+
tcpc.write('Hello, World!')
190+
191+
192+
.. method:: socket.setblocking(flag)
193+
:no-index:
194+
195+
Set blocking or non-blocking mode of the socket: if flag is false, the
196+
socket is set to non-blocking, else to blocking mode.
197+
198+
This method is a shorthand for certain `settimeout()` calls:
199+
200+
* ``sock.setblocking(True)`` is equivalent to ``sock.settimeout(None)``
201+
* ``sock.setblocking(False)`` is equivalent to ``sock.settimeout(0)``
202+
203+
:param bool flag: If True, the socket will be in blocking mode. If False, the socket will be in non-blocking mode.
204+
:return: None
205+
:rtype: None
206+
207+
UiFlow2 Code Block:
208+
209+
|setblocking.png|
210+
211+
MicroPython Code Block:
212+
213+
.. code-block:: python
214+
215+
tcpc.setblocking(True)
216+
tcpc.setblocking(False)
217+
218+
219+
.. method:: socket.settimeout(timeout)
220+
:no-index:
221+
222+
Set a timeout on blocking socket operations. The value argument can be a
223+
nonnegative floating point number expressing seconds, or None. If a
224+
non-zero value is given, subsequent socket operations will raise an OSError
225+
exception if the timeout period value has elapsed before the operation has
226+
completed. If zero is given, the socket is put in non-blocking mode. If None
227+
is given, the socket is put in blocking mode.
228+
229+
:param float timeout: The timeout value in seconds. If None, the socket is put in blocking mode.
230+
:return: None
231+
:rtype: None
232+
233+
UiFlow2 Code Block:
234+
235+
|settimeout.png|
236+
237+
MicroPython Code Block:
238+
239+
.. code-block:: python
240+
241+
tcpc.settimeout(5)
242+
tcpc.settimeout(None)

docs/en/software/tcp.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
TCP
2+
===
3+
4+
TCP is a connection-oriented protocol, meaning that a connection must be
5+
established before data can be sent. It uses a three-way handshake to establish
6+
a connection, which involves the exchange of SYN (synchronize) and ACK
7+
(acknowledge) packets between the client and server. Once the connection is
8+
established, data can be sent in both directions until either party decides to
9+
close the connection.
10+
11+
12+
API
13+
---
14+
15+
.. toctree::
16+
:maxdepth: 1
17+
18+
tcp.client.rst
19+
tcp.server.rst

0 commit comments

Comments
 (0)