This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP432E401Y: TCP/IP Lwip ECHO example nortos how to send data

Part Number: MSP432E401Y


Hi,

I am making a program that receives TCP packets from ethernet using the lwip library. My code is based off the echo example (nortos). The function is to recieve data from TCP and send it through UART serial and vice versa. I have managed to use the echo example and tweak it to recieve packets, process it and send it through UART however I am having trouble Sending data to the same TCP port using the tcp write function. What would be the simplest way to send a string of data to the same tcp port. The only adjustments I made is to the echo_recv function where I took the incoming packet, processes it and then cleared the buffer.

err_t
echo_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
struct echo_state *es;
err_t ret_err;

LWIP_ASSERT("arg != NULL",arg != NULL);
es = (struct echo_state *)arg;
if (p == NULL)
{
/* remote host closed connection */
es->state = ES_CLOSING;
if(es->p == NULL)
{
/* we're done sending, close it */
echo_close(tpcb, es);
}
else
{
/* we're not done yet */
tcp_sent(tpcb, echo_sent);
echo_send(tpcb, es);
}
ret_err = ERR_OK;
}
else if(err != ERR_OK)
{
/* cleanup, for unkown reason */
if (p != NULL)
{
es->p = NULL;
pbuf_free(p);
}
ret_err = err;
}
else if(es->state == ES_ACCEPTED)
{
/* first data chunk in p->payload */
es->state = ES_RECEIVED;
char string1[10];
memcpy(string1,p->payload, p->len);
int i = 0;

for(i = 0; i <= 10 ; i++){
coordinator.modemManager.buffer.push(string1[i]);

}
tcp_recved(tpcb, p->tot_len);
es->p = NULL;
pbuf_free(p);
ret_err = ERR_OK;


/* store reference to incoming pbuf (chain) */
// es->p = p;
/* install send completion notifier */
// tcp_sent(tpcb, echo_sent);
// echo_send(tpcb, es);
// ret_err = ERR_OK;
}
else if (es->state == ES_RECEIVED)
{
/* read some more data */
if(es->p == NULL)
{
es->p = p;
// tcp_sent(tpcb, echo_sent);
// echo_send(tpcb, es);
char string2[10];
memcpy(string2,p->payload, p->len);
int i = 0;

for(i = 0; i <= 10 ; i++){
coordinator.modemManager.buffer.push(string2[i]);

}
tcp_recved(tpcb, p->tot_len);
es->p = NULL;
pbuf_free(p);
ret_err = ERR_OK;
}
else
{
struct pbuf *ptr;

/* chain pbufs to the end of what we recv'ed previously */
ptr = es->p;
pbuf_chain(ptr,p);
}
ret_err = ERR_OK;
}
else if(es->state == ES_CLOSING)
{
/* odd case, remote side closing twice, trash data */
tcp_recved(tpcb, p->tot_len);
es->p = NULL;
pbuf_free(p);
ret_err = ERR_OK;
}
else
{
/* unkown es->state, trash data */
tcp_recved(tpcb, p->tot_len);
es->p = NULL;
pbuf_free(p);
ret_err = ERR_OK;
}
return ret_err;
}

  • Hi,

    You are saying that having trouble on sending data to the TCP port. Can you block all the other functions and only keep the send data function through TCP and see if it works fine?

    Best regards,

    Cash Hao

  • Hi, 

    so I managed to get the TCP to write data through however It only works inside  the echo accept function, when i place the write function in my main while loop it does not send,

    err_t
    echo_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
    {
    err_t ret_err;
    struct echo_state *es;

    LWIP_UNUSED_ARG(arg);
    LWIP_UNUSED_ARG(err);

    /* commonly observed practive to call tcp_setprio(), why? */
    tcp_setprio(newpcb, TCP_PRIO_MIN);

    es = (struct echo_state *)mem_malloc(sizeof(struct echo_state));
    if (es != NULL)
    {
    es->state = ES_ACCEPTED;
    es->pcb = newpcb;
    es->retries = 0;
    es->p = NULL;
    /* pass newly allocated es to our callbacks */
    tcp_arg(newpcb, es);
    tcp_recv(newpcb, echo_recv);
    tcp_err(newpcb, echo_error);
    tcp_sent(newpcb, echo_sent);

    char modemtest[3] = {'A' , 'C', 'K'};
    char *ptrdata = modemtest;
    tcp_write(newpcb, ptrdata,3, 1);

    ret_err = ERR_OK;
    }
    else
    {
    ret_err = ERR_MEM;
    }
    return ret_err;
    }

  • Yes, i seem to fine the issue, the pcb created from the accept callback function is not the same pcb I am calling to write. I am now getting an issue where my code is going into the default handler when it tries to execute the tcp_write function. It is going in there because tcp write is calling an interrupt that does not have a handler. My main code is c++ so I had to use extern C on my Interrupt Handlers. I was wondering if I have to add extern C to another function in the echo code to allow the Interrupt handlers for the interrupts that tcp_write set to be registered. The startup file with the Interrupt Handlers is in C which is why I have to add extern C.

    Thanks