Skip to main content
TARHAN SAMAH
Senior
April 26, 2018
Question

i need to connect two stm32 udp with a switch to pc

  • April 26, 2018
  • 2 replies
  • 4317 views
Posted on April 26, 2018 at 09:33

hello when i connect two stm32 one ip 1.1.88 port 88 the other one is 1.1.70 port 70 to a with switch device using udp to a remote pc ethernet ip 1.1.89 , if one send data the other stm cannot send ???? separatly they can work very good

on the same time they cannot work ??

do i need a special switch for that ??? which one

thanks

Note: this post was migrated and contained many threaded conversations, some content may be missing.
    This topic has been closed for replies.

    2 replies

    TARHAN SAMAH
    Senior
    April 26, 2018
    Posted on April 26, 2018 at 11:58

    i use these receiving code on each stm :

    conn=netconn_new(NETCONN_UDP);//create new network connection UDP type

    if ((netconn_bind(conn,&local_ip,local_port))== ERR_OK)//bind to local ip port 23 succeed

    {

    netconn_connect(conn,&remote_ip,remote_port);//connect to a remote pc destinaton ip

    /* Infinite loop */

    while (1)

    {

    while (( recv_err = netconn_recv(conn, &buf_rx)) == ERR_OK)/* block until we get an incoming connection */

    {//data received

    do

    {

    //************************receive from ethernet********************************************//

    netbuf_data(buf_rx, &datarx, &len);

    for (i=0;i<len;i++)

    {

    printf('%d-',datarx[i]);

    ETH_store[i]=datarx[i];

    }

    check_build_ethernet_protocol();

    //*************************************send ack to pc **************************************//

    // struct netbuf* buf_tx = netbuf_new();

    // sprintf(buffer,'ack from stm\n\r');

    //

    // void* data2 = netbuf_alloc(buf_tx, strlen(buffer));

    // memcpy (data2, buffer, strlen(buffer));

    //

    // netconn_send(conn, buf_tx);

    // netbuf_delete(buf_tx);//delete the sended data buffer otherwise it will not send again

    }

    while (netbuf_next(buf_rx) >= 0);

    netbuf_delete(buf_rx);//delete the recieved data

    }//data received

    netconn_delete(conn); /* discard connection identifier. */

    }//while1

    }//bind

    else

    {//bind

    netconn_delete(conn);//we coudlnt bind so delete the connection

    }

    it looks a bloking code when there is no msg it blokes the connection or smthg like that ??? so the other stm cannot connect same remote ip on pc ,, can you advice me on what to do ? 

    Pawan Kumar sharma
    Associate II
    April 27, 2018
    Posted on April 27, 2018 at 08:57

    hi TARHAN

    can tell me what is 

    remote_port, local_port number and can u explain me more detail about UDP port number.

    please check my code also,

    void StartDefaultTask(void const * argument)

    {

    /* init code for LWIP */

    MX_LWIP_Init();

    /* USER CODE BEGIN 5 */

    /* conn = netconn_new( NETCONN_UDP );

    netconn_bind(conn,IP_ADDR_ANY, 7); //local port

    netconn_connect(conn, IP_ADDR_BROADCAST, 1235 );

    */

    conn = netconn_new(NETCONN_UDP);

    if(conn!=NULL)

    {

    HAL_UART_Transmit(&huart3,'new\r\n',5,100);

    HAL_Delay(100);

    }

    /* set up the IP address of the remote host */

    addr.addr = htonl(0xC0A86472);

    /* connect the connection to the remote host */

    if(netconn_bind(conn,&addr,23)==ERR_OK)

    {

    HAL_UART_Transmit(&huart3,'bind\r\n',6,100);

    HAL_Delay(100);

    }

    if( netconn_connect(conn,&addr,0)==ERR_OK)

    {

    HAL_UART_Transmit(&huart3,'connect\r\n',9,100);

    HAL_Delay(100);

    }

    /* Infinite loop */

    for(;;)

    {

    /* create a new netbuf */

    HAL_UART_Transmit(&huart3,'default\r\n',9,100);

    HAL_Delay(100);

    buf = netbuf_new();

    if(buf!=NULL)

    {

    HAL_UART_Transmit(&huart3,'netbuf_new\r\n',12,100);

    HAL_Delay(100);

    }

    data = netbuf_alloc(buf, 10);

    if(data!=NULL)

    {

    HAL_UART_Transmit(&huart3,'netbuf_alloc\r\n',14,100);

    HAL_Delay(100);

    }

    /* create some arbitrary data */

    for(i = 0; i < 10; i++)

    data[i] = 'a';

    /* send the arbitrary data */

    if(netconn_send(conn, buf)==ERR_OK)

    {

    HAL_UART_Transmit(&huart3,'sending1\r\n',10,100);

    HAL_Delay(100);

    }

    vTaskDelay( 200 ); //some delay!

    /* reference the text into the netbuf */

    if(netbuf_ref(buf, text, sizeof(text))==ERR_OK)

    {

    HAL_UART_Transmit(&huart3,'netbuf_ref\r\n',12,100);

    HAL_Delay(100);

    }

    /* send the text */

    if(netconn_sendto(conn,buf,&addr,55555)==ERR_OK)

    {

    HAL_UART_Transmit(&huart3,'sending2\r\n',10,100);

    HAL_Delay(100);

    }

    vTaskDelay( 200 ); //some delay!

    /* deallocate connection and netbuf */

    netbuf_delete(buf);

    osDelay(1);

    }

    /* USER CODE END 5 */

    }
    TARHAN SAMAH
    Senior
    April 27, 2018
    Posted on April 27, 2018 at 14:19

    Hello mister

    pawanravib

    , thanks for taking time to answer ,

    yes i have two stm nucleo 144 ,429ZI

    ONE İS 1.1.60 PORT 60-----------------------> dest ip 1.1.89 pc via switch port 61

    SECOND 1.1.70 PORT 70------------------------> dest ip same ip 1.1.89 via switch port 71

    when i use them separtly it works very good i send and receive no bloking ,

    but when i connect both of them to pc via switch device ,, both they can send but at the recieving one if it receive the other stm blockes

    i separate the port for evry stm to avoid any confusion already i use hercules to send udp msgs ,,it doesnt accept .

    PC port 1.1.89

    |

    |

    switch device

    / \

    / \

    stm32F429 device1 stm32F429 device2

    1.1.60 port 60 --->send to port61 1.1.70 port70--->send to port 71

    they have same remote ip but different port at send and at receive why they interfer each other at the receiving i have prblm ???

    does any one tried more than one stm same LAN how can i manage that ,, help needed thanks

    Jack Peacock
    Visitor II
    April 28, 2018
    Posted on April 28, 2018 at 04:57

    Ethernet is a single stream of bits on a wire pair.  If your PC is receiving a message from one STM32, there's no hardware available to process a second stream of bits at the same time.  Unless your switch does a 'store and forward', buffering messages like a router, one of the two STM32s has to wait until the other one finishes.  Does your TCP/IP implementation handle collisions with retry? 

    Typically with UDP, since there's no ACK, you have to look for some response from the target or just send on a periodic basis.  If there's a collision and the message is lost there's no notification, that's the nature of UDP protocol.  Each message stands alone, independent of earlier or later messages.

    A switch moves collisions off the wire, since every connection is point to point.  That doesn't mean you can ignore collisions though.  Too many messages going to the same place, especially without a random backoff between packets, can saturate any network.  It sounds like you've created a miniature DDOS, one STM32 transmitting continuously, no delay between packets, and no time for the second STM32 to send a message.  You might try adding a delay between transmit packets on the STM32s to see if that helps.

    If you need a confirmed arrival then you are better off with TCP.  It has retry and timeouts along with receive acknowledgment.

      Jack Peacock

    TARHAN SAMAH
    Senior
    April 28, 2018
    Posted on April 28, 2018 at 16:07

    Thanks much Mister Jack for replying ,, yes we selected udp in purpose because connections is not condition for our protocol i mean if in the other side pc is not connected or the cable went out my stm keep send the data on the ethernet wire ,, i manage the ackonwlege by my application ,why we do that if cable went off i dont need to connect manually it will automaticly send ,, and we need top insert frame number in the ack , so we decide udp is good for our application ..anyway

    WE SOLVED THE PRBL İT WAS BECAUSE WE USED SAME MAC NUMBER NOW İ DİDNT PAY ATTENTİON İT CAN BLOKE .. NOW İT WORKS VERY GOOD THANKS MUCH FOR ALL

    MİSTER

    pawanravib

    İLL SEND U THE code thanks