Skip to content

Commit 15d76ee

Browse files
TinyuZhaolbuque
authored andcommitted
software: Add easysocket udp server and client support.
Signed-off-by: tinyu <tinyu.zhao@gmail.com>
1 parent 96dfa6c commit 15d76ee

22 files changed

Lines changed: 1492 additions & 725 deletions
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
.. |check_event.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/client/check_event.png
3+
.. |close.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/client/close.png
4+
.. |udp_client_core2_example.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/client/example.png
5+
.. |getpeername.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/client/getpeername.png
6+
.. |getsockname.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/client/getsockname.png
7+
.. |init.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/client/init.png
8+
.. |received_callback.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/client/received_callback.png
9+
.. |send.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/client/send.png
10+
.. |sendto.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/client/sendto.png
11+
.. |sendto2.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/client/sendto2.png
12+
13+
.. |udp_client_core2_example.m5f2| raw:: html
14+
15+
<a
16+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/software/easysocket/udp_client/udp_client_core2_example.m5f2"
17+
target="_blank"
18+
>
19+
udp_client_core2_example.m5f2
20+
</a>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
.. |check_event.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/server/check_event.png
3+
.. |close.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/server/close.png
4+
.. |udp_server_core2_example.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/server/example.png
5+
.. |getpeername.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/server/getpeername.png
6+
.. |getsockname.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/server/getsockname.png
7+
.. |init.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/server/init.png
8+
.. |received_callback.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/server/received_callback.png
9+
.. |send.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/server/send.png
10+
.. |sendto.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/server/sendto.png
11+
.. |sendto2.png| image:: https://static-cdn.m5stack.com/mpy_docs/software/easy_udp/server/sendto2.png
12+
13+
.. |udp_server_core2_example.m5f2| raw:: html
14+
15+
<a
16+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/software/easysocket/udp_server/udp_server_core2_example.m5f2"
17+
target="_blank"
18+
>
19+
udp_server_core2_example.m5f2
20+
</a>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ Classes
2929

3030
tcp.client.rst
3131
tcp.server.rst
32+
udp.client.rst
33+
udp.server.rst

docs/en/software/index.rst

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

docs/en/software/udp.client.rst

Lines changed: 13 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
UDP Client
2-
==========
1+
EasyUDPClient
2+
=============
33

4-
.. include:: ../refs/software.udp.client.ref
4+
.. include:: ../refs/software.easysocket.udp.client.ref
5+
6+
EasyUDPClient provides a simple way to create UDP clients in an event-driven manner.
57

68
UiFlow2 Example
79
---------------
810

911
simple client
1012
^^^^^^^^^^^^^
1113

12-
Open the |cores3_udp_client_example.m5f2| project in UiFlow2.
14+
Open the |udp_client_core2_example.m5f2| project in UiFlow2.
1315

14-
This example creates a UDP client that sends data to a server.
16+
This example creates a UDP client that connects to a server and sends data.
1517

1618
UiFlow2 Code Block:
1719

18-
|cores3_udp_client_example.png|
20+
|udp_client_core2_example.png|
1921

2022
Example output:
2123

@@ -24,14 +26,14 @@ Example output:
2426
MicroPython Example
2527
-------------------
2628

27-
sample client
29+
simple client
2830
^^^^^^^^^^^^^
2931

30-
This example creates a UDP client that sends data to a server.
32+
This example creates a UDP client that connects to a server and sends data.
3133

3234
MicroPython Code Block:
3335

34-
.. literalinclude:: ../../../examples/software/udp/cores3_udp_client_example.py
36+
.. literalinclude:: ../../../examples/software/easysocket/udp_client/udp_client_core2_example.py
3537
:language: python
3638
:linenos:
3739

@@ -43,152 +45,5 @@ Example output:
4345
**API**
4446
-------
4547

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-
udpc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
67-
udpc.connect(('192.168.8.236', 8000))
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-
udpc.close()
84-
85-
86-
.. method:: socket.recv(bufsize)
87-
:no-index:
88-
89-
Receive data from the socket.
90-
91-
:param int bufsize: The maximum amount of data to be received at once.
92-
:return: The received data.
93-
:rtype: bytes
94-
95-
UiFlow2 Code Block:
96-
97-
|recv.png|
98-
99-
MicroPython Code Block:
100-
101-
.. code-block:: python
102-
103-
data = udpc.recv(1024)
104-
print(data)
105-
106-
107-
.. method:: socket.read()
108-
:no-index:
109-
110-
Read data from the socket.
111-
112-
:return: The received data.
113-
:rtype: bytes
114-
115-
UiFlow2 Code Block:
116-
117-
|read.png|
118-
119-
MicroPython Code Block:
120-
121-
.. code-block:: python
122-
123-
data = udpc.read()
124-
print(data)
125-
126-
127-
.. method:: socket.send(data)
128-
:no-index:
129-
130-
Send data to the socket. The socket must be connected to a remote socket.
131-
132-
:param data: The data to be sent.
133-
:type data: bytes | str
134-
:return: The number of bytes sent.
135-
:rtype: int
136-
137-
UiFlow2 Code Block:
138-
139-
|send.png|
140-
141-
MicroPython Code Block:
142-
143-
.. code-block:: python
144-
145-
udpc.send(b'Hello, World!')
146-
udpc.send('Hello, World!')
147-
148-
149-
.. method:: socket.write(data)
150-
:no-index:
151-
152-
Write the buffer of bytes to the socket. This function will try to write all
153-
data to a socket (no “short writes”). This may be not possible with a
154-
non-blocking socket though, and returned value will be less than the length
155-
of buf.
156-
157-
:param data: The data to be sent.
158-
:type data: bytes | str
159-
:return: The number of bytes sent.
160-
:rtype: int
161-
162-
UiFlow2 Code Block:
163-
164-
|write.png|
165-
166-
MicroPython Code Block:
167-
168-
.. code-block:: python
169-
170-
udpc.write(b'Hello, World!')
171-
udpc.write('Hello, World!')
172-
173-
174-
.. method:: socket.setsockopt(level, optname, value)
175-
:no-index:
176-
177-
Set the value of the given socket option. The needed symbolic constants are
178-
defined in the socket module (SO_* etc.). The value can be an integer or a
179-
bytes-like object representing a buffer.
180-
181-
:param int level: The level at which the option is defined.
182-
:param int optname: The name of the option to set.
183-
:param value: The value to set for the option.
184-
185-
UiFlow2 Code Block:
186-
187-
|setsockopt.png|
188-
189-
MicroPython Code Block:
190-
191-
.. code-block:: python
192-
193-
udpc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 0)
194-
udpc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
48+
.. autoclass:: software.easysocket.udp_client.EasyUDPClient
49+
:members:

docs/en/software/udp.rst

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)