全能小网关|CH32V208--第三篇:ETH使用
<div class='showpostmsg'> 本帖最后由 xiaolinen 于 2024-7-2 20:43 编辑<p><span style="font-size:22px;"><strong>一:准备工作</strong></span></p>
<p><span style="font-size:20px;"> PC端设置以太网IP和端口,如下图所示:</span></p>
<div style="text-align: center;"></div>
<p><strong><span style="font-size:22px;">二:TCP 客户端测试使用</span></strong></p>
<p><span style="font-size:20px;"> 2.1,重点代码记录</span></p>
<p><span style="font-size:20px;"> 2.1.1,创建TCP客户端</span></p>
<pre>
<code class="language-cpp">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);
}</code></pre>
<p><span style="font-size:20px;"> 2.1.2,套接字中断事件监控</span></p>
<pre>
<code class="language-cpp">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, RECE_BUF_LEN);
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {
if (socket == 0xff) {//save connected socket id
socket = socketid;
break;
}
}
printf("TCP Connect Success\r\n");
printf("socket id: %d\r\n", socket);
}
if (intstat & SINT_STAT_DISCONNECT)//disconnect
{
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {//delete disconnected socket id
if (socket == socketid) {
socket = 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 == socketid) {
socket = 0xff;
break;
}
}
printf("TCP Timeout\r\n");
WCHNET_CreateTcpSocket();
}
}</code></pre>
<p><span style="font-size:20px;"> 2.2,实验现象:TCP客户端接收到服务器下发的数据,打印展示,并将数据原路返回至服务器,如下图所示:</span></p>
<div style="text-align: center;"></div>
<div><strong><span style="font-size:22px;">三:UDP服务器测试使用</span></strong></div>
<div><span style="font-size:20px;"> 3.1,重点代码记录</span></div>
<div><span style="font-size:20px;"> 3.1.1,创建UDP服务器</span></div>
<div>
<pre>
<code class="language-cpp">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);
}</code></pre>
<p><span style="font-size:20px;"> 3.1.2,数据接收回调函数</span></p>
<pre>
<code class="language-cpp">void WCHNET_UdpServerRecv(struct _SOCK_INF *socinf, u32 ipaddr, u16 port, u8 *buf, u32 len)
{
u8 ip_addr, i;
printf("Remote IP:");
for (i = 0; i < 4; i++) {
ip_addr = ipaddr & 0xff;
printf("%d ", ip_addr);
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);
}</code></pre>
<p><span style="font-size:20px;"> 3.2,实验现象:UDP服务器监听客户端,打印展示</span><span style="font-size:20px;">接收到的数据,并将数据原路返回至客户端,如下图所示:</span></p>
</div>
<div style="text-align: center;"></div>
<p> </p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> 他这个,是跑了OS吗?需要跑OS吗? lugl4313820 发表于 2024-7-3 06:55
他这个,是跑了OS吗?需要跑OS吗?
<p>帖子里的实验过程没有跑OS的,但是这款芯片是可以跑OS的,具体怎么选择看自己的需求了。</p>
页:
[1]