Skip to main content
RMG
Associate
July 1, 2020
Question

Creating 2 TCP threads

  • July 1, 2020
  • 1 reply
  • 1379 views

Hi

I am trying to create two TCP threads for my application.these packets are intended to control two VFDs which has two IP addresses(192.168.10.4 and 192.168.10.5) but same port number(502- modbus TCP).I am able to send packets to both the servers simultaneously. the VFDs gives me encoder data embedded in the TCP packets. when i try to capture the encoder data, i found that the data received in the netbuf is getting mixed up . I mean encoder 1 is displayed in encoder2 position in HMI and vice versa. communication to HMI is UDP. i call the udp transmit function in the respective threads itself.i have given seperate names for the netbuf and all the other buffers. i am clearing netbuf in both the threads.looks as if both the threads are trying to write tha same netbuf. how to resolve this issue. is there any problem in doing 2 TCP threads in LWIP. i have given priorities above normal to one thread and normal to the other one. if i comment the netconn_recv of one thread the other thread will work perfectly and i am getting the correct encoder data. whereas if i comment the UDP tranmission to HMI , i can see the data swtching. that's why i assume that the netbuf is getting updated simultaneously. please help. i am stuck with this issue for the last few days

the relevant portions of the code is attached

thanks in advance

RMG

This topic has been closed for replies.

1 reply

TDK
July 2, 2020

A lot of those functions return an err_t value which can indicate an error. I'd start with checking for that. Seems like the memory pool is getting okay.

Putting code within a code block makes it much easier to read compared to a picture of code.

"If you feel a post has answered your question, please click ""Accept as Solution""."
RMG
RMGAuthor
Associate
July 2, 2020

I am attaching the edited code. though i have given names to err_t vales i am not actually using all of them. can i assign memory location for netbuf. even though i have assigned different names for the netbufs in the threads it looks like both the threads updating the same netbuf. i tried netbuf_delete and netbuf_free. but still the issue is there. please help

Main.c
/* Init thread */
 osThreadDef(Start, StartThread, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 4);
 osThreadCreate (osThread(Start), NULL);
 /* Start scheduler */
 osKernelStart();
 
 for( ;; );
}
 
static void StartThread(void const * argument)
{ 
 /* Create tcp_ip stack thread */
 tcpip_init(NULL, NULL);
 
 /* Initialize the LwIP stack */
 Netif_Config();
 
 /* Initialize webserver demo */
		
 mm_thread_init();
 sp_thread_init();
	
	
 for( ;; )
 {
 /* Delete the Init Thread */ 
 osThreadTerminate(NULL);
 }
}
SP thread init function
 
 
void sp_vfd_init(void)
{
	struct netbuf *inbuf_sp;
 err_t recv_err_sp;
	char* buf_sp;	
	u16_t buflen_sp;	
	struct ip4_addr iDestAddr;
 	 struct netconn *conn_sp;
	conn_sp = netconn_new(NETCONN_TCP);
	IP_ADDR4(&iDestAddr,192,168,10,5);
		if ( ERR_OK == netconn_connect(conn_sp, &iDestAddr, 502))
			{
				netconn_write(conn_sp, data3, 19, NETCONN_COPY); 			
			
		 while ((recv_err_sp = netconn_recv(conn_sp, &inbuf_sp)) == ERR_OK)
				 {
					do{
					netbuf_data(inbuf_sp, (void**)&buf_sp, &buflen_sp);								
						if (buf_sp[5]==0X09 && buflen_sp == 15)
						{
					
				
						netbuf_copy( inbuf_sp,data_buff_sp,inbuf_sp->p->tot_len);
						data_buff_sp[inbuf_sp->p->tot_len]='\0';
						data_buff_sp[17]= 0x55;
						udp_client_send_sp();
						}
	else
	if ( buf_sp[7]==0X10 && buf_sp[9]==0X04 && buf_sp[11]==0X03 && buflen_sp == 0X0C)	 
 {
	 netconn_write(conn_sp, data4, 12, NETCONN_COPY);
	}
	netconn_write(conn_sp, data3, 19, NETCONN_COPY);
	 vTaskDelay (10);
	}while (netbuf_next(inbuf_sp) >= 0);
	netbuf_delete(inbuf_sp);
 
	}
				
	}
		netconn_close(conn_sp);	
		netconn_delete(conn_sp);	
 
}
////Second thread function 
 
void mm_vfd_init(void)
{
	struct netbuf *inbuf_mm;
 err_t recv_err_mm;
	char* buf_mm;	
	u16_t buflen_mm;	
	struct ip4_addr iDestAddr;
 	struct netconn *conn_mm;
	conn_mm = netconn_new(NETCONN_TCP);
	IP_ADDR4(&iDestAddr,192,168,10,4);
	if ( ERR_OK == netconn_connect(conn_mm, &iDestAddr, 502))
	{
		netconn_write(conn_mm, data6, 19, NETCONN_COPY); 			
 while ((recv_err_mm = netconn_recv(conn_mm, &inbuf_mm)) == ERR_OK)
	 {
		do{
			netbuf_data(inbuf_mm, (void**)&buf_mm, &buflen_mm);								
		 if (buf_mm[5]==0X09 && buflen_mm == 15)
			{
						
			netbuf_copy( inbuf_mm,data_buff_mm,inbuf_mm->p->tot_len);
			data_buff_mm[inbuf_mm->p->tot_len]='\0';
 data_buff_mm[17]= 0x44;
			udp_client_send_mm();
													}
	else
	if ( buf_mm[7]==0X10 && buf_mm[9]==0X04 && buf_mm[11]==0X03 && buflen_mm == 0X0C)	
{
	netconn_write(conn_mm, data4, 12, NETCONN_COPY);
}
	netconn_write(conn_mm, data6, 19, NETCONN_COPY);
	 vTaskDelay (10);
	}while (netbuf_next(inbuf_mm) >= 0);
	netbuf_delete(inbuf_mm);			
	}
				 		
}
		netconn_close(conn_mm);		
		netconn_delete(conn_mm);	
}
 
static void sp_thread(void *arg)
{
 
		while(1)
		{
			udp_client_connect_sp();
			sp_vfd_init();
			udp_remove(upcb_sp);
		}
 
 }
static void mm_thread(void *arg)
{
 
		while(1)
		{
			udp_client_connect_mm();
	 		mm_vfd_init();
			udp_remove(upcb_mm);
		}
 
 }
 
 
void sp_thread_init()
{
 
sys_thread_new("sp_thread", sp_thread, NULL, (configMINIMAL_STACK_SIZE*2), SP_THREAD_PRIO);
}
 
void mm_thread_init()
{
 
 sys_thread_new("mm_thread", mm_thread, NULL, (configMINIMAL_STACK_SIZE*2), MM_THREAD_PRIO);
}