# UDP driver altering data

**URL:** <https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322>\
**Category:** Ignition\
**Created:** [August 8, 2015, 9:49am UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322 "2015-08-08T09:49:51Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [August 8, 2015, 9:49am UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322/1 "2015-08-08T09:49:51Z")

</div>

I’ve encountered a problem with the UDP driver. I’m trying to bring in a sequence of bytes which contain binary representations of different values (e.g. 32-bit integers and 32-bit IEEE floating point numbers). I’ve set the Message Delimiter Type to PacketBased, which brings the whole sequence in as a string.

The problem with this is that it cannot cope with byte values greater than 127. If it finds any it replaces them with 0xfd.

Any ideas how we can access the correct values? We were hoping to use the Python struct library in a tag’s Value Changed event to parse the values and write them out to other Memory tags.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 11, 2015, 6:19pm UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322/2 "2015-08-11T18:19:57Z")

</div>

I don’t think these messages can survive a round trip from binary -\> unicode -\> binary again. Or, at least, I haven’t figured out a way to get the original binary out of a unicode string yet.

The UDP driver message tag may not be the appropriate conduit for your data. Maybe you can do the UDP part in scripting as well.

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [August 11, 2015, 7:04pm UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322/3 "2015-08-11T19:04:32Z")

</div>

Do you mean some kind of Gateway script, or something lower level than that?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 11, 2015, 7:14pm UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322/4 "2015-08-11T19:14:50Z")

</div>

Yeah, maybe start something up in a gateway startup script that runs until shutdown. I think simple socket communication is easy enough to do in Python/Jython that you should try to avoid the overhead involved with writing a module.

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [August 11, 2015, 7:55pm UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322/5 "2015-08-11T19:55:06Z")

</div>

I tried the following script in the Gateway startup script:

```auto
import socket
import binascii

UDP_IP = "192.168.1.250"
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))

while True:
	data, addr = sock.recvfrom(1024)
	print "Received %s from %s" % (binascii.hexlify(data), addr)
	system.tag.write("[tags]udp_byte", data)

```

but my Designer now hangs when I try to save it. I’d imagine the Gateway is now thrashing non-stop at the script - is there any way of avoiding this and still running quickly enough to read each packet as it arrives?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 11, 2015, 8:00pm UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322/6 "2015-08-11T20:00:06Z")

</div>

You’ll probably want to throw all that into a function and then invokeAsync that function.

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [August 11, 2015, 8:15pm UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322/7 "2015-08-11T20:15:43Z")

</div>

Nearly! The script is now

```auto
def read_udp():
	import socket
	import binascii

	UDP_IP = "192.168.1.250"
	UDP_PORT = 5005

	sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	sock.bind((UDP_IP, UDP_PORT))

	while True:
		data, addr = sock.recvfrom(1024)
		print "Received %s from %s" % (binascii.hexlify(data), addr)
		system.tag.write("[default]udp_byte", data)

system.util.invokeAsynchronous(read_udp)

```

It gets the data, but then gives the following error:

```auto
INFO | jvm 1 | 2015/08/11 21:07:04 | Received 01 from (u'192.168.1.1', 51771)
INFO | jvm 1 | 2015/08/11 21:07:04 | ERROR [GatewaySystemUtilities] [21:07:04,005]: Error running function from system.util.invokeAsynchronous
INFO | jvm 1 | 2015/08/11 21:07:04 | Traceback (most recent call last):
INFO | jvm 1 | 2015/08/11 21:07:04 | File "<[test] Startup Script>", line 14, in read_udp
INFO | jvm 1 | 2015/08/11 21:07:04 | NameError: global name 'system' is not defined
INFO | jvm 1 | 2015/08/11 21:07:04 | 
INFO | jvm 1 | 2015/08/11 21:07:04 | at org.python.core.Py.NameError(Py.java:260)
INFO | jvm 1 | 2015/08/11 21:07:04 | at org.python.core.PyFrame.getglobal(PyFrame.java:265)
INFO | jvm 1 | 2015/08/11 21:07:04 | at org.python.pycode._pyx2.read_udp$1(<[test] Startup Script>:11)
INFO | jvm 1 | 2015/08/11 21:07:04 | at org.python.pycode._pyx2.call_function(<[test] Startup Script>)
INFO | jvm 1 | 2015/08/11 21:07:04 | at org.python.core.PyTableCode.call(PyTableCode.java:165)
INFO | jvm 1 | 2015/08/11 21:07:04 | at org.python.core.PyBaseCode.call(PyBaseCode.java:301)
INFO | jvm 1 | 2015/08/11 21:07:04 | at org.python.core.PyFunction.function ___call__ (PyFunction.java:376)
INFO | jvm 1 | 2015/08/11 21:07:04 | at org.python.core.PyFunction. __call__ (PyFunction.java:371)
INFO | jvm 1 | 2015/08/11 21:07:04 | at org.python.core.PyFunction. __call__ (PyFunction.java:361)
INFO | jvm 1 | 2015/08/11 21:07:04 | at org.python.core.PyFunction. __call__ (PyFunction.java:356)
INFO | jvm 1 | 2015/08/11 21:07:04 | at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:637)
INFO | jvm 1 | 2015/08/11 21:07:04 | at com.inductiveautomation.ignition.gateway.script.GatewaySystemUtilities$1.run(GatewaySystemUtilities.java:65)
INFO | jvm 1 | 2015/08/11 21:07:04 | at java.lang.Thread.run(Thread.java:745)

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 11, 2015, 8:19pm UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322/8 "2015-08-11T20:19:08Z")

</div>

The scoping has changed a bit for 7.7. Try importing system as well.

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [August 11, 2015, 8:26pm UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322/9 "2015-08-11T20:26:14Z")

</div>

I saw that just as you posted Jordan 🙂 I’m now getting an ‘Address already in use’ error. It doesn’t seem to be closing the socket before trying to open another one the next time I save the project.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 11, 2015, 8:40pm UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322/10 "2015-08-11T20:40:00Z")

</div>

Was afraid of this…

You’re going to need to shutdown the socket you create, in a shutdown script perhaps?

But then you’ll need to be able to get ahold of the socket… maybe you can store it as a global singleton? Seems there was just a thread about that kind of thing.

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [August 11, 2015, 8:57pm UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322/11 "2015-08-11T20:57:14Z")

</div>

For anyone else testing this, my Python script for sending data to the Gateway from the command line is as follows:

```auto
import socket
import struct

UDP_IP = "192.168.1.250"
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
MESSAGE = struct.pack("B", 0x01)
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

```

Note that both the sending script and the receiving script use the destination IP address, which is a bit counter intuitive.

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [August 13, 2015, 9:59pm UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322/12 "2015-08-13T21:59:13Z")

</div>

Anyone from IA able to help with this one? Kevin’s suggestion about storing the socket as a global variable so it can be closed properly sounds good but is beyond my ability 🙂

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [August 13, 2015, 10:32pm UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322/13 "2015-08-13T22:32:40Z")

</div>

Hi Al,

I just finished implementing and testing controlled, organized, supported global storage in Ignition.

It is called “gstore”, short for Global Storage. It provides a simple way to have and use global state in Ignition.

Global project state is stored in a global dictionary at pa.gstore. pa.gstore is global to each project. It can be used in Gateway scripts or client/designer scripts.

Here’s the code to use in your circumstance:

Put this code in a Gateway Startup Script:[code]def read\_udp():  
import socket  
import binascii

UDP\_IP = “192.168.1.250”  
UDP\_PORT = 5005

sock = socket.socket(socket.AF\_INET, socket.SOCK\_DGRAM)  
pa.gstore[“sock”] = sock  
sock.bind((UDP\_IP, UDP\_PORT))

while True:  
data, addr = sock.recvfrom(1024)  
print “Received %s from %s” % (binascii.hexlify(data), addr)  
system.tag.write("[default]udp\_byte", data)

system.util.invokeAsynchronous(read\_udp)[/code]  
The only difference to your code is adding the socket to gstore.

Then add the following code to the Gateway Shutdown Script:`pa.gstore["sock"].close()`gstore has more functionality than this but I haven’t updated the documentation yet, but I will soon. If you want to use this and need any help let me know.

gstore is the newest capability in the [PA Power Scripting Module](http://marketplace.inductiveautomation.com/moduledetail?22&moduleId=87).

Edit: getting rid of GStore: [nickmudge.info/post/getting-rid-of-gstore](http://nickmudge.info/post/getting-rid-of-gstore)  
Use Python module level variables instead: [nickmudge.info/post/python-modul … lient-tags](http://nickmudge.info/post/python-module-level-variables-vs-client-tags)

Best,

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [August 13, 2015, 11:13pm UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322/14 "2015-08-13T23:13:40Z")

</div>

I tried testing it out. I’m having problems with reconnecting the socket once it is closed. The OS doesn’t want to use a port for awhile once it is closed.

I found that using this code before binding and not closing the socket seems to work:

```auto
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
```

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [August 14, 2015, 4:44pm UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322/15 "2015-08-14T16:44:34Z")

</div>

Thanks Nick, that seems to have sorted in out. It took a bit to get going - I had to change to another port for a while before going back to the original port - so I’m not sure yet how reliable it is, but this lets me keep testing.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [March 5, 2020, 1:45pm UTC](https://forum.inductiveautomation.com/t/udp-driver-altering-data/10322/16 "2020-03-05T13:45:47Z")

</div>

7 posts were split to a new topic: [Closing a TCP Socket from Jython](https://forum.inductiveautomation.com/t/closing-a-tcp-socket-from-jython/32829)
