STM32 ETHERNET #4. TCP SERVER

Started by tha, February 28, 2022, 06:48:52 AM

Previous topic - Next topic

tha

https://controllerstech.com/stm32-ethernet-4-tcp-server/

STM32 ETHERNET #4. TCP SERVER

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

แก้ไขข้อผิดพลาด

ตอนนี้โค้ดได้รับการแก้ไขแล้ว และคุณสามารถรับอักขระได้มากกว่า 5 ตัวโดยไม่มีข้อมูลถูกทำให้เสียหาย

tha

                                                                 CubeMX Setup

The cubeMX Setup จะยังคงเหมือนเดิมกับบทช่วยสอนก่อนหน้านี้ ฉันจะทิ้งรูปภาพบางส่วนไว้ด้านล่างเพื่อให้คุณอ้างอิง สำหรับการตั้งค่าโดยละเอียด โปรดดูที่บทช่วยสอนเกี่ยวกับ the Connection tutorial



tha

                                               Some Insight into the CODE

ข้างล่างนี้เป็นขั้นตอนที่ให้เพื่อจัดให้มีใช้ the TCP Server ด้วย STM32



/* การจัดให้มีใช้สำหรับ the TCP Server
   1. สร้าง TCP Block.
   2. ผูก the Block กับ server address, และ port.
   3. ฟังสำหรับ the  incoming requests โดย the client
   4. ยอมรับ the Request, และตอนนี้ the server พร้อมสำหรับ the data transfer
*/


tha

Initialization of the TCP server



     •  ที่นี่ก่อนอื่น, เราจะสร้าง a new TCP Control block. tpcb = tcp_new();

     •  จากนั้นเราจะผูก the Block เข้ากับ the local IP Address and Port. นี้จะเป็น the server IP and Port.       
         tcp_bind(tpcb, &myIPADDR, 7)

     •  ขั้นตอนถัดไปคือฟังสำหรับ the incoming traffic. tpcb = tcp_listen(tpcb);

     •  และสุดท้ายเราจะยอมรับการร้องขอจาก the client. tcp_accept(tpcb, tcp_server_accept);

tha

tcp_server_accept

เริ่มต้นบางสิ่งเพิ่มเติมเล็กน้อย โค้ดสำหรับมันถูกแสดงด้านล่าง




คุณสามารถเห็นข้างบนว่ามันเริ่มต้น the callbacks อย่าง tcp_arg, tcp_recv and tcp_poll. แต่ที่นี่จะสนใจใน tcp_recv เท่านั้น.

tha

tcp_server_recv

tcp_recv เริ่มต้น the callback tcp_server_recv, ซึ่งจะถูกเรียกเมื่อใดก็ตามที่ the server รับบาง data จาก the client. The function ถูกแสดงข้างล่าง





มีบางสิ่งที่กำหนดไว้ล่วงหน้าและเราจะปล่อยให้มันเป็นไปตามที่มันเป็น เราจะเน้นที่บรรทัดที่ 42 เป็นต้นไป

     •  เมื่อ the data ถูกรับเป็นครั้งแรก the state จะถูกเซ็ตเป็นยอมรับ

     •  นี้คือเพื่อให้แน่ใจว่า the tcp_sent callback ถูกเริ่มต้น และควรเริ่มต้นเพียงครั้งเดียว

     •  หลังจากนี้, the state จะถูกเปลี่ยนเป็น RECEIVED, และ the code ที่บรรทัดเลขที่ 58 จะปฏิบัติ.

     •  ที่นี่เราจะเก็บ the incoming data ลงใน our buffer (line 63), และเรียก the server handle function (line 66) เพื่อจัดการ
         the incoming data.

     •  ฉันได้สร้าง server handle function นี้ขึ้น, เพื่อให้คุณสามารถประมวลผล the incoming data ได้ดีขึ้น. และคุณควรปล่อย the
         received callback ไว้เหมือนเดิม.

tha

Server Handle



     •  ที่นี่เราจะได้ the IP address and the port สำหรับ the client

     •  The IP address อยู่ใน the integer format, และเราสามารถใช้ the function ipaddr_ntoa เพื่อแปลง the address ใน the
         proper format

     •  The incoming data จาก the client ถูกเก็บใน the payload ของ the p buf

     •  ฉันกำลังผสม the incoming data กับข้อมูลใหม่, และเก็บมันใน the buffer.

     •  จากนั้นเราจะอัฟเดต the p buf ด้วย the new payload, total length, and the length ของ the buffer ที่จะส่ง.

     •  สุดท้ายเราจะส่ง the p buf ไปยัง the client โดยใช้ tcp_server_send function.

tha

                                                        RESULT



     •  ที่นี่ผมได้ใช้ the Hercules เป็น the TCP Client

     •  The Pink color คือ the data ที่ส่งโดย the client, และในการตอบสนอง, the Black color data ถูกส่งโดย the Server