open-tcp

xod/w5500/open-tcp

Open a TCP connection to the specified server
open-tcp
@/open-tcp
Open a TCP connection to the specified server
INET@/w5500-inet
An internet connection
SRVstring
Server name
PORTnumber
A destination server port number
CONNpulse
Open the connection
open-tcp
SOCK
DONE
ERR
INET
SRV
PORT
CONN
ERRpulse
Pulses when opening failed
DONEpulse
Pulses when the connection is successfully open
SOCKxod/net/socket
The socket connected

C++ implementation

{{#global}}
#include <SPI.h>
#include <Ethernet2.h>
{{/global}}

struct State {
};

{{ GENERATED_CODE }}

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

    auto client = EthernetClient();
    auto serverName = getValue<input_SRV>(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 {
        emitValue<output_ERR>(ctx, 1);
    }

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