TCP receive function problem

I have the following function:

function readMessage() {
    try {        
        var message = socket.receive({timeout: 500});
        console.log("SSP800 received: " + message);
        return message;
    }
    catch(err) {
        console.log("SSP800 receive error: " + err);
        return "";
   }

console.log(“SSP800 receive: nothing”);
return “”;
}

I’m able to send() to this socket, but when I call this function, none of the console.log entries are written to log, so I’m not sure I’ve done the try correctly. Removing the timeout does not change the behavior other than blocking the second time its called.

Can you temporarily change it to this?

I know the console log is very strict on requiring string parameters. I’m not certain “err” is a string. That may be an object. If it’s an object your code might not run.

Since you are having issues, it’s also probably a good idea to add a log statement at the top of the routine. Something that you know will definitely run.

function readMessage() {
   console.log("reading message");
    try {        
        var message = socket.receive({timeout: 500});
        console.log("SSP800 received: " + message);
        return message;
    }
    catch(err) {
        console.log("SSP800 receive error: ");
        return "";
   }
console.log("SSP800 receive: nothing");
return "";
}

… I notice too that your code snippet is using non-standard quotes at the bottom of your routine. Make sure your actual code is using standard quotes. Guessing that might just be a formatting issue in the forum, not sure. But that’s also something to look at. Simply having those non-standard quotes could be breaking your entire script.

Thanks Bill. I tried removing the “+ Err” from the exception handler and nothing changed.
Any caller that calls readMessage() does not resume execution.
I changed it to the following and the behavior remains the same:

function readMessage() {
var message = “”;
try {
var message = socket.receive({timeout: 500});
console.log("SSP800 received: " + message);
return message;
}
catch(err)
{
console.log("SSP800 receive error: ");
return message;
}
console.log(“SSP800 receive: nothing”);
return message;

PS. How do you put a code snippet in a reply so that it is formatted correctly? (The quote issue was the formatter not displaying quotes properly)

That’s not the code I shared with you. It’s missing several of the changes that I recommended you make. Please use that code snippet I wrote for you.

Here’s how to embed code in a post.

Using your code results in “reading message” being the only thing written to the console log. The system still responds to other commands.

I forgot. There’s an issue with timeouts for the receive function. Can you use the solution provided here?

That solution doesn’t receive any data – perhaps because it’s not waiting long enough for some data to arrive? I couldn’t work out how to add a sleep in the plugin because I assume that it might take a few ms for the device to respond to the command.
Or perhaps the device isn’t responding at all, or there’s a problem with the tx->rx cable (it’s connected via two DB9->RJ45 adapters over Cat5 cable). The device IS receiving the commands ok because it’s performing the actions expected.
It’s not a big deal though, I’m expecting to replace this device with another Anthem unit in the next 6-9 months, so making it a one-way communication pathway until then is giving me enough functionality at least to control it.

Use the “sleep” function to add a delay. The following code will force a 1 second delay (1000 ms).

sleep(1000);

Thanks, should’ve known better and tried a lowercase “s” for sleep.