STM32 ETHENRET #2. UDP SERVER

Started by tha, February 21, 2022, 05:38:00 AM

Previous topic - Next topic

tha

https://controllerstech.com/stm32-ethenret-2-udp-server/

STM32 ETHENRET #2. UDP SERVER

นี่เป็นบทช่วยสอนที่สองใน STM32 ETHERNET Series และวันนี้เราจะมาดูวิธีสร้างเซิร์ฟเวอร์ UDP โดยใช้ STM32

UDP เป็นโปรโตคอลที่ง่ายที่สุด และนี่คือเหตุผลที่ฉันเริ่มต้นกับมัน

บทช่วยสอนนี้จะครอบคลุมเฉพาะ the UDP SERVER mode, ส่วนถัดไปจะครอบคลุม UDP Client mode, และในทำนองเดียวกัน เราจะย้ายไปยัง TCP และหลังจากนั้นกับ HTTP

tha

                                                   Some Insight into the CODE

The cube MX Setup เหมือนกับในบทช่วยสอนก่อนหน้า ตอนนี้ มาดูโค้ดกัน.

Initialize the UDP SERVER



ในการเริ่มต้น the UDP Server, มีการดำเนินการตามขั้นตอนต่อไปนี้

•  สร้าง a new UDP control block โดยใช้ udp_new.

•  เชื่อมโยง the block กับ the local IP address และ Port

•  ในการทำเช่นนี้ ก่อนอื่นเราจะแปลง the IP address ไปเป็น the integer form

•  จากนั้นเราจะใช้ the local IP address เพื่อเชื่อมโยง the control block โดยใช้ the function udp_bind.

•  เมื่อการเชื่อมโยงเสร็จสมบูรณ์ the server จะรอให้ the client ส่ง the data.

•  ในการทำเช่นนี้ เราสามารถเซ็ต a callback (udp_recv), ซึ่งจะถูกเรียกเมื่อใดก็ตามที่ the server ได้รับ the data จาก the client.

•  เมื่อ the data ถูกรับ, เราสามารถประมวลผล the data, และส่งการตอบกลับบางประการไปยัง the client.

tha

The Receive Callback



The receive callback ถูกเรียก, เมื่อ the server รับบาง data จาก the client. ต่อไปนี้แสดงวิธีจัดการ data นี้.

     1.  ก่อนอื่นผมกำลังสร้าง a new Packet Buffer (txBuf), ซึ่งจะถูกใช้ส่ง the data.

     2.  char *remoteIP = ipaddr_ntoa(addr); แปลง the IP address จาก integer format เป็น the regular IP format.

     3.  นี่เป็นเพียงกรณีที่เราต้องการใช้ the address and port ของ the client.

     4.  ต่อไปฉันกำลังผสม the incoming data กับ some additional data.

     5.  เพื่อส่ง data ใหม่นี้ไปยัง the client, สิ่งแรกเราจำเป็นต้องจัดสรรบาง memory สำหรับ the packet buffer.

     6.  นี้สามารถถูกทำด้วย pbuf_alloc.

     7.  จากนั้นเราจะคัดลอก the data ลงใน the packet buffer โดยใช้ pbuf_take.

     8.  ถัดไปเราจะเชื่อมต่อถึง the client.

     9.  นี้สามารถถูกทำโดยการใช้ udp_connect. The parameters, address and port ของ the client, เป็นส่วนหนึ่งของ the
          function, เมื่อ udp_connect กำลังถูกเรียก.

     10.  จากนั้นเราส่ง the data โดยใช้ the udp_send(upcb, txBuf);

     11.  ที่นี่ upcb คือ the control block ซึ่งมี the information ทั้งหมดเกี่ยวกับ the local and remote connection.

     12.  txBuf คือ the packet buffer, และมันมี the information ทั้งหมดเกี่ยวกับ  the data.

     13.  เมื่อ the data ถูกส่งแล้ว, เราจะยกเลิกการเชื่อมต่อ the client, ดังนั้น a new client สามารถเชื่อมต่อได้.

     14.  และในท้ายสุดเราจะปล่อยฟรี the memories ทั้งหมด.


tha

The main code




     •  ที่นี่ทุกอย่างคล้ายกับบทช่วยสอนก่อนหน้า ยกเว้นว่าเราจะเรียกใช้ฟังก์ชัน uspServer_init() ด้วย

     •  โดยทั่วไป ethernetif_input จะจัดการคำขอที่เข้ามาทั้งหมด.

     •  มันเรียก the appropriate low level function, ขึ้นอยู่กับว่า protocol อะไรที่กำลังถูกใช้.

     •  ดังนั้นเราจึงสามารถใช้ code เดียวกันใน TCP ได้เช่นกัน

tha

                                                                Result



คุณสามารถดูข้างบน, the data ถูกส่งและถูกรับโดย the client.