STM32 ETHERNET #3. UDP CLIENT

  • 5 Replies
  • 504 Views
*

Offline tha

  • *****
  • 5007
    • View Profile
STM32 ETHERNET #3. UDP CLIENT
« on: February 24, 2022, 06:21:11 am »
https://controllerstech.com/stm32-ethernet-3-udp-client/

STM32 ETHERNET #3. UDP CLIENT

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

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

*

Offline tha

  • *****
  • 5007
    • View Profile
Re: STM32 ETHERNET #3. UDP CLIENT
« Reply #1 on: February 25, 2022, 06:51:10 am »
                                                 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.

*

Offline tha

  • *****
  • 5007
    • View Profile
Re: STM32 ETHERNET #3. UDP CLIENT
« Reply #2 on: February 26, 2022, 05:54:56 am »
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.

*

Offline tha

  • *****
  • 5007
    • View Profile
Re: STM32 ETHERNET #3. UDP CLIENT
« Reply #3 on: February 26, 2022, 07:17:14 am »
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.

*

Offline tha

  • *****
  • 5007
    • View Profile
Re: STM32 ETHERNET #3. UDP CLIENT
« Reply #4 on: February 14, 2023, 06:13:56 am »
Result


*

Offline tha

  • *****
  • 5007
    • View Profile
Re: STM32 ETHERNET #3. UDP CLIENT
« Reply #5 on: February 14, 2023, 06:29:09 am »
<a href="https://www.youtube.com/v/Kc7OHc7JfRg" target="_blank" class="new_win">https://www.youtube.com/v/Kc7OHc7JfRg</a>