Hello,
I work with the microcontroller SAM3X8E and I develop the program with Atmel Studio 7.
I am a beginner of FreeRTOS-Plus6TCP. I want to send some data from my Computer to my programming board thanks to ethernet bus TCP/IP.
To do that I have include in the program of my programming board FreeRTOS-Plus-TCP (v202012.01). For the computer I use a python program with python socket library.
I can ping my card but the issue is that when I try to connect the socket of my computer (the client) to the socket of my programming board (the server) I have timeout error…
This is the program of my computer :
import socket
IP_PORT = 5050
BAUD_RATE = int(115200)
arduino_IP_address = "192.168.1.40" //It's the IP adress of my programming board (I can ping my card with this IP)
try
if client is None:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.settimeout(5)
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
client.connect((arduino_IP_address, IP_PORT))
except OSError as err:
print(err)
This is what I get when I launch this program :
This is the program of my programming board :
void task_ethernet(void *param)
{
//Wait for the IP-stack run one time
vTaskDelay(pdMS_TO_TICKS(10));
/* Variables */
Socket_t xListeningSocket, xConnectedSocket;
static const TickType_t xReceiveTimeOut = 200;
struct freertos_sockaddr xBindAddress, xClient;
socklen_t xSize = sizeof(xClient);
const BaseType_t xBacklog = 20;
/* Creation + check of the socket */
xListeningSocket = FreeRTOS_socket(FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP);
if(xListeningSocket == FREERTOS_INVALID_SOCKET){
printf("problem with the socket \r\n");
return;
}
/* Set a time out so accept() will just wait for a connection. */
FreeRTOS_setsockopt(xListeningSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ));
/* Set of the IP port */
xBindAddress.sin_port = (uint16_t) 5050;
xBindAddress.sin_port = FreeRTOS_htons(xBindAddress.sin_port);
/* Bind the socket to the port that the client RTOS task will send to. */
if(FreeRTOS_bind(xListeningSocket, &xBindAddress, sizeof( xBindAddress )) < 0){
printf("problem with the bind of the socket \r\n");
return;
}
/* Set the socket into a listening state so it can accept connections. */
if(FreeRTOS_listen(xListeningSocket, xBacklog) < 0){
printf("problem with the listen mode of the socket \r\n");
return;
}
printf("Socket ready \r\n");
static char cRxedData[BUFFER_SIZE];
BaseType_t lBytesReceived;
uint32_t ul_frm_size = 0;
while(1)
{
/* To ping my card */
if (emac_dev_read(&gs_emac_dev, (uint8_t *) gs_uc_eth_buffer, sizeof(gs_uc_eth_buffer), &ul_frm_size) == EMAC_OK) {
emac_process_eth_packet((uint8_t *) gs_uc_eth_buffer, ul_frm_size);
}
printf("Start");
/* Wait for incoming connections. */
xConnectedSocket = FreeRTOS_accept(xListeningSocket, &xClient, &xSize);
printf("End \r\n");
if(xConnectedSocket == FREERTOS_INVALID_SOCKET || xConnectedSocket == NULL){
continue;
}
else{
printf("connected \r\n");
}
vTaskDelay(pdMS_TO_TICKS(200));
}//End while(1)
}
int main(void)
{
/* Initialize interrupts */
irq_initialize_vectors();
cpu_irq_enable();
/* Initialize the SAM system */
sysclk_init();
/* Setup of the I/Os, DAC and ADC PINs */
board_init();
/* Initialize the console uart */
configure_console();
/* Print of basic informations in the consol uart */
puts(STRING_HEADER);
/* Setup Ethernet */
init_eth();
FreeRTOS_IPInit(gs_uc_ip_address, gs_uc_netmask, gs_uc_gateway_address, gs_uc_DNS_address, gs_uc_mac_address);
/* Tasks */
xTaskCreate(task_ethernet, "Ethernet_task", 2500, NULL, 4, &point_task_eth);
//Wait for the cable ethernet to be plug
while(ethernet_phy_auto_negotiate(EMAC, BOARD_EMAC_PHY_ADDR) != EMAC_OK);
while(ethernet_phy_set_link(EMAC, BOARD_EMAC_PHY_ADDR, 1) != EMAC_OK);
/* Scheduler */
vTaskStartScheduler();
}
The socket is well create but the FreeRTOS_accept(…) always return me a NULL socket.
I put somes printf in this function and the way follows is always the same :
Socket_t FreeRTOS_accept( Socket_t xServerSocket, struct freertos_sockaddr * pxAddress, socklen_t * pxAddressLength )
{
FreeRTOS_Socket_t * pxSocket = ( FreeRTOS_Socket_t * ) xServerSocket;
FreeRTOS_Socket_t * pxClientSocket = NULL;
TickType_t xRemainingTime;
BaseType_t xTimed = pdFALSE, xAsk = pdFALSE;
TimeOut_t xTimeOut;
IPStackEvent_t xAskEvent;
if( prvValidSocket( pxSocket, FREERTOS_IPPROTO_TCP, pdTRUE ) == pdFALSE )
{
/* Not a valid socket or wrong type */
pxClientSocket = FREERTOS_INVALID_SOCKET;
}
else if( ( pxSocket->u.xTCP.bits.bReuseSocket == pdFALSE_UNSIGNED ) &&
( pxSocket->u.xTCP.ucTCPState != ( uint8_t ) eTCP_LISTEN ) )
{
/* Parent socket is not in listening mode */
pxClientSocket = FREERTOS_INVALID_SOCKET;
}
else
{
/* Loop will stop with breaks. */
for( ; ; )
{
/* Is there a new client? */
vTaskSuspendAll();
{
printf("lol");
if( pxSocket->u.xTCP.bits.bReuseSocket == pdFALSE_UNSIGNED )
{
printf("lol2_1");
pxClientSocket = pxSocket->u.xTCP.pxPeerSocket;
}
else
{
printf("lol2_2");
pxClientSocket = pxSocket;
}
printf("lol3");
if( pxClientSocket != NULL )
{
printf("lol4");
pxSocket->u.xTCP.pxPeerSocket = NULL;
/* Is it still not taken ? */
if( pxClientSocket->u.xTCP.bits.bPassAccept != pdFALSE_UNSIGNED )
{
printf("lol5_1");
pxClientSocket->u.xTCP.bits.bPassAccept = pdFALSE;
}
else
{
printf("lol5_2");
pxClientSocket = NULL;
}
}
printf("lol6");
}
xTaskResumeAll();
printf("lol7");
if( pxClientSocket != NULL )
{
printf("lol8");
if( pxAddress != NULL )
{
/* IP address of remote machine. */
pxAddress->sin_addr = FreeRTOS_ntohl( pxClientSocket->u.xTCP.ulRemoteIP );
/* Port on remote machine. */
pxAddress->sin_port = FreeRTOS_ntohs( pxClientSocket->u.xTCP.usRemotePort );
}
if( pxAddressLength != NULL )
{
*pxAddressLength = sizeof( *pxAddress );
}
if( pxSocket->u.xTCP.bits.bReuseSocket == pdFALSE_UNSIGNED )
{
xAsk = pdTRUE;
}
}
printf("lol9");
if( xAsk != pdFALSE )
{
/* Ask to set an event in 'xEventGroup' as soon as a new
* client gets connected for this listening socket. */
xAskEvent.eEventType = eTCPAcceptEvent;
xAskEvent.pvData = pxSocket;
( void ) xSendEventStructToIPTask( &xAskEvent, portMAX_DELAY );
}
printf("lol10");
if( pxClientSocket != NULL )
{
printf("lol11");
break;
}
if( xTimed == pdFALSE )
{
printf("lol12");
/* Only in the first round, check for non-blocking */
xRemainingTime = pxSocket->xReceiveBlockTime;
if( xRemainingTime == ( TickType_t ) 0 )
{
printf("lol13");
break;
}
/* Don't get here a second time */
xTimed = pdTRUE;
/* Fetch the current time */
vTaskSetTimeOutState( &xTimeOut );
}
printf("lol14");
/* Has the timeout been reached? */
if( xTaskCheckForTimeOut( &xTimeOut, &xRemainingTime ) != pdFALSE )
{
printf("lol15");
break;
}
printf("lol16");
/* Go sleeping until we get any down-stream event */
( void ) xEventGroupWaitBits( pxSocket->xEventGroup, ( EventBits_t ) eSOCKET_ACCEPT, pdTRUE /*xClearOnExit*/, pdFALSE /*xWaitAllBits*/, xRemainingTime );
printf("lol17");
}
}
printf("lol18");
return pxClientSocket;
}
I always got this (even when I launch my computer program to connect to my programming board) :
Socket ready (mean the socket is well create)
Start (just befor the FreeRTOS_accept(…) in my ethernet_task)
lol lol2_1 lol3 lol6 lol7 lol9 lol10 lol12 lol14 lol16 lol17
lol lol2_1 lol3 lol6 lol7 lol9 lol10 lol14 lol15 lol18
End (after the FreeRTOS_accept(…) in my ethernet_task)
Do you have any idea of the problem ?