STM32 ETHERNET #5. TCP CLIENT

Started by tha, March 03, 2022, 08:41:36 AM

Previous topic - Next topic

tha

https://controllerstech.com/stm32-ethernet-5-tcp-client/

STM32 ETHERNET #5. TCP CLIENT

นี่คือบทช่วยสอนที่ 5 ในซีรีส์ STM32 ETHERNET และวันนี้เราจะมาดูวิธีใช้ STM32 ของเราในฐานะ TCP Client กัน ฉันอยากจะแนะนำให้คุณอ่านบทช่วยสอนก่อนหน้านี้ก่อน เพราะฉันจะไม่อธิบายทุกอย่างที่นี่ เช่น การเชื่อมต่อ
ไปที่ลิงก์ด้านบนและดูวิธี the connection ถูกทำใน the first tutorial. ดู the TCP server tutorial ด้วย, เพื่อให้เข้าใจสิ่งนี้ได้ง่ายขึ้นเล็กน้อย.

tha

                                                           Some Insight into the CODE

Initializing the TCP Client



     •  ก่อนอื่นเราจำเป็นต้องสร้าง TCP Block โดยใช้ฟังก์ชัน tcp_new ()

     •  ถัดมาเราจะเชื่อมต่อถึง the server ที่มี the IP 192.168.0.102 and Port 31

เมื่อ the client ถูกเชื่อมต่อถึง the server แล้ว, the client connected callback จะถูกเรียก. ข้างล่างคือ the code ที่เหมือนกัน.

tha

Client Connected Callback

callback นี้ถูกเรียก, ทันที่ที่การเชื่อมต่อระหว่าง the server และ the client สำเร็จ.




     •  ดังที่คุณเห็นด้านบน, callback จะเริ่มต้น callbacks อื่นๆสองสามรายการ.

     •  ตัวอย่างเช่น, the tcp_poll callback, tcp_sent callback, tcp_recv callback เป็นต้น.

     •  สิ่งเหล่านี้จำเป็นสำหรับการประมวลผล และคุณไม่ควรแก้ไขพวกมัน

     •  ที่นี่เราสนใจเฉพาะ the tcp_recv callback เท่านั้น นี่คือที่ที่เราจะเขียนโค้ดของเราเพื่อจัดการกับ the incoming data.


tha

Client Receive Callback

tcp_recv เริ่มต้น the tcp_client_recv function. โดยพื้นฐานแล้ว a callback, ซึ่งถูกเรียกเมื่อไรก็ตามที่ the client รับบาง data จาก the server.

เราจะเขียน code ของเราเพื่อจัดการ received data นี้.



     •  The dots ใน the code ข้างบนแสดงบาง predefined code เพื่อจัดการ the errors. คุณควรปล่อยมันไว้ตามเดิม.

     •  ถ้า the state ถูกสลับเป็น connected, เราสามารถประมวลผล the received data.

     •  ที่นี่ tpcb เก็บ the info ทั้งหมดเกี่ยวกับ the server and client, และ pbuf เก็บ the info ทั้งหมดเกี่ยวกับ the data.

     •  ที่นี่ ฉันกำลังเก็บ the reference ถึง the incoming buffer ลงใน the "es" structure, ซึ่งต่อมาถูกส่งผ่านไปยัง client
         handle, เพื่อให้เราสามารถใช้ประโยชน์ของ incoming data นี้ได้.

     •  จากนั้นเราต้องรับทราบ(Acknowledge) the received data, และเราดำเนินการโดยการใช้ the function tcp_recved

     •  ตอนนี้ tcp_client_hanldle ถูกเรียกเพื่อจัดการ the data ที่รับจาก the server.

     •  เราสามารถจัดการ the data ได้ที่นี่เช่นกัน แต่ฉันพยายามรักษาความคล้ายคลึงในการ code ระหว่างโปรโตคอลต่างๆ และนี่คือเหตุผลที่
         ฉันได้สร้างฟังก์ชันอื่นขึ้นมาเพียงแค่เพื่อวัตถุประสงค์ในการจัดการข้อมูลเท่านั้น

     •  หลังจาก the data ถูกประมวลผลแล้ว, เราจะปล่อยฟรี the buffer โดยใช้ pbuf_free

tha

Client data Handler

tcp_client_handle จัดการ the incoming data จาก the server.



     •  นี่คือฟังก์ชัน ซึ่งคุณสามารถเขียน the code ได้ตามความต้องการของคุณ

     •  ฉันไม่ได้ทำอะไรเป็นพิเศษ แต่แค่รับ IP ของเซิร์ฟเวอร์และเพิ่มตัวแปรตัวนับ

     •  counter variable จะถูกใช้ภายหลังในขณะที่ส่ง the data ไปยัง the server.

     •  ถ้าคุณต้องการใช้ the incoming data, the information เกี่ยวกับ the data สามารถถูกพบใน the "es" structure, และ the
         info เกี่ยวกับ the server and client สามารถถูกพบใน the "tpcb" structure.

tha

Sending the data to the server

ก็เหมือนกันกับ the UDP client, ที่สี่ฉันได้สร้าง a timer callback ด้วย, ซึ่งจะถูกเรียกทุกๆวินาที.

ภายใน the callback, เราจะส่ง the data ไปยัง the server.



     •  ที่นี่คุณสามารถเห็นด้านบนฉันกำลังรวม the counter value กับ a string.

     •  จากนั้น pbuf_alloc จะจัดสรร the memory สำหรับ the pbuf ที่เราจะส่ง

     •  pbuf_take จะคัดลอก the string ลงใน the payload ของ the pbuf ที่เรากำลังส่ง

     •  tcp_client_send จะส่ง the pbuf ไปยัง the server

     •  และสุดท้ายเราจะปล่อยฟรี the memory โดยใช้ pbuf_free

     •  ดังนั้น the counter value จะเพิ่มขึ้นเท่านั้น, ถ้า the client handle function ถูกเรียก, ซึ่งจะเกิดขึ้นเฉพาะเมื่อ the client รับ
         บาง data จาก the server

     •  มิฉะนั้น the client จะส่ง the same counter value ไปยัง the server ทุกๆวินาที.

tha

                                                                   RESULT



     •  ดังแสดงในรูปภาพข้างบน, the client กำลังส่งแล้ว the old counter value.

     •  ทันทีที่ the server ส่งบาง data, the counter จะเพิ่มขึ้น, และ the client สตาร์ทการส่ง new counter value ทุกๆ second