Sending Data to TCP/IP Printer

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 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

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

[code]def sendString(inStr) :
from 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

[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

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).

1 Like