cancel
Showing results for 
Search instead for 
Did you mean: 

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

TARHAN SAMAH
Senior
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.
15 REPLIES 15
TARHAN SAMAH
Senior
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 ? 

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 */

}
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

Posted on April 27, 2018 at 17:20

i mean 

,

local_port number=60 

remote_port=61 for the first device 

local ip    10.1.1.60

remote ip 10.1.1.89

local_port number=70 

remote_port=71 for the first device 

local ip    10.1.1.70

remote ip 10.1.1.89

Jack Peacock
Associate II
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

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

Posted on April 30, 2018 at 08:42

helllo for Mister kumar :

if i have my stm 

i mean 

,

remote_port=61 for the first device 

local ip    10.1.1.60 local_port number=60  for example --->of my stm  and 

remote ip 10.1.1.89--->is my pc you can fix it if you want static by parameters .

you need to make gate way 10.1.1.1 and mask 255.255.255.0 

on both pc and device this is my pc config it is my host for my application 

0690X0000060AooQAE.png

you can use this code for udp , in cube you fix the ip as static 10.1.1.60 of our stm 

0690X0000060ApXQAU.png

then you use this code :dot for get receive and sending port should be different as i understood ,and not used by another application :

put this in a thread :

void StartDefaultTask(void const * argument)// thread just for receive because netconn_recv blokes untill data comes 

 

{

/* init code for LWIP */

MX_LWIP_Init();

/* USER CODE BEGIN 5 */

printf(''ETHERNET STM32F429ZI lwip init complete\r\n'');

while(gnetif.ip_addr.addr==0) osDelay(1);//wait the ip to reach the structure

printf(''OUR Static ip adress:%s\r\n'',ip4addr_ntoa(&gnetif.ip_addr));

local_ip =gnetif.ip_addr;

IP4_ADDR(&remote_ip,remip1, remip2, remip3,remip4);

printf(''OUR remote ip adress:%s\r\n'',ip4addr_ntoa(&remote_ip));

printf(''remote port=%d-'',remote_port);

//LWIP_UNUSED_ARG(conn); //looks not necessary

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

if ((netconn_bind(conn,&local_ip,60))== ERR_OK)//bind to local ip 10.1.1.60  

{

netconn_connect(conn,&remote_ip,61);//connect to a remote pc destinaton ip--->10.1.1.89   

/* Infinite loop */

while (1)

{

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

{//data received/* receive data until the other host closes the connection */

do

{

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

len = netbuf_len(buf_rx);

printf(''\n\r received frame lenght=%d\n\r'',len);

netbuf_copy(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();

// char ack_text[] =''ack from stm\n\r'';

// netbuf_ref(buf_tx,ack_text, sizeof(ack_text));

// 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

}

}

osDelay(1);

/* USER CODE END 5 */

}

for example i added another thread for sending with button :

void StartCommunication_Task(void const * argument)

 

{

 

/* USER CODE BEGIN StartCommunication_Task */

for(;;)

{

//*******************************************button**************************************//

if(HAL_GPIO_ReadPin(GPIOC, BUTTON_Pin)==1)//USER BUTTON PUSHED send a text udp to the host 

{

while(HAL_GPIO_ReadPin(GPIOC, BUTTON_Pin)==1)

//////***********************send udp data to ethernet**********************************//

sprintf(buffer,''button udp from stm\n\r'');

struct netbuf* buf_tx = netbuf_new();

netbuf_ref(buf_tx,buffer, sizeof(buffer));

netconn_send(conn, buf_tx);

netbuf_delete(buf_tx);//delete the sending buffer

////********************************************************************************//

}

HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14);//blink a led on the cart

HAL_Delay(100);

osDelay(1);

}

/* USER CODE END StartCommunication_Task */

}

i tested on hercules udp :

0690X0000060ApYQAU.png

good luck hope it will work for you 

Posted on April 30, 2018 at 09:06

hi

doumandjisamah

its all looking good please send me declaration and definition of above code to understand me very clearly.

thanks for your all replies.

Posted on April 30, 2018 at 10:09

hi

doumandjisamah

do you declare local_ip as

uint8_t local_ip[4];

OR

ip4_addr_t local_ip;

?