Author Topic: Where can fnSendTCP(...) be called?  (Read 9139 times)

Offline mic

  • Newbie
  • *
  • Posts: 2
    • View Profile
Where can fnSendTCP(...) be called?
« on: July 25, 2008, 05:58:02 PM »
Hi,

I try to understand how I can use the fnSendTCP function. Is it possible to use this function outside fnListener(...)?

My problem is following:

I have a TCP request to a client on the serial interface (Gateway functionality). After reception of the TCP frame I call the function fnWritetoMBrtu(). This function posts a message to the serial interface task. This is working.

My fnListener function looks like this:

Code: [Select]
static int fnMBtcpListener(USOCKET Socket, unsigned char ucEvent, unsigned char *ucIp_Data, unsigned short usPortLen)
{
switch(ucEvent)
{
case TCP_EVENT_CONREQ:
case TCP_EVENT_CONNECTED:
case TCP_EVENT_CLOSE:
case TCP_EVENT_ACK:
case TCP_EVENT_ARP_RESOLUTION_FAILED:
case TCP_EVENT_PARTIAL_ACK:
break;
case TCP_EVENT_REGENERATE:
case TCP_EVENT_DATA:
                //Extract MB Header from TCP data
fnExtractMBtcpData(&mbTcpMessageRx, ucIp_Data);
                //Send Message to Serial Interface Task
                fnWriteToMBrtu();

return APP_REJECT_DATA;
break;
case TCP_EVENT_ABORT:
case TCP_EVENT_CLOSED:
fnTCP_Listen(Socket, MODBUS_TCP_PORT, 0);
break;
default:
break;
}
return APP_ACCEPT;
}
   

Now my question: Where do I have to call the fnSendTCP(...) function?

At the moment I don't understand how coming back from the Serial Task to the fnListener(...) function. So I tried to call the fnSendTCP(...) within the serial task.

Code: [Select]

case DO_MB_TCP_TIMEOUT:
mbTcpMessageRx.mbTcpHeader.packetLength = 0x3;
uStrcpy((char*)mbTcpMessageRx.mbTcpData, mbException);
fnSendTCP(mbTcpSocket, (unsigned char *)&mbTcpMessageRx.tcpHeader, 9,
                       TCP_FLAG_PUSH);
uTaskerMonoTimer(TASK_U1K, (DELAY_LIMIT)(1*SEC), DO_TIMEOUT);
break;
   

I don't get the expected TCP response. Can anybody help me?


Regards,

Michael

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3237
    • View Profile
    • uTasker
Re: Where can fnSendTCP(...) be called?
« Reply #1 on: July 25, 2008, 10:22:31 PM »
Hi Michael

I am not sure about the APP_REJECT_DATA in the TCP_EVENT_DATA case of the listener. This will cause the the TCP layer to not send an ACK and cause the other side of the connection to repeat the frame again - do you really want this? Normally the return APP_ACCEPT is used.

As long as you have an active connection, the fnSendTCP() function will send a TCP frame. If you do not have an active connection you will first have to establish one first.

The listener call back is purely used by reception frames so is otherwise independent of transmission. I therefore don't actually see a problem with your transmission code - it is basically fine to send it in the serial task.

Note that if you activate TELNET (and establish a connection to it) you can easily send strings via TCP using fnDebugMsg(); the project either sends debug messages to the UART or else to the TELNET TCP connection - if it is connected. This technique is a little different (it uses buffered TCP and is faster, with the down side of requiring more buffer memory).

Regards

Mark

[PS. I hope that you find a solution since I am traveling for the next 4 days with no Internet possibilities...There are however may users with TCP experience who may offer a helping hand if you still have problems]

Offline mic

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Where can fnSendTCP(...) be called?
« Reply #2 on: July 29, 2008, 06:50:24 AM »
Hi Mark,

Thanks for your input. I changed the acknowledge to APP_ACCEPT and the communication recorded by Ethreal looks quite better.


Regards,

Michael