STM32 ETHERNET #3. UDP CLIENT

Started by tha, February 24, 2022, 06:21:11 AM

Previous topic - Next topic

tha

https://controllerstech.com/stm32-ethernet-3-udp-client/

STM32 ETHERNET #3. UDP CLIENT

นี่เป็นบทช่วยสอนที่สามในซีรีส์ STM32 Ethernet และวันนี้เราจะมาดูวิธีสร้าง UDP client โดยใช้ STM32 ฉันได้กล่าวถึงบทช่วยสอนเกี่ยวกับ UDP SERVER แล้ว คุณสามารถดูได้ที่นี่

The hardware connection จะคล้ายกับที่ฉันเคยใช้ในวิดีโอแรก และคุณสามารถตรวจสอบได้ที่นี่.

tha

                                                 Some Insight into the CODE

Connect the Client to the server



     •  เราจะใช้ขั้นตอนต่อไปนี้เพื่อเชื่อมต่อ the UDP client ถึง a server

     •  ก่อนอื่นเราจะสร้าง a new UDP control block, โดยใช้ udp_new ()

     •  ต่อไปฉันกำลังผูก the control block กับ the local IP and port, โดยใช้ udp_bind ()

     •  เชื่อมต่อ the control block เข้ากับ the server IP and port, โดยใช้ udp_connect() ด้วย

     •  จากนั้นเราจะส่งบาง data ไปยัง the server, และเซ็ต a receive function, ซึ่งจะถูกเรียก, เมื่อ the server ส่งบาง data ไปยัง
         the client.

tha

The Receive callback



The receive callback ถูกเรียก, เมื่อ the client รับบาง data จาก the server. มาดูกัน วิธีที่ฉันจัดการฟังชั่นนี้

     •  ที่นี่ฉันกำลังคัดลอก the data ที่ส่งโดย the server ลงใน the buffer.

     •  แม้ว่าฉันจะไม่ได้ใช้ data นี้ แต่คุณสามารถใช้มันได้ตามความต้องการของคุณ

     •  แล้วเพิ่ม  the counter. counter นี้จะถูกใช้เมื่อเราจะส่ง the data

     •  สุดท้ายปล่อยฟรี the packet buffer.


tha

Sending Data to the Server



     •  ที่นี่ฉันได้สร้าง a periodic timer อีกด้วย, ซึ่งจะทไให้เกิด an interrupt ทุกๆ 1 second.

     •  The data จะส่งถึง the server ใน the timer callback, และดังนั้นทุกๆ 1 second.

     •  เพื่อส่ง the data ไปยัง the server, ก่อนอื่นเราจะสร้าง a packet buffer, txBuf

     •  จากนั้นเราจะจัดสรร the memory สำหรับ packet buffer นี้

     •  pbuf_take จะถูกใช้เพื่อคัดลอก the data ลงใน the packet buffer.

     •  และสุดท้ายเราจะส่ง the data โดยใช้ the udp_send function.

tha