STM32 ETHERNET #8 UDP Client using LWIP NETCONN (RTOS)

Started by tha, February 25, 2023, 07:25:17 AM

Previous topic - Next topic

tha

The udpinit_thread:




•   ที่นี่ก่อนอื่นเราจะสร้าง a new netconn connection. NETCONN_UDP argument จะสร้าง a UDP connection.
•   ถัดไปเราจะผูก the connection เข้ากับ available IP address ใดๆ(อันที่คุณตั้งค่าใน the cubeMX)และ the Port 7. นี้จะทำหน้าที่
     เป็น the IP and port ของ the Client.
•   ถ้า the bind สำเร็จ, เราจะต่อถึง the server. ที่นี่ the destination IP คือ the IP of the server (computer), และ the port
     คือ port of the server.
•   จากนั้นเราจะรับ the data จาก the server. The netconn_recv function จะรอจนกว่า the data ถูกรับ.
•   เนื่องจากนี่เป็นเพียงบทแนะนำการทดสอบเท่านั้น เราจะแก้ไข message นี้, และจัดเก็บมันไว้ใน smgs เพื่อให้เราสามารถส่งมันกลับไปยัง
     server.
•   ถัดไปเราจะเรียก the udpsend function เพื่อส่ง the data กลับไปยัง the server.
•   สุดท้ายลบ the netbuf

tha

The udpsend_thread:



The udpsend_thread จะส่ง the data ทุกๆ 500ms, โดยไม่คำนึงว่า the server จะส่งการตอบกลับใดๆ หรือไม่.
•   ที่นี่ฉันเก็บค่าของ the index variable ใน smsg array.
•   จากนั้นส่ง data ไยัง the server โดยใช้ the function udpsend.
•   ขบวนการนี้ทำซ้ำทุกๆ 500ms.

tha

The udpsend function:



The udpsend function จะส่ง the data ไปยัง the server.
•   ที่นี่ก่อนอื่นเราจะจัดสรร a netbuf structure, ซึ่ง pointer จะถูกเก็บไว้ใน buf.
•   เพื่อก๊อปปี้ the data ลงใน the pbuf of the netbuf, เราใช้ the nebuf_ref เพื่อเชื่อมโยง the data pointer กับ a netbuf.
•   netcon_send จะส่ง the buf ปยัง the connected destination (server ในกรณีนี้)
•   สุดท้ายยกเลิกการจัดสรร the netbuf structure.


tha

                                                                              Result

ข้างล่างคือ the screenshot ของ the data ที่ส่งโดย the client และ the response ที่ส่งโดย the server.



•   ดังที่แสดงไว้ด้านบน, the client จะคงการส่งค่าของ the index variable ทุกๆ 500ms.
•   เมื่อใดก็ตามที่ the server ส่ง some data ไปยง client, มันจะแก้ไข the data และส่งมันกลับไปยัง the client.
•   และอีกครั้งมันสตาร์ทการส่งค่าที่อัฟเดตของ the index variable.