Send/receive message via TCP socket

Hello I'm attempting to send ASCII messages to a thermotron8800 controller via the script console calling the following gateway script. I keep getting a 'failed to connect connection refused error when testing the socket connection. I'm thinking I'll need to use a different socket for communication or is there a better way to send/receive status messages from Ignition to the controller?

def test_connection(ip, port):

import socket

try:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
    status = "Connection successful"
except socket.error as e:
    status = "Failed to connect: {0}".format(e)
finally:
    sock.close()
    
return status

A 'connection refused' error indicates the error is entirely on the side of whatever you're trying to connect to. Either the controller itself or something else on the network in between is rejecting the attempt to connect from Ignition.

I would also generally recommend using Java's socket infrastructure over the Jython standard library.

1 Like

I know this is an old post but any reason you didnt use the tcp/udp driver and decided to use python? Just curious.

The TCP/UDP drivers are...basically, pretty bad. Among other significant issues, they're incapable of sending/receiving arbitrary raw binary payloads, which is fundamental to the vast majority of low level protocols.

OK cool, I'll go the python route then! Thanks for the info

Consider using java.net's socket class instead of python's, harder to footgun

2 Likes

Thanks, any links to some docs?

javadocs

Some other posts that may provide insight:

2 Likes