c - LWIP ECHO SERVER:如何增加itoa函数中的缓冲区大小?

标签 c fpga lwip

我正在使用Xilinx Ethernetlite(LWIP)设计。仅当buf = 32时,我才能通过以太网将数据从KC板传输到PC(Hercules)。但是我的实际缓冲区大小是1024。如何将缓冲区大小从32增加到1024

我无法确定错误是在代码中还是在大力士中。要读取大力神中的值(整数),我正在执行此功能。

最初,我将从Hercules将Hello命令发送到Board,然后Board接受该请求。此后,开发板将数据(整数值)输出到Hercules。

itoa的C代码

char* itoa(int val, int base)
{
static char buf[32] = {0};          //buf size 
int i = 30;

for(; val && i ; --i, val /= base)
buf[i] = "0123456789abcdef"[val % base];
return &buf[i+1];
}


修改后的代码

    #define  DAQ_FIFO_DEPTH  128

  int transfer_data() 
  {
  return 0;
  }

  err_t tcp_write_u32_string(struct tcp_pcb *pcb, unsigned char   prefix, u32_t value)
   {
    unsigned char  buf[11]; /* enough room for prefix and value. */
    err_t          result;
    u16_t          len;
    unsigned char *p = buf + sizeof buf;
  do {
    /* ASCII encoding: '0' = 48, '1' = 49, ..., '9' = 57. */
    *(--p) = 48 + (value % 10u);
    value /= 10;
     } while (value);
     if (prefix)
    *(--p) = prefix;
 len = buf + sizeof buf - p;
  if (tcp_sndbuf(pcb) < len) 
     {
    result = tcp_output(pcb);
    if (result != ERR_OK)
        return result;
    }
 return tcp_write(pcb, p, len, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
  }

   err_t send_list(struct tcp_pcb *pcb, const u32_t data[], u16_t len)
  {
  static const char  newline[2] = { 13, 10 }; /* ASCII \r\n */
  err_t              result;

    if (len > 0) {
     u16_t  i;


      result = tcp_write_u32_string(pcb, 0, data[0]);
      if (result != ERR_OK)
        return result;
    for (i = 1; i < len; i++) 
   {
        /* ASCII comma is code 44. (Use 32 for space, or 9 for tab.) */
        result = tcp_write_u32_string(pcb, 44, data[i]);
        if (result != ERR_OK)
            return result;
     }
   }
   result = tcp_write(pcb, newline, 2, 0);
    if (result)
    return result; 
   return tcp_output(pcb);
  }
 int application_connection(void *arg, struct tcp_pcb *conn, err_t err)
 {
  struct netif *netif = arg; /* Because of tcp_arg(, netif). */
  u32_t         data[DAQ_FIFO_DEPTH];
  u32_t         i, n;
 if (err != ERR_OK) {
    tcp_abort(conn);
    return ERR_ABRT;
   }
  err = daq_setup();
  if (err != ERR_OK) 
  {
    tcp_abort(conn);
    return ERR_ABRT;
 }
 while (1) 
    {
    xemacif_input(netif);
    tcp_tmr();
    tcp_output(conn);
    n = daq_acquire(data, DAQ_FIFO_DEPTH);
    if (n > DAQ_FIFO_DEPTH)
        break;
    if (tcp_write(conn, data, n * sizeof data[0], TCP_WRITE_FLAG_COPY) != ERR_OK)
        break;
     }
// daq_close();

/* Close the TCP connection. */
    if (tcp_close(conn) == ERR_OK)
     return ERR_OK;

/* Close failed. Abort it, then. */
    tcp_abort(conn);
    return ERR_ABRT;
    }
  int application_main(struct netif *netif, unsigned int port)
  {
   struct tcp_pcb *pcb;
   err_t           err;
   pcb = tcp_new();
   if (!pcb) {
    /* Out of memory error */
    return -1;
      }
    err = tcp_bind(pcb, IP_ADDR_ANY, port);
    if (err != ERR_OK) {
    /* TCP error */
    return -1;
     }
   pcb = tcp_listen_with_backlog(pcb, 1);
  if (!pcb) {
    /* Out of memory. */
    return -1;
    }
  tcp_arg(pcb, netif); 
  tcp_accept(pcb, application_connection);
  while (1)
  xemacif_input(netif);
  }


大力神输出
 enter image description here

最佳答案

那么,这是Xilinx forums讨论的延续吗?

itoa()函数将无符号整数(存储在int中)转换为缓冲区buf中的前30个左右字符。

recv_callback()函数几乎没有意义。

aurora_rx_main()的调用记录为“功能调用”,它没有帮助(因为我们不知道它的作用),甚至其返回值也被完全忽略。

第一个for循环将u32中前100个DestinationBuffer[]的内容转储用于调试目的,因此代码与手头的任务无关。但是,我们不知道谁填充了DestinationBufferaurora_rx_main()调用可能已填充也可能未填充它;我们都没有被告知。

tcp_*()函数似乎遵循Wikia lwIP Wiki中描述的API。)

如果p参数为NULL,则将调用tcp_close(tcpb),然后调用tcp_recv(tcpb, NULL)。这使所有事情变得毫无意义:为什么在关闭后尝试接收任何内容(以及为什么使用NULL参数)?

下一部分同样令人困惑。看起来if测试检查TCP发送缓冲区的大小是否超过1024字节。如果不是,则释放p缓冲区。否则,for循环尝试将u32中的每个DestinationBuffer转换为字符串,然后将该字符串写入TCP缓冲区;但是,它使用常数1而不是适当的api标志,甚至不检查附加到TCP发送缓冲区是否有效。

总而言之,这看起来像一堆复制粘贴的代码,毫无意义。不仅没有必要增加itoa函数中的缓冲区大小(即使将u32转换为int,它也始终可以容纳12个字符(不包括减号或末尾的nul字节,因此使13个字符),但与应该解决的问题完全无关。

根本问题是代码太可怕了。修改它就像将车身填充物放在一块旧口香糖上,以试图“固定”它。正确的解决方法是将所有垃圾代码全部剔除,而改用更好的代码。

编辑:OP声明他们是新程序员,因此,以上注释应作为对所显示代码的直接,诚实的见解,而不是OP本身。让我们看看是否可以帮助OP生成更好的代码。



首先,显示的itoa()功能很愚蠢。假设实际上是将u32_t中的DestinationBuffer作为十进制字符串发送回去,那么最好实现一个辅助函数来执行转换。由于该值前面带有逗号(或其他分隔符),因此我们也可以对其进行琐碎的添加。由于它将使用tcp_write()发送,因此我们将结合以下功能:

err_t tcp_write_u32_string(struct tcp_pcb *pcb,
                           unsigned char   prefix, /* 0 for none */
                           u32_t           value)
{
    /* Because 0 <= u32_t <= 4294967295, the value itself is at most 10 digits long. */
    unsigned char  buf[11]; /* enough room for prefix and value. */
    err_t          result;
    u16_t          len;
    unsigned char *p = buf + sizeof buf;

    /* Construct the value first, from right to left. */
    do {
        /* ASCII encoding: '0' = 48, '1' = 49, ..., '9' = 57. */
        *(--p) = 48 + (value % 10u);
        value /= 10;
    } while (value);

    /* Prepend the prefix, if any. */
    if (prefix)
        *(--p) = prefix;

    /* Calculate the length of this part. */
    len = buf + sizeof buf - p;

    /* If the TCP buffer does not have enough free space, flush it. */
    if (tcp_sendbuf(pcb) < len) {
        result = tcp_output(pcb);
        if (result != ERR_OK)
            return result;
    }

    /* Append the buffer to the TCP send buffer.
       We also assume the packet is not done yet. */
    return tcp_write(pcb, p, len, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
}


这样就可以将指定数组中的num u32_t作为十进制字符串发送,并以换行符结尾,您可以使用

err_t send_list(struct tcp_pcb *pcb,
                const u32_t data[],
                u16_t len)
{
    static const char  newline[2] = { 13, 10 }; /* ASCII \r\n */
    err_t              result;

    if (len > 0) {
        u16_t  i;

        /* The first number has no prefix. */
        result = tcp_write_u32_string(pcb, 0, data[0]);
        if (result != ERR_OK)
            return result;

        /* The following numbers have a comma prefix. */
        for (i = 1; i < len; i++) {
            /* ASCII comma is code 44. (Use 32 for space, or 9 for tab.) */
            result = tcp_write_u32_string(pcb, 44, data[i]);
            if (result != ERR_OK)
                return result;
        }
    }

    /* We add a final newline.
       Note that this one can be referenced,
       and it does complete what we wanted to send thus far. */
    result = tcp_write(pcb, newline, 2, 0);
    if (result)
        return result;

    /* and flush the buffer, so the packet gets sent right now. */
    return tcp_output(pcb);
}


现在,我还没有为Xilinx编写C或根本没有使用lwIP堆栈,因此上述代码是盲目的编写。但是,我非常有信心它会起作用(除非有任何错别字或想法,否则,请在评论中进行举报,然后我将进行验证和修复)。

两个缓冲区(bufnewline)被声明为static,因此尽管它们仅在各自的函数中可见,但它们的值在全局范围内有效。

由于TCP是流协议,因此不必将每个响应都适合单个数据包。除了11个字符(每个数字及其前缀字符)和2个字符(换行符)缓冲区外,您唯一需要的大缓冲区是TCP发送缓冲区(maximum transmission unitmaximum segment size,因为我不是确定lwIP如何在内部使用缓冲区),通常介于536和1518字节之间。

上面的两个函数试图在数字之间分割数据包,但这仅仅是因为比准确地填充每个数据包要容易得多。如果下一个(逗号和)值适合缓冲区,则将其添加到缓冲区。否则,首先刷新缓冲区,然后将下一个(逗号和)值添加到缓冲区。

在接收方,您应该使用例如netcat。 (我不知道Hercules是应用程序还是本地计算机的名称。)由于TCP是流协议,因此接收者无法(可靠地)检测到数据包边界在哪里(不同于UDP数据报)。实际上,TCP连接只是两个数据流,每个数据流都是一种方式,而拆分为数据包只是应用程序程序员无需担心的协议详细信息。对于lwIP,由于它是一个低级别的库,因此需要多加注意,但是从上面的代码可以看出,实际上并没有那么多。



OP在一条评论中解释说,他们不是非常有经验,并且总体目的是让设备接受TCP连接,并通过该连接以无符号32位整数的形式流传输数据(由单独的采集板采集的样本)。

因为我很想拥有其中的一块FPGA板(我可以完成一些任务,看看是否可以卸载到FPGA上),但是却没有足够的资源来获得一块,所以我将在这里概述整个应用。请注意,我唯一需要继续的信息是Xilinx OS和库文档集合(UG643)(PDF)的2018版本。好像OP想要使用原始API来获得高性能。

将样本转换为文本是愚蠢的,尤其是在需要高性能的情况下。我们应该只使用原始二进制文件,以及KC705使用的任何字节序。 (我没有从文档中快速看到它,但是我怀疑它是低端的)。

根据文档,原始API main()与以下内容类似:

int main(void)
{
    /* MAC address. Use an unique one. */
    unsigned char  mac[6] = { 0x00, 0x0A, 0x35, 0x00, 0x01, 0x02 };

    struct netif  *netif = NULL;
    ip_addr_t      ipaddr, netmask, gateway;

    /* Define IP address, netmask, and gateway. */
    IP4_ADDR(&ipaddr,  192, 168,   1,  1);
    IP4_ADDR(&netmask, 255, 255, 255,  0);
    IP4_ADDR(&gateway,   0,   0,   0,  0);

    /* Initialize lwIP networking stack. */
    lwip_init();

    /* Add this networking interface, and make it the default one */
    if (!xemac_add(netif, &ipaddr, &netmask, &gateway, mac, EMAC_BASEADDR)) {
        printf("Error adding network interface\n\r");
        return -1;
    }
    netif_set_default(netif);

    platform_enable_interrupts();

    /* Bring the network interface up (activate it) */
    netif_set_up(netif);

    /* Our application listens on port 7. */
    return application_main(netif, 7);
}


在文档示例中,您会看到对return application_main(netif);的调用,而不是start_application(),然后是一个定期调用xemacif_input(netif)的无限循环。这仅表示application_main()必须定期调用xemacif_input(netif)才能接收数据。 (lwIP文档说我们还应该定期调用sys_check_timeouts()tcp_tmr()。)

请注意,我省略了错误报告printfs,并且不会从错误中正常恢复,而只是返回(从main());我不确定这是否导致KC705重新启动还是什么。

int application_main(struct netif *netif, unsigned int port)
{
    struct tcp_pcb *pcb;
    err_t           err;

    pcb = tcp_new();
    if (!pcb) {
        /* Out of memory error */
        return -1;
    }

    /* Listen for incoming connections on the specified port. */
    err = tcp_bind(pcb, IP_ADDR_ANY, port);
    if (err != ERR_OK) {
        /* TCP error */
        return -1;
    }
    pcb = tcp_listen_with_backlog(pcb, 1);
    if (!pcb) {
        /* Out of memory. */
        return -1;
    }

    /* The accept callback function gets the network interface
       structure as the extra parameter. */
    tcp_arg(pcb, netif);

    /* For each incoming connection, call application_connection(). */
    tcp_accept(pcb, application_connection);

    /* In the mean time, process incoming data. */
    while (1)
        xemacif_input(netif);
}


对于与端口的每个TCP连接,我们都会调用application_connection()。此功能可设置数据采集板,并在接收者需要的时间内传输数据。

/* How many DAQ samples to process in each batch.
 * Should be around the DAQ FIFO depth or so, I think. */
#define  DAQ_FIFO_DEPTH  128

err_t application_connection(void *arg, struct tcp_pcb *conn, err_t err)
{
    struct netif *netif = arg; /* Because of tcp_arg(, netif). */
    u32_t         data[DAQ_FIFO_DEPTH];
    u32_t         i, n;

    /* Drop the connection if there was an error. */
    if (err != ERR_OK) {
        tcp_abort(conn);
        return ERR_ABRT;
    }

    /* Setup the data aquisition. */
    err = daq_setup();
    if (err != ERR_OK) {
        tcp_abort(conn);
        return ERR_ABRT;
    }

    /* Data acquisition to TCP loop. */
    while (1) {

        /* Keep the networking stack running. */
        xemacif_input(netif);
        tcp_tmr();

        /* Tell the networking stack to output what it can. */
        tcp_output(conn);

        /* Acquire up to DAQ_FIFO_DEPTH samples. */
        n = daq_acquire(data, DAQ_FIFO_DEPTH);
        if (n > DAQ_FIFO_DEPTH)
            break;

        /* Write data as-is to the tcp buffer. */
        if (tcp_write(conn, data, n * sizeof data[0], TCP_WRITE_FLAG_COPY) != ERR_OK)
            break;
    }

    /* Stop data acquisition. */
    daq_close();

    /* Close the TCP connection. */
    if (tcp_close(conn) == ERR_OK)
        return ERR_OK;

    /* Close failed. Abort it, then. */
    tcp_abort(conn);
    return ERR_ABRT;
}


还有另外三个功能要实现:daq_setup(),应设置数据采集和FIFO; daq_acquire(u32_t *data, u32_t count)最多将count个样本存储到data[],并返回实际存储的样本数-最好是先耗尽FIFO,而不是等待新样本到达-最好daq_close(),将停止数据采集。

我相信他们应该是这样的:

XLlFifo         daq_fifo;

err_t daq_setup(void)
{
    XLlFifo_Config *config = NULL;

    config = XLlFifo_LookupConfig(DAQ_FIFO_ID);
    if (!config)
        return ERR_RTE;

    if (XLlFifo_CfgInitialize(&daq_fifo, config, config->BaseAddress) != XST_SUCCESS)
        return ERR_RTE;
}

u32_t daq_acquire(u32_t *data, u32_t max)
{
    u32_t len, have;

    have = XLlFifo_iRxGetLen(&daq_fifo);
    if (have < 1)
        return 0;
    else
    if (have < max)
        max = have;

    for (len = 0; len < max; len++)
        data[len] = XLlFifo_RxGetWork(&daq_fifo);

    return len;
}

err_t daq_close(void)
{
    /* How to stop the FIFO? Do we need to? */
}


就是这样

关于c - LWIP ECHO SERVER:如何增加itoa函数中的缓冲区大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53008415/

相关文章:

c - 获取传递给 C 函数的数组长度

linux-kernel - 如何使用 Nios II MMU 和 Linux 4.9 启用 SD 卡

vhdl - VHDL中奇怪的XNOR行为

自定义 lwip microblaze echo 示例

c - 运行 lwIP 1.3.2 的 stellaris 板上的 Websockets

c - 在树莓派上制作 "not recognized as an internal or external command"

c - 为什么在使用函数时无法正确添加到链表?

c - 在只读数据库上获取 SQLITE_BUSY 是否正常?

arrays - Modelsim VHDL 阵列初始化发出警告 (vcom-1320)