# Sending Data to TCP/IP Printer

**URL:** <https://forum.inductiveautomation.com/t/sending-data-to-tcp-ip-printer/9212>\
**Category:** Ignition\
**Created:** [October 22, 2014, 10:12pm UTC](https://forum.inductiveautomation.com/t/sending-data-to-tcp-ip-printer/9212 "2014-10-22T22:12:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![WildeKurt](https://avatars.discourse-cdn.com/v4/letter/w/50afbb/32.png) [@WildeKurt](https://forum.inductiveautomation.com/u/WildeKurt)\
**Post date:** [October 22, 2014, 10:12pm UTC](https://forum.inductiveautomation.com/t/sending-data-to-tcp-ip-printer/9212/1 "2014-10-22T22:12:15Z")

</div>

I have an application where I need to send ascii data to a TCP/IP printer on port 3000. I’ve see from this thread [http://inductiveautomation.com/forum/viewtopic.php?p=13319&hilit=socket&t=5358&f=70#p13319](http://inductiveautomation.com/forum/viewtopic.php?p=13319&hilit=socket&t=5358&f=70#p13319) that a listener has been implemented with java so I’d like a little help with the sending portion. I guess what I really need to know is how to build the packet.

I’ve got this bit a code but it’s having problems specifically with the 'packet = ’ part.

[code]def sendString() :

```
from java.net import DatagramSocket, DatagramPacket, InetAddress
from java.lang import String

buf = []
buf.append(1)
buf.append(2)

addr = InetAddress.getByName("127.0.0.1")
packet = DatagramPacket(buf,0,0,addr)
port = 3002

socket = DatagramSocket(port,addr)

try :
	socket.send(packet)
finally :
	socket.close()

```

[/code]

Thanks.

Kurt

---

<div class="post-metadata">

**Author:** ![WildeKurt](https://avatars.discourse-cdn.com/v4/letter/w/50afbb/32.png) [@WildeKurt](https://forum.inductiveautomation.com/u/WildeKurt)\
**Post date:** [October 23, 2014, 1:00am UTC](https://forum.inductiveautomation.com/t/sending-data-to-tcp-ip-printer/9212/2 "2014-10-23T01:00:21Z")

</div>

Answering my own question at least in part: What I’ve come up with for sending is:

[code]def sendString(inStr) :  
from [java.net](http://java.net) import DatagramSocket, DatagramPacket, InetAddress  
from java.lang import String

```
buf = []
buf = String.getBytes(inStr)
addr = InetAddress.getByName("127.0.0.1")
receivePort = 3001
packet = DatagramPacket(buf,len(buf),addr,receivePort)
sendPort = 3010
socket = DatagramSocket(sendPort,addr)
socket.send(packet)
print "OK":
socket.close()("Hello")

```

[/code]

I made a listener using code I found [http://inductiveautomation.com/forum/viewtopic.php?p=13319&hilit=socket&t=5358&f=70#p13319](http://inductiveautomation.com/forum/viewtopic.php?p=13319&hilit=socket&t=5358&f=70#p13319)

[code]def startReceiver() :

```
from jarray import zeros
from java.net import DatagramSocket, DatagramPacket, InetAddress
from java.lang import String

packet = DatagramPacket(zeros(1024, 'b'), 1024)

# listening port & address
port = 3001
addr = InetAddress.getByName("127.0.0.1")

socket = DatagramSocket(port, addr)

try:
   system.tag.write("SocketTest/ServerActive",1)
   for x in range(10):
	  socket.receive(packet)
	  data = String(packet.data, packet.offset, packet.length)
	  system.tag.write("SocketTest/InputBuffer",data)
	  print "Received packet from %s: '%s'" % (packet.address, data)
finally:
   system.tag.write("SocketTest/ServerActive",0)
   socket.close()

```

[/code]

I’m starting that code with

```auto
system.util.invokeAsynchronous(project.socket.startReceiver)
```

It seems to work just sending simple strings now I need to adapt it to deal with the printers protocol (VideoJet ESI in case anyone’s interested).
