我正在尝试将数据从MIMXRT1062发送到PC,并且已设置UDP Echo Server。 代码如下:
struct netconn *conn;
struct netbuf *buf_rcv;
char buffer[200];
conn = netconn_new(NETCONN_UDP);
netconn_bind(conn, IP_ADDR_ANY, 9007);
LWIP_ERROR("udpecho: invalid conn", (conn != NULL), return;);
while (1) {
err = netconn_recv(conn, &buf_rcv);
printf("address: %d\n port: %d\n",netbuf_fromaddr(buf_rcv),netbuf_fromport(buf_rcv));
if (err == ERR_OK) {
//no need netconn_connect here, since the netbuf contains the address
if(netbuf_copy(buf_rcv, buffer, sizeof(buffer)) != buf_rcv->p->tot_len) {
printf("netbuf_copy failed\n");
} else {
buffer[buf_rcv->p->tot_len] = '\0';
printf("Received: %s\r\n",buffer);
err = netconn_send(conn, buf_rcv);
if(err != ERR_OK) {
LWIP_DEBUGF(LWIP_DBG_ON, ("netconn_send failed: %d\n", (int)err));
printf("netconn_send failed!\r\n");
} else {
LWIP_DEBUGF(LWIP_DBG_ON, ("got %s\n", buffer));
}
}
netbuf_delete(buf_rcv);
}
我已使用此代码成功将数据回显到PC。但是我希望能够在调试期间查看数据,但是当我尝试
时printf("Received: %s\r\n",buffer);
不打印数据。作为附带说明,我最终想将数据发送到PC而不接收数据,我尝试了以下代码:
ip_addr_t ipaddr;
conn = netconn_new(NETCONN_UDP);
netconn_connect(conn, IP_ADDR_ANY, 9007);
while(1){
buf = netbuf_new();
netbuf_ref(buf,buffer, sizeof(buffer));
if(netconn_send(conn, buf)==ERR_OK){
printf("Sending %s\r\n",buffer);
}
else{
printf("Sending failed\r\n");
}
netbuf_delete(buf);
}
netconn_close(conn);
netconn_delete(conn);
代码只是echo的修改版本。回显有效,但我无法显示正在回显的数据,而在其他版本中,PC端看不到已发送的数据。 我不确定自己在做什么错。帮助将不胜感激。谢谢!