už delší dobu zápasím s UDP na mcu. Uniká mi něco a ne a ne na to přijít.
Kód: Vybrať všetko
// Define a FreeRTOS queue handle
QueueHandle_t SendData_queue;
static struct netconn *conn;
static struct netbuf *buf;
static struct pbuf *txBuf;
static ip_addr_t *addr;
static unsigned short port;
char receiveBuffer[MAX_BUFFER_SIZE];
static void vUDPnetconnTask(void *pvParameters) {
err_t err, recv_err;
/* Create a new connection identifier */
conn = netconn_new(NETCONN_UDP);
Send_FEKU_Data sendData;
if (conn != NULL) {
/* Bind connection to the port 7 */
err = netconn_bind(conn, IP_ADDR_ANY, 50055);
if (err == ERR_OK) {
/* The while loop will run everytime this Task is executed */
while (1) {
/* Receive the data from the connection */
recv_err = netconn_recv(conn, &buf);
if (recv_err == ERR_OK) { // if the data is received
addr = netbuf_fromaddr(buf); // get the address of the client
port = netbuf_fromport(buf); // get the Port of the client
uint16_t len = buf->p->len > MAX_BUFFER_SIZE ? MAX_BUFFER_SIZE : buf->p->len;
void *data;
netbuf_data(buf, &data, &len);
memcpy(receiveBuffer, data, len);
printf("Received data: ");
for (uint8_t i = 0; i < len; i++) {
print("%02x ", receiveBuffer[i]);
}
// Here work with receive data
netbuf_delete(buf); // delete the netbuf
}
// Wait for a message on the queue
BaseType_t queueStatus;
queueStatus = xQueueReceive(SendData_queue, &sendData, portMAX_DELAY);
if (queueStatus == pdPASS) {
// Process the received data and send the response
txBuf = pbuf_alloc(PBUF_TRANSPORT, sendData.Len + 2, PBUF_RAM);
if (txBuf != NULL) {
uint8_t sendArray[100];
sendArray[0] = sendData.ID;
sendArray[1] = sendData.Len;
sendArray[2] = sendData.Data[0];
sendArray[3] = sendData.Data[1];
sendArray[4] = sendData.Data[2];
sendArray[5] = sendData.Data[3];
pbuf_take(txBuf, sendArray, sendData.Len + 2);
// In a real application, you might need to get the destination address and port
// from the message or another source
buf->p = txBuf;
netconn_connect(conn, addr, port); // connect to the destination address and port
netconn_send(conn, buf);
netbuf_delete(buf); // delete the netbuf
pbuf_free(txBuf); // clear the pbuf
}
}
}
} else {
netconn_delete(conn);
}
}
}
Odešlu data z PC do mcu, kde je zpracuji. Následně se v jiném tasku vygenerují odchozí data. Na to mám frontu SendData_queue. Jakmile se v ní objeví data, tak se mají odeslat po UDP zpět do PC.
Data se odešlou, ale dostávám chybu:
FAIL-A: "pbuf_free: p->ref > 0" on 752 in ERA-FW/MP-ETH-STM32-LwIP/MP/src/core/pbuf.c
Kde to vidím na něco prohnilého zde:
Kód: Vybrať všetko
pbuf_take(txBuf, sendArray, sendData.Len + 2);
// In a real application, you might need to get the destination address and port
// from the message or another source
buf->p = txBuf;
netconn_connect(conn, addr, port); // connect to the destination address and port
netconn_send(conn, buf);
netbuf_delete(buf); // delete the netbuf
pbuf_free(txBuf); // clear the pbuf