Несколько подключений к одному порту с использованием netconn - PullRequest
0 голосов
/ 10 апреля 2019

Попытка разработки ведомого приложения MODBUS через STM32H743BI с использованием LwIP 2.0.3 и FreeRTOS 9.0.0

Мое приложение должно иметь возможность обрабатывать несколько клиентов одновременно.Я хочу создать отдельные потоки для каждого принятого соединения и убить себя, если больше не нужен, как вы видите ниже:

void modbus_server_netconn_thread(void *arg)
{
  struct netconn *conn = NULL, *newconn = NULL;
  err_t err, accept_err;


  osThreadDef(MBParticular, modbus_handler, osPriorityNormal, 8, configMINIMAL_STACK_SIZE *4);

  /* Create a new TCP connection handle */

  conn = netconn_new(NETCONN_TCP);

  if (conn!= NULL)
  {
    /* Bind to port 502 with default IP address */

    err = netconn_bind(conn, NULL, 502);

    if (err == ERR_OK)
    {
      /* Put the connection into LISTEN state */

      netconn_listen(conn);

      while(1)
      {
        /* accept any incoming connection */

        accept_err = netconn_accept(conn, &newconn);

        if(accept_err == ERR_OK)
        {
            osThreadCreate (osThread(MBParticular), (void *)newconn);

//          /* delete connection */
//          netconn_delete(newconn);  //if I put this here, no data can be rcved
            newconn = NULL;
        }
        else netconn_delete(newconn);
      }
    }
  }
}

static void modbus_handler(void const *arg)
{
  struct netconn *conn = (struct netconn *)arg;
  err_t recv_err;
  u16_t buflen;
  char* buf;
  struct pbuf *p = NULL;
  struct netbuf *inbuf;

  while(TRUE){

  /* Read the data from the port, blocking if nothing yet there.
   We assume the request (the part we care about) is in one netbuf */

  recv_err = netconn_recv(conn, &inbuf);

  if (recv_err == ERR_OK)
  {
    if (netconn_err(conn) == ERR_OK)
    {
      netbuf_data(inbuf, (void**)&buf, &buflen);

    //    HANDLE PACKET

    }
  }

  /* Delete the buffer (netconn_recv gives us ownership, so we have to make sure to deallocate the buffer) */

  netbuf_delete(inbuf);

  }

  /* Close the connection */
  netconn_delete(conn);
  osThreadTerminate(NULL);
}

Но не может подключиться не более 1. С другой стороны, если программа MODBUS Masterотключен, он не может подключиться снова.netconn_accept возвращает ERR_ABRT в обеих ситуациях.

Это часть моего lwipopts.h:

#define MEMP_NUM_NETCONN        30
#define MEMP_NUM_NETBUF         30
#define MEMP_NUM_PBUF           60
#define MEMP_NUM_UDP_PCB        6
#define MEMP_NUM_TCP_PCB        10
#define MEMP_NUM_TCP_PCB_LISTEN 8
#define MEMP_NUM_TCP_SEG        8
#define MEMP_NUM_SYS_TIMEOUT    10

Я мог бы сделать основную ошибку.Простите, если это напрасно тратит ваше время.Спасибо!

...