9 years, 7 months ago.

Problem with TCP Client over Wifly

Hi everyone, i have a problem with making a tcp client in mbed using David Smart's Wifly library. When i sent a char array with the send command first i receive an exit string (that i never sent). Looking in the DEBUG mode of the WIFLY library, it seems that is tooking too long to open the connection to the server and then the enter cmd mode fails.

Please help me

Greetings

Ney

1 Answer

9 years, 7 months ago.

Hi Ney,

I've found different Wifly modules (most likely related to the firmware version) have different timing characteristics for the commands. In the Wifly driver file, you'll find "FlushIn()" called, which clears any leftover characters. I recently found a few stray characters were not flushed before transitioning the interface from command mode back to data. The default amount of time to wait is 1 ms, but increasing it to 5 solved it (for me). In an attempt to reduce any unnecessary delays, I guess I took it too far.

int Wifly::send(const char * str, int len, const char * ACK, char * res, int timeout)
{
...
                if (tolower(read) == tolower(ACK[ackIndex])) {
                    ackIndex++;
                    if (ackIndex == strlen(ACK)) {
                        //flushIn();
                        flushIn(5);     // this small delay let's is remove trailing parts of a command
                        break;
                    }
                }

I don't know if this is the same place that it interfering with your application, but it might be the easy thing to try first.

Hi David i saw that when i open a tcp connection and then sent a string the command mode exit automatically. I deactivate this two lines in your code:

bool Wifly::connect(const char * host, int port)
{
    char rcv[20];
    char cmd[20];

    // try to open
    sprintf(cmd, "open %s %d\r", host, port);
    if (sendCommand(cmd, "OPEN", NULL, 10000)) {
        setConnectionState(true);
        //exit();
        return true;
    }

    // if failed, retry and parse the response
    if (sendCommand(cmd, NULL, rcv, 5000)) {
        if (strstr(rcv, "OPEN") == NULL) {
            if (strstr(rcv, "Connected") != NULL) {
                if (!sendCommand("close\r", "CLOS"))
                    return false;
                if (!sendCommand(cmd, "OPEN", NULL, 10000))
                    return false;
            } else {
                return false;
            }
        }
    } else {
        return false;
    }

    setConnectionState(true);
    //exit();
    return true;
}

And then i resolve the problem of strange strings (blank and exit string)

I will try your solution

Greetings

posted by Ney Palma 25 Sep 2014

Hi Ney, Do you have a small sample you can share that shows how the change in connect(...) is solved with the removal of exit()? I'd like to replicate that and do a bit of testing. Thanks.

posted by David Smart 26 Sep 2014

Hi David, i will try to send you the example in this days.

Greetings

posted by Ney Palma 02 Oct 2014