zhangyuyu 发表于 2022-4-17 09:18:24

关于netudp_send函数的使用

大家好!我想请问一下使用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;
      uint16_t i = 0;

      /* 确保是udp_soc的回调 */
      if (socket != udp_sock)
      {
                return (0);
      }

      /* 发消息的远程IP,打印IP和端口号 */
      sprintf(buffer, "远程连接IP: %d.%d.%d.%d", addr->addr, addr->addr, addr->addr, addr->addr);
      printf_debug("%sport:%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);
      }

      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 = '1';
                                                        sendbuf = '2';
                                                        sendbuf = '3';
                                                        sendbuf = '4';
                                                        sendbuf = '5';
                                                        sendbuf = '6';
                                                        sendbuf = '7';
                                                        sendbuf = '8';
                                                        res = netUDP_Send(udp_sock, &addr, sendbuf, 8);         // 在这个地方res的值为 "netOK: Operation succeeded",

                                                        /* 保证发送成功了发送次数才可以减一,这里发送成功并不保证远程设备接受成功 */
                                                        if(res == netOK)
                                                        {
                                                                iCount--;
                                                        }
                                                        else
                                                        {
                                                                printf("发送失败%s\r\n", ReVal_Table);
                                                        }
                                                                        //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 = 'd';
                                        sendbuf = 'o';
                                        sendbuf = 'n';
                                        sendbuf = 'e';
                                                       
                                        res = netUDP_Send(udp_sock,&addr, sendbuf, 4);            //在这个地方res的值为"netInvalidParameter: Invalid parameter specified",
                                        if(res==netOK)
                                        {
                                                printf("system is already!");
                                        }

eric2013 发表于 2022-4-17 12:35:59

修改后的代码,贴个完整的看看。

zhangyuyu 发表于 2022-4-17 17:23:28

你好,这个函数如下:
void UDPnetTest(void)
{
    int32_t iCount;
    netStatus res;
    const uint16_t usMaxBlockTime=2;
    uint8_t *sendbuf;
    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);
    }
               
                if (logo ==1)
                {
                        sendbuf =netUDP_GetBuffer(4);
                       
                        if(sendbuf != NULL)
                        {
                                        /* 初始化4个字节变量 */
                                        sendbuf = 'd';
                                        sendbuf = 'o';
                                        sendbuf = 'n';
                                        sendbuf = 'e';
                                                       
                                        res = netUDP_Send(udp_sock,&addr, sendbuf, 4);          // 设置断点运行到这里发现并未发送成功,而是说参数无效
                                        if(res==netOK)
                                        {
                                                printf("system is already!");
                                        }
                                       
                                        logo =0 ;//清空通信标志
                   }
               
    while(1)
    {
                                //osDelay(10000);
       // sendbuf =netUDP_GetBuffer(8);
      //if(sendbuf != NULL)
      
       //   /* 初始化8个字节变量 */
         // sendbuf = '1';
         // sendbuf = '2';
         // sendbuf = '3';
         // sendbuf = '4';
         // sendbuf = '5';
          //sendbuf = '6';
          //sendbuf = '7';
         // sendbuf = '8';
          //res = netUDP_Send(udp_sock, &addr, sendbuf, 8);
                       
                                                switch (Rec)
                        {
                                /* 接收到模式2,UDP发送数据 */
                               
                                case 2:       
                                       
                                        sendbuf =netUDP_GetBuffer(4096*8);         //申请空间,分配数据
                                  
                                  if(sendbuf != NULL)
                                        {
                                                for(iCount=0;iCount<=4096*8;iCount++)
                                     {
                                          sendbuf = CirOut;
                                     }
                                        }
                                                       
                                /* 必须使用申请的内存空间 */
                                        res = netUDP_Send(udp_sock, &addr, sendbuf, 4096*8);
                                               
                                        break;
                                       
                                default:
                                        break;
                        }
                       
                       
       }
    }
}

eric2013 发表于 2022-4-18 16:04:10

zhangyuyu 发表于 2022-4-17 17:23
你好,这个函数如下:
void UDPnetTest(void)
{


你的这个logo变量是在哪里设置的。

zhangyuyu 发表于 2022-4-19 08:22:25

eric2013 发表于 2022-4-18 16:04
你的这个logo变量是在哪里设置的。

它是全局变量,程序可以运行到udp_send那里

eric2013 发表于 2022-4-20 09:39:55

zhangyuyu 发表于 2022-4-19 08:22
它是全局变量,程序可以运行到udp_send那里

感觉是程序逻辑有点问题。

当前已经保证修改前是正常的吧。
页: [1]
查看完整版本: 关于netudp_send函数的使用