# Ignition: Receive TCP MSG from device

**URL:** <https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902>\
**Category:** General Discussion\
**Created:** [January 26, 2014, 1:40pm UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902 "2014-01-26T13:40:34Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![SergioS](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@SergioS](https://forum.inductiveautomation.com/u/SergioS)\
**Post date:** [January 26, 2014, 1:40pm UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902/1 "2014-01-26T13:40:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MGambrell](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@MGambrell](https://forum.inductiveautomation.com/u/MGambrell)\
**Post date:** [January 27, 2014, 9:18am UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902/2 "2014-01-27T09:18:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colby.Clegg](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/colby.clegg/32/3282_2.png) [@Colby.Clegg](https://forum.inductiveautomation.com/u/Colby.Clegg)\
**Post date:** [January 27, 2014, 6:42pm UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902/3 "2014-01-27T18:42:31Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![SergioS](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@SergioS](https://forum.inductiveautomation.com/u/SergioS)\
**Post date:** [January 28, 2014, 6:41am UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902/4 "2014-01-28T06:41:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MGambrell](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@MGambrell](https://forum.inductiveautomation.com/u/MGambrell)\
**Post date:** [January 28, 2014, 1:11pm UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902/5 "2014-01-28T13:11:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![greg.simpson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/greg.simpson/32/3135_2.png) [@greg.simpson](https://forum.inductiveautomation.com/u/greg.simpson)\
**Post date:** [June 3, 2014, 6:14pm UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902/6 "2014-06-03T18:14:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dstauft](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dstauft/32/6796_2.png) [@dstauft](https://forum.inductiveautomation.com/u/dstauft)\
**Post date:** [October 10, 2017, 5:32pm UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902/7 "2017-10-10T17:32:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [October 10, 2017, 6:01pm UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902/8 "2017-10-10T18:01:41Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [October 10, 2017, 8:04pm UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902/9 "2017-10-10T20:04:54Z")

</div>

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

> **[Kymera Systems Inc – IIoT Solutions - Socket Event Handler Module](https://www.kymerasystems.com/kymera-socket-event-handler-module/)**
>
> The Kymera Socket Event Handler module allows you to configure and write flexible scripts that are executed when data is...

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [October 11, 2017, 6:01am UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902/10 "2017-10-11T06:01:48Z")

</div>

> [@Kyle\_Chase](#):
>
> 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?

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [October 11, 2017, 4:22pm UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902/11 "2017-10-11T16:22:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![chi](https://avatars.discourse-cdn.com/v4/letter/c/0ea827/32.png) [@chi](https://forum.inductiveautomation.com/u/chi)\
**Post date:** [October 11, 2017, 7:43pm UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902/12 "2017-10-11T19:43:36Z")

</div>

You could also try the Generic TCP driver [from here](https://github.com/chi-Ignition/generic-tcp-driver/releases). 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.

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [October 12, 2017, 6:00am UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902/13 "2017-10-12T06:00:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 12, 2017, 12:45pm UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902/14 "2017-10-12T12:45:42Z")

</div>

I generally just import the [java.net](http://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. (-:

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [October 13, 2017, 9:27am UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902/15 "2017-10-13T09:27:19Z")

</div>

No need for [java.net](http://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).

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [October 13, 2017, 9:47am UTC](https://forum.inductiveautomation.com/t/ignition-receive-tcp-msg-from-device/7902/16 "2017-10-13T09:47:23Z")

</div>

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.

```auto
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()

```
