Jump to content
  • 0

Waveforms - Scripting - TCP Server doesn't detect disconnect, reconnect not possible


Jonathan MB

Question

I'm trying to create a simple waveforms script TCP REPL to remote control the Waveforms software to allow some external automation of I2C while still being able to use other parts of Waveforms software like scope. It reads data from the socket until it receives a "\n", evaluates the line and sends the return values in JSON format. This part works very well, I can connect to it and get correct responses. However, if I disconnect the client, server.isConnected() still shows true, despite there being no connection. The server port remains open and I can reconnect to it, however no data is being transmitted or received. Is there another way to detect connection loss or automatically allow reconnecting?

Thanks!

This is the code so far, it never reaches the print("Not Connected").

var server = Tcp();
var text = "";
var delimiter = "\n";

if(!server.listen("127.0.0.1", 50000)) throw "server error";

while(wait(0.01)){
    if(!server.isConnected()){
        print("Not Connected");
        server.listen("127.0.0.1", 50000);
        while(wait(0.01)){
            server.waitForNewConnection(0.1);
            if(server.isConnected()){break;}
        }
        print("New Connection");
    }
    recv = server.readText();
    if(!recv)
        {continue;}
    text += recv;
    var delimiterIndex = 0;

    while((delimiterIndex = text.indexOf(delimiter)) !== -1) {
        const line = text.slice(0, delimiterIndex);
        text = text.slice(delimiterIndex + delimiter.length);
        try {
            var resp = eval(line);
        } catch (e) {
            var resp = e;
        }
        if(server.isConnected()){
            server.writeText(JSON.stringify(resp)+"\n");
        }
    }
}
print("Disconnect");
server.disconnect();
server.close();

 

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...