前言
????????上一章我們用W5500-EVB-PICO開(kāi)發(fā)板通過(guò)DNS解析www.baidu.com(百度域名)成功得到其IP地址,那么本章我們將用我們的開(kāi)發(fā)板作為客戶端去連接服務(wù)器,并做數(shù)據(jù)回環(huán)測(cè)試:收到服務(wù)器發(fā)送的數(shù)據(jù),并回傳給服務(wù)器。
TCP是什么?什么是TCP Client? 能做什么?
????????TCP (Transmission Control Protocol) 是一種面向連接的、可靠的、基于字節(jié)流的傳輸協(xié)議,用于在計(jì)算機(jī)網(wǎng)絡(luò)上傳輸數(shù)據(jù)。TCP Client是指TCP網(wǎng)絡(luò)服務(wù)的客戶端連接,主動(dòng)向服務(wù)器發(fā)起連接請(qǐng)求并建立連接,用于實(shí)現(xiàn)串口數(shù)據(jù)和服務(wù)器數(shù)據(jù)的交互,保證數(shù)據(jù)的可靠交換。TCP Clent通常用于設(shè)備與服務(wù)器之間的數(shù)據(jù)交互,是最常用的聯(lián)網(wǎng)通信方式。
????????TCP Client的主要作用是建立和管理與TCP服務(wù)器之間的連接,實(shí)現(xiàn)數(shù)據(jù)的可靠傳輸。通過(guò)TCP Client,設(shè)備可以向服務(wù)器發(fā)送數(shù)據(jù)并從服務(wù)器接收數(shù)據(jù),從而實(shí)現(xiàn)設(shè)備與服務(wù)器之間的數(shù)據(jù)交互。
????????在TCP Client中,客戶端程序需要指定服務(wù)器的IP地址和端口號(hào),并使用TCP協(xié)議與服務(wù)器建立連接。一旦連接建立成功,客戶端程序就可以通過(guò)數(shù)據(jù)流對(duì)象 (NetworkStream) 與服務(wù)器進(jìn)行數(shù)據(jù)交互。
????????因此,TCP Client可以幫助設(shè)備實(shí)現(xiàn)與服務(wù)器之間的可靠數(shù)據(jù)交換,是設(shè)備聯(lián)網(wǎng)通信的重要方式之一。在工業(yè)自動(dòng)化、物聯(lián)網(wǎng)、智能家居等應(yīng)用中,TCP Client被廣泛使用。
連接方式
使開(kāi)發(fā)板和我們的電腦處于同一網(wǎng)段:
- 開(kāi)發(fā)板(設(shè)備)通過(guò)交叉線直連主機(jī)(PC)
- ?開(kāi)發(fā)板和主機(jī)都接在路由器LAN口
測(cè)試工具
- 網(wǎng)絡(luò)調(diào)試工具(任意)
- wireshark抓包工具
回環(huán)測(cè)試
1. 相關(guān)代碼
?如下所示,我們可以看到應(yīng)用實(shí)例里面loopback_tcpc的具體實(shí)現(xiàn),我們需要傳入四個(gè)參數(shù):socket端口號(hào)、收發(fā)緩存、目的IP地址和目的端口號(hào);函數(shù)里面用一個(gè)Switch狀態(tài)機(jī),對(duì)socket端口狀態(tài)輪詢并進(jìn)行相應(yīng)的處理,處于連接狀態(tài)就判斷是否收到數(shù)據(jù),如果有就獲取數(shù)據(jù)大小,并做回傳處理,回傳失敗就關(guān)閉端口;處于關(guān)閉狀態(tài)就跑tcp協(xié)議并打開(kāi)端口;處于初始化狀態(tài)就連接服務(wù)器;處于等待關(guān)閉狀態(tài)就斷開(kāi)連接。
int32_t loopback_tcpc(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport)
{
int32_t ret; // return value for SOCK_ERRORs
uint16_t size = 0, sentsize=0;
// Destination (TCP Server) IP info (will be connected)
// >> loopback_tcpc() function parameter
// >> Ex)
// uint8_t destip[4] = {192, 168, 0, 214};
// uint16_t destport = 5000;
// Port number for TCP client (will be increased)
static uint16_t any_port = 50000;
// Socket Status Transitions
// Check the W5500 Socket n status register (Sn_SR, The 'Sn_SR' controlled by Sn_CR command or Packet send/recv status)
switch(getSn_SR(sn))
{
case SOCK_ESTABLISHED :
if(getSn_IR(sn) & Sn_IR_CON) // Socket n interrupt register mask; TCP CON interrupt = connection with peer is successful
{
#ifdef _LOOPBACK_DEBUG_
printf("%d:Connected to - %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport);
#endif
setSn_IR(sn, Sn_IR_CON); // this interrupt should be write the bit cleared to '1'
}
//
// Data Transaction Parts; Handle the [data receive and send] process
//
if((size = getSn_RX_RSR(sn)) > 0) // Sn_RX_RSR: Socket n Received Size Register, Receiving data length
{
if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE; // DATA_BUF_SIZE means user defined buffer size (array)
ret = recv(sn, buf, size); // Data Receive process (H/W Rx socket buffer -> User's buffer)
if(ret <= 0) return ret; // If the received data length <= 0, receive failed and process end
size = (uint16_t) ret;
sentsize = 0;
// Data sentsize control
while(size != sentsize)
{
ret = send(sn, buf+sentsize, size-sentsize); // Data send process (User's buffer -> Destination through H/W Tx socket buffer)
if(ret < 0) // Send Error occurred (sent data length < 0)
{
close(sn); // socket close
return ret;
}
sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
}
}
//
break;
case SOCK_CLOSE_WAIT :
#ifdef _LOOPBACK_DEBUG_
//printf("%d:CloseWait\r\n",sn);
#endif
if((ret=disconnect(sn)) != SOCK_OK) return ret;
#ifdef _LOOPBACK_DEBUG_
printf("%d:Socket Closed\r\n", sn);
#endif
break;
case SOCK_INIT :
#ifdef _LOOPBACK_DEBUG_
printf("%d:Try to connect to the %d.%d.%d.%d : %d\r\n", sn, destip[0], destip[1], destip[2], destip[3], destport);
#endif
if( (ret = connect(sn, destip, destport)) != SOCK_OK) return ret; // Try to TCP connect to the TCP server (destination)
break;
case SOCK_CLOSED:
close(sn);
if((ret=socket(sn, Sn_MR_TCP, any_port++, 0x00)) != sn){
if(any_port == 0xffff) any_port = 50000;
return ret; // TCP socket open with 'any_port' port number
}
#ifdef _LOOPBACK_DEBUG_
//printf("%d:TCP client loopback start\r\n",sn);
//printf("%d:Socket opened\r\n",sn);
#endif
break;
default:
break;
}
return 1;
}
?主函數(shù)就比較簡(jiǎn)單,在此之前我們先聲明socket端口號(hào)和所用最大的緩存大小,不做分片處理默認(rèn)為2KB;然后初始化網(wǎng)絡(luò)信息、目標(biāo)IP地址和目標(biāo)端口,最后在while循環(huán)里調(diào)用loopback_tcpc并傳入相應(yīng)參數(shù)即可。
注意:這里的目的IP地址設(shè)置為我們的電腦IP地址,因?yàn)槲覀円岆娔X端作為服務(wù)器,使用網(wǎng)絡(luò)調(diào)試助手進(jìn)行數(shù)據(jù)回環(huán)測(cè)試;另外目標(biāo)端口選擇盡量避免使用特殊端口,這里使用8080
#define SOCKET_ID 0
#define ETHERNET_BUF_MAX_SIZE (1024 * 2)
void network_init(void);
wiz_NetInfo net_info = {
.mac = {0x00, 0x08, 0xdc, 0x16, 0xed, 0x2e},
.ip = {192, 168, 1, 10},
.sn = {255, 255, 255, 0},
.gw = {192, 168, 1, 1},
.dns = {8, 8, 8, 8},
.dhcp = NETINFO_STATIC};
wiz_NetInfo get_info;
static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {0,};
static uint8_t des_ip[4] = {192, 168, 1, 2};
static uint16_t des_port = 8080;
int main()
{
stdio_init_all();
sleep_ms(2000);
network_init();
while(true)
{
loopback_tcpc(SOCKET_ID, ethernet_buf, des_ip, des_port);
sleep_ms(500);
}
}
void network_init(void)
{
uint8_t temp;
wizchip_initialize();
printf("W5500 tcp client example.\r\n");
sleep_ms(2000);
wizchip_setnetinfo(&net_info);
print_network_information(get_info);
sleep_ms(2000);
}
2.測(cè)試現(xiàn)象
我們編譯燒錄后,打開(kāi)串行監(jiān)視器,可以看到,配置相關(guān)信息后,嘗試連接我們初始化設(shè)置的目的IP(電腦IP),然后我們?cè)陔娔X上打開(kāi)網(wǎng)絡(luò)調(diào)試助手,選擇tcp服務(wù)器模式,IP選擇電腦的本機(jī)IP(一般默認(rèn)即為電腦IP),端口號(hào)寫8080(跟我們?cè)陂_(kāi)發(fā)板配置的信息一致,不然監(jiān)聽(tīng)不到),配置完成打開(kāi)后,可以看到客戶端上線提示,嘗試發(fā)送數(shù)據(jù),可以看到成功回傳。
?
?我們也可以在打開(kāi)wireshark抓包工具,輸入命令<ip.addr == 192.168.1.10 and tcp>過(guò)濾數(shù)據(jù)包(IP地址改成自己電腦的,也即開(kāi)發(fā)板設(shè)置的目標(biāo)IP地址);我這里先關(guān)閉網(wǎng)絡(luò)調(diào)試助手,然后又打開(kāi),接著發(fā)送0~9十個(gè)阿拉伯?dāng)?shù)字,可以通過(guò)抓包工具十分清楚明了的看到具體交互過(guò)程,如下圖所示。
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-642794.html
?相關(guān)鏈接:
本章相應(yīng)例程https://gitee.com/wiznet-hk/w5500-evb-pico-routine.gitwireshark抓包工具下載鏈接https://www.wireshark.org/download.html文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-642794.html
到了這里,關(guān)于W5500-EVB-PICO作為TCP Client 進(jìn)行數(shù)據(jù)回環(huán)測(cè)試(五)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!