open-tcp

xod-dev/w5500/open-tcp

Open a TCP connection to the specified server Possible errors: — Can't open TCP connection
open-tcp
@/open-tcp
Open a TCP connection to the specified server Possible errors: — Can't open TCP connection
INET@/w5500-inet
An internet connection
HOSTstring
Server name
PORTnumber
A destination server port number
CONNpulse
Open the connection
open-tcp
INET'
SOCK
DONE
INET
HOST
PORT
CONN
DONEpulse
Pulses when the connection is successfully open
SOCKxod/net/socket
The socket connected
INET'@/w5500-inet
An internet connection
To use the node in your project you should have the xod-dev/w5500 library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

C++ implementation

#pragma XOD evaluate_on_pin disable
#pragma XOD evaluate_on_pin enable input_CONN
#pragma XOD error_raise enable

#include <SPI.h>
#include <Ethernet2.h>

node {
    void evaluate(Context ctx) {
        if (!isInputDirty<input_CONN>(ctx))
            return;

        auto client = EthernetClient();
        auto serverName = getValue<input_HOST>(ctx);
        auto port = getValue<input_PORT>(ctx);

        auto len = length(serverName);
        char serverNameBuff[len + 1];
        dump(serverName, serverNameBuff);
        serverNameBuff[len] = '\0';

        if (client.connect(serverNameBuff, port)) {
            emitValue<output_DONE>(ctx, 1);
        } else {
            raiseError(ctx);
        }

        emitValue<output_SOCK>(ctx, client.getSocketNumber());
        emitValue<output_INETU0027>(ctx, getValue<input_INET>(ctx));
    }
}