STM32 ETHERNET #5. TCP CLIENT

  • 7 Replies
  • 636 Views
*

Offline tha

  • *****
  • 4992
    • View Profile
STM32 ETHERNET #5. TCP CLIENT
« on: March 03, 2022, 08:41:36 am »
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 ด้วย, เพื่อให้เข้าใจสิ่งนี้ได้ง่ายขึ้นเล็กน้อย.

*

Offline tha

  • *****
  • 4992
    • View Profile
Re: STM32 ETHERNET #5. TCP CLIENT
« Reply #1 on: March 03, 2022, 08:55:08 am »
                                                           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 ที่เหมือนกัน.

*

Offline tha

  • *****
  • 4992
    • View Profile
Re: STM32 ETHERNET #5. TCP CLIENT
« Reply #2 on: March 04, 2022, 08:49:38 am »
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.

*

Offline tha

  • *****
  • 4992
    • View Profile
Re: STM32 ETHERNET #5. TCP CLIENT
« Reply #3 on: March 05, 2022, 08:42:49 am »
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

*

Offline tha

  • *****
  • 4992
    • View Profile
Re: STM32 ETHERNET #5. TCP CLIENT
« Reply #4 on: March 06, 2022, 08:54:16 am »
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.

*

Offline tha

  • *****
  • 4992
    • View Profile
Re: STM32 ETHERNET #5. TCP CLIENT
« Reply #5 on: March 07, 2022, 07:16:48 am »
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 ทุกๆวินาที.

*

Offline tha

  • *****
  • 4992
    • View Profile
Re: STM32 ETHERNET #5. TCP CLIENT
« Reply #6 on: March 07, 2022, 07:26:59 am »
                                                                   RESULT



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

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

*

Offline tha

  • *****
  • 4992
    • View Profile
Re: STM32 ETHERNET #5. TCP CLIENT
« Reply #7 on: February 15, 2023, 10:24:54 am »
<a href="https://www.youtube.com/v/_AIZ2CuZKJg" target="_blank" class="new_win">https://www.youtube.com/v/_AIZ2CuZKJg</a>