Ignition: Receive TCP MSG from device

GoodMorning
I’m searching about possibility to receive TCP Message on specif Port (IPaddress:Port) from device (badge reader) with IGNITION .

I can configure badge reader for send to me MSG on specific IPaddress:Port ; how can i read it?

Somebody know if is possible?

Thanks

Sergio

Good day,

We have been attempting to solve the same issue, albeit from a completely different type of data source. at the moment the solution that we have put in place is to create a .bat file which runs on the server which in turn runs a .jar that i have created which listens to a port, the client then connects, which in your case would be the card reader. sends the data to the port then discconects, the program recieves the data and writes it to my SQL database which is then read into Ignition.

Hope that this helps.

Hi,

There is the “UDP and TCP Drivers Module” that lets you receive basic data from these types of devices. It is included with the main install, so all you need to do to use it is go to Devices in the gateway and add a new TCP or UDP device. Whether or not it will work in your case will depend a bit on the device, but it’s worth a shot.

Regards,

MGambrell I’m have thonking to do the same…

Colby, I have try to create connection but driver tell me always “Connected 0/1”

The card reader try to connect in tcp mode only when a’ card is in touch with device then connection go down, may be this the problem.

Sergio,

It is a relatively easy program to create, i wouldnt say that i am an expert programmer and i got the main part of the program working in a couple of hours only needing to tweak for the database connection and the fact that we are listening to a number of ports each with a different table.

Colby, would it be possible to run this program in the gateway as a script if the code was written in python? this is what we had initially planned but using the IAlabs scripting module to dynamicly create/delete tags as needed.

Regards Michael

Which program are you wanting to run in the gateway script? If you are referring to your .bat file you could use the system.util.execute function.

You should be able to configure the TCP driver to do receive the TCP message. If you are seeing 0/1 connections then maybe something else needs to be configured. Check your firewall settings, either disable (temporarily) or create an exception for the port being used for communication.

Resurrecting this question… I have a similar instance, a checkweigher which is configured to send a TCP/IP data packet to a specific port on the Gateway, in this case port 4999. So, I need to somehow configure a TCP driver to be a passive listener on this port so I can grab the incoming packets. I have seen a few posts showing scripting to open a socket on a specific port, but those did not appear to apply to this application. Any ideas?

Thanks in advance,
Dan

I had similar problem 4 years ago… (http://forum.inductiveautomation.com/t/tcp-driver-connect-to-barcode-scanner/6965 and http://forum.inductiveautomation.com/t/tcp-driver-active-passive-connections/6987)
After four years nothing has changed… :unamused:

Go here and vote…
http://ideas.inductiveautomation.com/forums/255378-ignition-features-and-ideas/suggestions/31838332-tcp-driver-active-or-passive-connection

We have a module that accepts tcp sockets and spits out the decoded values to an event script. We would have to change the decode and encoding pieces, but this might work for you

Is there any documentation/manual for that module?

I was trying whole day to get some values to ‘Kymera Socket Event Handler Module’, but I couldn’t…
How a telegram needs to be compiled to get values to the module?

You could also try the Generic TCP driver from here. It is principally designed for binary data, but is also able to receive a simple string that can be parsed in a value change event.
I am using it for a couple of barcode scanners that send data to an UDP listener.

Hello,
The module is expecting a packed binary packet, and you define the in and output packets. If you are expecting json or xml, or anything other than a packed set of data, we would have to fork the module for you.

I generally just import the java.net and java.nio classes for stream sockets and run a semi-permanent asynchronous thread listening as needed. Total control. But then, us controls engineers are biased that way. (-:

No need for java.net, python also has a socket module. The hardest part is to get the semi-permanent asynchronous thread correct.

It can’t be completely permanent, as you have to halt the thread when the code updates (otherwise the thread would keep existing and handling requests with outdated code until the gateway is completely restarted).

I’ve set up a socket started from a timer on the server with a timeout every second. So at least every second, the code is updated. And the socket object itself can be stored in the legacy globals object from Ignition, so you reuse the connection (Phil helped with that last part).

Here’s the extract of the code we use to send a request to a socket and handle the response:

The SendMessages() function is called on a timed basis, and can also be called from a tag trigger (f.e. when you know there’s a message present to send).

It works with our setup, but every socket implementation is a bit different in when they expect a connection to close, what request or acknowledgements they expect, how messages are delimited, … If you need a listener, you can use the same socket to listen on a port, but you also need a timeout to bail out from time to time and allow the underlying code to refresh.

import socket
from datetime import datetime

TIMEOUT = 10  # seconds > timeout for socket to generate answer on our request, after this timeout a new socket is created
BUFFER_SIZE = 1024 # size of packets received

busyConnecting = False
def _ConnectSocket():
	global busyConnecting
	if busyConnecting:
		return
	busyConnecting = True
	try:
		globalVars = system.util.getGlobals()
		if "CtcSocket" in globalVars:
			try:
				globalVars["MySocket"].close()
			except:
				pass
			
		s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		s.settimeout(TIMEOUT)
		s.connect((connectionData["ip"], connectionData["port"]))
		globalVars["MySocket"] = s
		print "Connection succesful"
	except:
		print "Exception happened during _ConnectSocket"
	finally:
		busyConnecting = False
		

def SendMessages():
	request = None
	try: 
		globalVars = system.util.getGlobals()
		if not "MySocket" in globalVars:
			_ConnectSocket()
		s = globalVars["MySocket"]

		request = "Whatever you want to send"

		s.sendall(request)

		startTime = datetime.now()
		response = ""
		while not response.endswith("message delimitor"): # happens when response doesn't fit in buffer
			response += s.recv(BUFFER_SIZE)
			if (datetime.now() - startTime).seconds > TIMEOUT:
				raise socket.timeout("Getting end of response took too long. Current data:\n" + response)
				
		# Handle response
	except:
		print "Exception in SendMessages; try to reconnect;\n" + str(request)
		_ConnectSocket()
1 Like