一:准备工作
PC端设置以太网IP和端口,如下图所示:
二:TCP 客户端测试使用
2.1,重点代码记录
2.1.1,创建TCP客户端
void WCHNET_CreateTcpSocket(void)
{
u8 i;
SOCK_INF TmpSocketInf;
memset((void *) &TmpSocketInf, 0, sizeof(SOCK_INF));
memcpy((void *) TmpSocketInf.IPAddr, DESIP, 4);//设置目的IP
TmpSocketInf.DesPort = desport;//设置目的端口
TmpSocketInf.SourPort = srcport++;//设置源端口
TmpSocketInf.ProtoType = PROTO_TYPE_TCP;//设置协议类型
TmpSocketInf.RecvBufLen = RECE_BUF_LEN;//设置数据缓冲区长度
i = WCHNET_SocketCreat(&SocketId, &TmpSocketInf);
printf("WCHNET_SocketCreat %d\r\n", SocketId);
mStopIfError(i);
i = WCHNET_SocketConnect(SocketId);//建立TCP连接
mStopIfError(i);
}
2.1.2,套接字中断事件监控
void WCHNET_DataLoopback(u8 id)
{
u32 len, totallen;
u8 *p = MyBuf, TransCnt = 255;
len = WCHNET_SocketRecvLen(id, NULL);//query length
printf("Receive Len = %d\r\n", len);
totallen = len;
WCHNET_SocketRecv(id, MyBuf, &len);//Read the data of the receive buffer into MyBuf
printf("recv buf :\r\n%s\r\n",MyBuf);
while(1){
len = totallen;
WCHNET_SocketSend(id, p, &len);//Send the data
totallen -= len;//Subtract the sent length from the total length
p += len;//offset buffer pointer
if( !--TransCnt ) break;//Timeout exit
if(totallen) continue;//If the data is not sent, continue to send
break;//After sending, exit
}
}
void WCHNET_HandleSockInt(u8 socketid, u8 intstat)
{
u8 i;
if (intstat & SINT_STAT_RECV)//receive data
{
printf("TCP Client Recv!\r\n");
WCHNET_DataLoopback(socketid);//Data loopback
}
if (intstat & SINT_STAT_CONNECT)//connect successfully
{
#if KEEPALIVE_ENABLE
WCHNET_SocketSetKeepLive(socketid, ENABLE);
#endif
WCHNET_ModifyRecvBuf(socketid, (u32) SocketRecvBuf[socketid], RECE_BUF_LEN);
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {
if (socket[i] == 0xff) {//save connected socket id
socket[i] = socketid;
break;
}
}
printf("TCP Connect Success\r\n");
printf("socket id: %d\r\n", socket[i]);
}
if (intstat & SINT_STAT_DISCONNECT)//disconnect
{
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {//delete disconnected socket id
if (socket[i] == socketid) {
socket[i] = 0xff;
break;
}
}
printf("TCP Disconnect\r\n");
}
if (intstat & SINT_STAT_TIM_OUT)//timeout disconnect
{
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {//delete disconnected socket id
if (socket[i] == socketid) {
socket[i] = 0xff;
break;
}
}
printf("TCP Timeout\r\n");
WCHNET_CreateTcpSocket();
}
}
2.2,实验现象:TCP客户端接收到服务器下发的数据,打印展示,并将数据原路返回至服务器,如下图所示:
void WCHNET_CreateUdpSocket(void)
{
u8 i;
SOCK_INF TmpSocketInf;
memset((void *) &TmpSocketInf, 0, sizeof(SOCK_INF));
TmpSocketInf.SourPort = srcport;//设置源端口
TmpSocketInf.ProtoType = PROTO_TYPE_UDP;//设置协议类型
TmpSocketInf.RecvStartPoint = (u32) SocketRecvBuf;//设置数据缓冲区起始地址
TmpSocketInf.RecvBufLen = UDP_RECE_BUF_LEN;//设置数据缓冲区长度
TmpSocketInf.AppCallBack = WCHNET_UdpServerRecv;//绑定接受回调函数
i = WCHNET_SocketCreat(&SocketId, &TmpSocketInf);
printf("WCHNET_SocketCreat %d\r\n", SocketId);
mStopIfError(i);
}
3.1.2,数据接收回调函数
void WCHNET_UdpServerRecv(struct _SOCK_INF *socinf, u32 ipaddr, u16 port, u8 *buf, u32 len)
{
u8 ip_addr[4], i;
printf("Remote IP:");
for (i = 0; i < 4; i++) {
ip_addr[i] = ipaddr & 0xff;
printf("%d ", ip_addr[i]);
ipaddr = ipaddr >> 8;
}
printf("srcport = %d len = %d socketid = %d\r\n", port, len,
socinf->SockIndex);
printf("buf:%s\r\n",buf);
WCHNET_SocketUdpSendTo(socinf->SockIndex, buf, &len, ip_addr, port);
}
3.2,实验现象:UDP服务器监听客户端,打印展示接收到的数据,并将数据原路返回至客户端,如下图所示:
本帖最后由 xiaolinen 于 2024-7-2 20:43 编辑
引用: lugl4313820 发表于 2024-7-3 06:55 他这个,是跑了OS吗?需要跑OS吗?
帖子里的实验过程没有跑OS的,但是这款芯片是可以跑OS的,具体怎么选择看自己的需求了。