之前應(yīng)用CH561出現(xiàn)問題,現(xiàn)在找銷售要來最新的CH32V307的開發(fā)板 也是還有如下的問題:WCHNET_SocketUdpSendTo發(fā)送到一個不在線的 目的地IP? 之后 整個socket 卡死,不能再發(fā)送 也不能恢復(fù) 直到重啟。 這個bug 有沒有其他使用者遇到過,還是根本不支持 直接
6月2日的evt測試表示沒有問題
??? while(1)
??? {
??????? WCHNET_MainTask();
??????? if(WCHNET_QueryGlobalInt())
??????? {
??????????? WCHNET_HandleGlobalInt();
??????? }
??????? if(keypress==1)?????? //PA0作為按鍵輸入 按一次 發(fā)送數(shù)據(jù)到以下兩個IP地址
??????? {
??????????? i = WCHNET_SocketUdpSendTo( SocketId,str,&len,roomaddr1,7888) ;
??????????? printf("roomaddr1=%d\r\n", i);
???????????? i=WCHNET_SocketUdpSendTo( SocketId,str,&len,roomaddr2,7888) ;
???????????? printf("roomaddr2=%d\r\n",i);
???????????? keypress=0;
??????? }
??? }
這個是我的代碼,是否是我使用上的問題 ?roomaddr1? roomaddr2 均為 與本機ip同網(wǎng)段的IP,這個是主動發(fā)送?。只要這兩個IP設(shè)備都在線 發(fā)送沒有問題,只有有其一不在線的 則不能再發(fā)送。???
如果你是按照官方例程在接收回調(diào)函數(shù)里面再發(fā)送給對方,肯定沒問題的!
我也測試了主動發(fā)送,沒問題,只是打印出來GINT_STAT_UNREACH,估計你問題出在socketid身上
socketid? 設(shè)置有什么 要求嗎 就是按照官方設(shè)置了一個目的地地址? 請指教
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 = RECE_BUF_LEN;
? ? TmpSocketInf.AppCallBack = WCHNET_UdpServerRecv;
? ? i = WCHNET_SocketCreat(&SocketId, &TmpSocketInf);
? ? printf("WCHNET_SocketCreat %d\r\n", SocketId);
? ? mStopIfError(i);
}
創(chuàng)建socketid 都是用的官方的例程函數(shù)
#include "string.h"
#include "debug.h"
#include "WCHNET.h"
#include "eth_driver.h"
u8 str[] = "ch03";
u8 roomaddr1[4] = {192,168,100,241};
u8 roomaddr2[4] = {192,168,100,242};
int?? keypress=0;
u8 MACAddr[6];?????????????????????????????????? //MAC address
u8 IPAddr[4] = { 192, 168, 100, 174 };????????????? //IP address
u8 GWIPAddr[4] = { 192, 168, 100, 1 };???????????? //Gateway IP address
u8 IPMask[4] = { 255, 255, 255, 0 };???????????? //subnet mask
u8 IP255[4] = { 255, 255, 255, 255};???????????? //subnet mask
u16 srcport = 1002;????????????????????????????? //source port
u8 SocketId;
u8 SocketRecvBuf[RECE_BUF_LEN];????????????????? //socket receive buffer
void mStopIfError(u8 iError)
{
??? if (iError == WCHNET_ERR_SUCCESS)
??????? return;
??? printf("Error: %02X\r\n", (u16) iError);
}
void EXTI0_INT_INIT(void)
{
??? GPIO_InitTypeDef GPIO_InitStructure = {0};
??? EXTI_InitTypeDef EXTI_InitStructure = {0};
??? NVIC_InitTypeDef NVIC_InitStructure = {0};
??? RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);
??? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
??? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
??? GPIO_Init(GPIOA, &GPIO_InitStructure);
??? /* GPIOA ----> EXTI_Line0 */
??? GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
??? EXTI_InitStructure.EXTI_Line = EXTI_Line0;
??? EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
??? EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
??? EXTI_InitStructure.EXTI_LineCmd = ENABLE;
??? EXTI_Init(&EXTI_InitStructure);
??? NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
??? NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
??? NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
??? NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
??? NVIC_Init(&NVIC_InitStructure);
}
void TIM2_Init(void)
{
??? TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = { 0 };
??? RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
??? TIM_TimeBaseStructure.TIM_Period = SystemCoreClock / 1000000 - 1;
??? TIM_TimeBaseStructure.TIM_Prescaler = WCHNETTIMERPERIOD * 1000 - 1;
??? TIM_TimeBaseStructure.TIM_ClockDivision = 0;
??? TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
??? TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
??? TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
??? TIM_Cmd(TIM2, ENABLE);
??? TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
??? NVIC_EnableIRQ(TIM2_IRQn);
}
void WCHNET_UdpServerRecv(struct _SCOK_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);
??? WCHNET_SocketUdpSendTo(socinf->SockIndex, buf, &len, ip_addr, port);
}
void WCHNET_CreateUdpSocket (void)
{
??? u8 i;
??? SOCK_INF TmpSocketInf;
??? memset((void *) &TmpSocketInf, 0, sizeof(SOCK_INF));
??? TmpSocketInf.DesPort = 1002;
??? TmpSocketInf.SourPort = 1002;
??? TmpSocketInf.ProtoType = PROTO_TYPE_UDP;
??? TmpSocketInf.RecvStartPoint = (u32) SocketRecvBuf;
??? TmpSocketInf.RecvBufLen = RECE_BUF_LEN;
??? TmpSocketInf.AppCallBack = WCHNET_UdpServerRecv;
??? i = WCHNET_SocketCreat(&SocketId, &TmpSocketInf);
??? printf("WCHNET_SocketCreat %d\r\n", SocketId);
??? mStopIfError(i);
}
void WCHNET_HandleSockInt(u8 socketid, u8 intstat)
{
??? if (intstat & SINT_STAT_RECV)?????????????????????????????? //receive data
??? {
??? }
??? if (intstat & SINT_STAT_CONNECT)??????????????????????????? //connect successfully
??? {
??????? printf("TCP Connect Success\r\n");
??? }
??? if (intstat & SINT_STAT_DISCONNECT)???????????????????????? //disconnect
??? {
??????? printf("TCP Disconnect\r\n");
??? }
??? if (intstat & SINT_STAT_TIM_OUT)??????????????????????????? //timeout disconnect
??? {
??????? printf("TCP Timeout\r\n");
??? }
}
void WCHNET_HandleGlobalInt(void)
{
??? u8 intstat;
??? u16 i;
??? u8 socketint;
??? intstat = WCHNET_GetGlobalInt();????????????????????????????? //get global interrupt flag
??? if (intstat & GINT_STAT_UNREACH)????????????????????????????? //Unreachable interrupt
??? {
??????? printf("GINT_STAT_UNREACH\r\n");
??? }
??? if (intstat & GINT_STAT_IP_CONFLI)??????????????????????????? //IP conflict
??? {
??????? printf("GINT_STAT_IP_CONFLI\r\n");
??? }
??? if (intstat & GINT_STAT_PHY_CHANGE)?????????????????????????? //PHY status change
??? {
??????? i = WCHNET_GetPHYStatus();
??????? if (i & PHY_Linked_Status)
??????????? printf("PHY Link Success\r\n");
??? }
??? if (intstat & GINT_STAT_SOCKET) {???????????????????????????? //socket related interrupt
??????? for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {
??????????? socketint = WCHNET_GetSocketInt(i);
??????????? if (socketint)
??????????????? WCHNET_HandleSockInt(i, socketint);
??????? }
??? }
}
int main(void)
{
??? u8 i;
??? u32 len;
??? len=4;
??? Delay_Init();
??? USART_Printf_Init(115200);??????????????????????????????????? //USART initialize
??? EXTI0_INT_INIT();
??? printf("UdpServer Test\r\n");
??? printf("SystemClk:%d\r\n", SystemCoreClock);
??? printf("net version:%x\n", WCHNET_GetVer());
??? if ( WCHNET_LIB_VER != WCHNET_GetVer()) {
??????? printf("version error.\n");
??? }
??? WCHNET_GetMacAddr(MACAddr);?????????????????????????????????? //get the chip MAC address
??? printf("mac addr:");
??? for (int i = 0; i < 6; i++)
??????? printf("%x ", MACAddr[i]);
??? printf("\n");
??? TIM2_Init();
??? i = ETH_LibInit(IPAddr, GWIPAddr, IPMask, MACAddr);?????????? //Ethernet library initialize
??? mStopIfError(i);
??? if (i == WCHNET_ERR_SUCCESS)
??????? printf("WCHNET_LibInit Success\r\n");
??? WCHNET_CreateUdpSocket();???????????????????????????????????? //Create? UDP Socket
??? while(1)
??? {
??????? WCHNET_MainTask();
??????? if(WCHNET_QueryGlobalInt())
??????? {
??????????? WCHNET_HandleGlobalInt();
??????? }
??????? if(keypress==1)?????? //PA0作為按鍵輸入 按一次 發(fā)送數(shù)據(jù)到以下兩個IP地址
??????? {
??????????? i = WCHNET_SocketUdpSendTo( SocketId,str,&len,roomaddr1,1002) ;
?????????? //? i=WCHNET_SocketUdpSendTo( SocketId,str,&len,roomaddr2,7888) ;
?????????? //? printf("roomaddr2=%d\r\n",i);
???????????? keypress=0;
??????? }
??? }
}
/*以上是我整個main.c的全部代碼,使用pa0中斷 按下之后 發(fā)送一次數(shù)據(jù),你可以修改IP地址 測試即可,對方使用電腦網(wǎng)絡(luò)調(diào)試助手,現(xiàn)在即使不發(fā)給其他IP,第一次也發(fā)送不出去。一定要先收到電腦發(fā)來第一次數(shù)據(jù)之后,才可以 主動發(fā)送數(shù)據(jù)?,F(xiàn)在測試到的就是 上電之后 一定要電腦先發(fā)數(shù)據(jù)過來,再按鍵 才可以發(fā)送數(shù)據(jù)到電腦。否則上電之后先按鍵發(fā)送了, 即使之后電腦再發(fā)數(shù)據(jù), 按鍵的主動發(fā)送也不會成功,而且 如果上電之后 主動發(fā)送 會提示 ?TCP Timeout。
*/
以上是我整個main.c的全部代碼,使用pa0中斷 按下之后 發(fā)送一次數(shù)據(jù),你可以修改IP地址 測試即可,對方使用電腦網(wǎng)絡(luò)調(diào)試助手,現(xiàn)在即使不發(fā)給其他IP,第一次也發(fā)送不出去。一定要先收到電腦發(fā)來第一次數(shù)據(jù)之后,才可以 主動發(fā)送數(shù)據(jù)?,F(xiàn)在測試到的就是 上電之后 一定要電腦先發(fā)數(shù)據(jù)過來,再按鍵 才可以發(fā)送數(shù)據(jù)到電腦。否則上電之后先按鍵發(fā)送了, 即使之后電腦再發(fā)數(shù)據(jù), 按鍵的主動發(fā)送也不會成功,而且 如果上電之后 主動發(fā)送 會提示 ?TCP Timeout。
大概看了一下,其余都基本一樣,除了這個函數(shù) void WCHNET_CreatSocket(void)
這個是我使用的函數(shù),全部功能正常,不會有第一次發(fā)送不了,也不會有超時發(fā)生,你說的問題,我這里一個也沒有
void WCHNET_CreatSocket(void)
{
SOCK_INF TmpSocketInf;
memset((void *)&TmpSocketInf, 0, sizeof(SOCK_INF));
TmpSocketInf.SourPort = LocalPort;
TmpSocketInf.ProtoType = PROTO_TYPE_UDP;
TmpSocketInf.RecvBufLen = RECE_BUF_LEN;
TmpSocketInf.AppCallBack = WCHNET_UdpServerRecv;
WCHNET_SocketCreat(&SocketId, &TmpSocketInf);
WCHNET_ModifyRecvBuf(SocketId, (u32)SocketRecvBuf[SocketId], RECE_BUF_LEN);
}
改成你這樣還是不行,可能是板子硬件不行,板子是剛剛拿到的沁恒的開發(fā)板,調(diào)這個東西真是心累
出現(xiàn)相似的問題了,要先收到一組數(shù)據(jù)后才能發(fā)出去,請教一下怎么解決,方便的話可以看看代碼嗎
您好,我這邊測試沒有發(fā)現(xiàn)這種情況,不知您是采用怎樣的方式測試的,我是通過按鍵,按一下發(fā)送數(shù)據(jù),因為您一開始主動發(fā)的時候不知道接收端的端口,所以會出現(xiàn)數(shù)據(jù)不可達的情況,如圖1,若您想接收到數(shù)據(jù),可以先通過調(diào)試助手發(fā)一次數(shù)據(jù),獲取已連通的接收端端口,修改代碼中的端口號,重新編譯燒錄之后(網(wǎng)絡(luò)調(diào)試助手別斷開),通過按鍵發(fā)送即可接收到數(shù)據(jù),若您是直接建立的固定端口,則不需要上述操作,直接通過按鍵發(fā)送即可接收到數(shù)據(jù)。測試結(jié)果如圖2所示,測試例程已附上,您可以測試一下,后續(xù)有問題可以通過郵箱聯(lián)系:kx@wch.cn
圖1:
圖2