|
大家好!我想请问一下使用udp_send函数时res值是netInvalidParameter该怎么修改呢?代码如下(此代码没有问题,但是我修改之后res的值会出错,修改后的代码放在最下边):
#include "includes.h"
/*
*********************************************************************************************************
* 用于本文件的调试
*********************************************************************************************************
*/
#if 1
#define printf_debug printf
#else
#define printf_debug(...)
#endif
#include "includes.h"
/*
*********************************************************************************************************
* 用于本文件的调试
*********************************************************************************************************
*/
#if 1
#define printf_debug printf
#else
#define printf_debug(...)
#endif
/*
*********************************************************************************************************
* 宏定义,远程服务器的IP和端口
*********************************************************************************************************
*/
/* 要访问的远程服务器IP和端口配置,也就是电脑端调试助手设置的IP和端口号 */
#define IP1 192
#define IP2 168
#define IP3 1
#define IP4 53
#define PORT_NUM 1001
#define Local_NUM 8080
/*
*********************************************************************************************************
* 变量
*********************************************************************************************************
*/
static NET_ADDR addr = { NET_ADDR_IP4, PORT_NUM, IP1, IP2, IP3, IP4};
static int32_t udp_sock; // UDP socket handle
/* TCPnet API的返回值 */
static const char * ReVal_Table[]=
{
"netOK: Operation succeeded",
"netBusy: Process is busy",
"netError: Unspecified error",
"netInvalidParameter: Invalid parameter specified",
"netWrongState: Wrong state error",
"netDriverError: Driver error",
"netServerError: Server error",
"netAuthenticationFailed: User authentication failed",
"netDnsResolverError: DNS host resolver failed",
"netFileError: File not found or file r/w error",
"netTimeout: Operation timeout",
};
/*
*********************************************************************************************************
* 函 数 名: tcp_callback
* 功能说明: TCP Socket的回调函数
* 形 参: socket UDP Socket类型
* remip 远程设备的IP地址
* remport 远程设备的端口号
* buf 远程设备发来的数据地址
* len 远程设备发来的数据长度,单位字节
* 返 回 值: 默认返回0即可,一般用不上
*********************************************************************************************************
*/
static uint32_t udp_cb_func (int32_t socket, const NET_ADDR *addr, const uint8_t *buf, uint32_t len)
{
char buffer[50];
uint16_t i = 0;
/* 确保是udp_soc的回调 */
if (socket != udp_sock)
{
return (0);
}
/* 发消息的远程IP,打印IP和端口号 */
sprintf(buffer, "远程连接IP: %d.%d.%d.%d", addr->addr[0], addr->addr[1], addr->addr[2], addr->addr[3]);
printf_debug("%s port:%d\r\n", buffer, addr->port);
/* 接收到的数据长度,单位字节 */
printf_debug("Data length = %d\r\n", len);
/* 打印接收到的消息 */
for(i = 0; i < len; i++)
{
printf_debug("buf[%d] = %d\r\n", i, buf[i]);
}
return (0);
}
/*
*********************************************************************************************************
* 函 数 名: DM9162TCPnetTest
* 功能说明: DM9162TCPnetTest应用
* 形 参: 无
* 返 回 值: 无
*********************************************************************************************************
*/
void UDPnetTest(void)
{
int32_t iCount;
uint8_t *sendbuf;
netStatus res;
const uint16_t usMaxBlockTime = 2; /* 延迟周期 */
uint32_t EvtFlag;
udp_sock = netUDP_GetSocket (udp_cb_func);
if (udp_sock >= 0)
{
netUDP_Open (udp_sock, Local_NUM);
//netUDP_SetOption (udp_sock, netUDP_OptionInterface, NET_IF_CLASS_ETH | 0);
}
while (1)
{
EvtFlag = osThreadFlagsWait(0x00000007U, osFlagsWaitAny, usMaxBlockTime);
switch (EvtFlag)
{
/* 接收到K1键按下,给远程TCP客户端发送数据 */
case KEY1_BIT0:
iCount = 1;
do
{
/* 申请8字节的空间 */
sendbuf = netUDP_GetBuffer(8);
if(sendbuf != NULL)
{
/* 初始化8个字节变量 */
sendbuf[0] = '1';
sendbuf[1] = '2';
sendbuf[2] = '3';
sendbuf[3] = '4';
sendbuf[4] = '5';
sendbuf[5] = '6';
sendbuf[6] = '7';
sendbuf[7] = '8';
res = netUDP_Send(udp_sock, &addr, sendbuf, 8); // 在这个地方res的值为 "netOK: Operation succeeded",
/* 保证发送成功了发送次数才可以减一,这里发送成功并不保证远程设备接受成功 */
if(res == netOK)
{
iCount--;
}
else
{
printf("发送失败%s\r\n", ReVal_Table[res]);
}
//osDelay(50);
}
}while(iCount > 0);
break;
/* 其他的键值不处理 */
default:
break;
}
}
}
修改后的部分代码为:
if (udp_sock >= 0)
{
netUDP_Open (udp_sock, Local_NUM);
//netUDP_SetOption (udp_sock, netUDP_OptionInterface, NET_IF_CLASS_ETH | 0);
}
if (logo ==1)
{
sendbuf = netUDP_GetBuffer(4);
if(sendbuf != NULL)
{
/* 初始化4个字节变量 */
sendbuf[0] = 'd';
sendbuf[1] = 'o';
sendbuf[2] = 'n';
sendbuf[3] = 'e';
res = netUDP_Send(udp_sock,&addr, sendbuf, 4); //在这个地方res的值为"netInvalidParameter: Invalid parameter specified",
if(res==netOK)
{
printf("system is already!");
}
|
|