How to capture the ZPL ^HV ouotput - python

I need help implementing the ^HV ZPL command and to capture it as the host.
I want to read the TID and encode it to the EPC using Python, i can send print and encode command to the printer but how do i read back from it ?
If I'm using the "Direct Communication" program in the Zebra Setup Utilities tool i can get the TID back in the "Data received" window.
Ive tried using TCP/IP but i dont know how to pull the info just to print
But how can i capture it using python ?
Thanks !

Communicating with a Zebra printer over TCP is the same as any other TCP connection. If the question is how to use the ^HV command, it is usually put into a stored format. The response happens when you use the format to print. Here's a snippet I modified from wiki.python.org.
#!/usr/bin/env python
import socket
TCP_IP = '127.0.0.1'
TCP_PORT = 9100
BUFFER_SIZE = 1024
FORMAT = "^XA^DFE:TEST.ZPL^FO30,30^A0N,50,50^FN1^FS^HV1,15,[,],^FS^XZ"
PRINT = "^XA^XFE:TEST.ZPL^FN1^FDHELLO WORLD^FS^XZ"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(FORMAT)
s.send(PRINT)
data = s.recv(BUFFER_SIZE)
s.close()
print "received data:", data

Related

In python want to read the UDP broadcast message on particular port

I am new to python programming. I have the task to read the broadcast feed on UDP port 4012.I have code of visual basic and it is working fine. The code is as follows.
#Dim receivingUdpClient As New UdpClient(4012)
#Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
#receiveBytes = receivingUdpClient.Receive(RemoteIpEndPoint)
#returnData = Encoding.ASCII.GetString(receiveBytes)
#Dim TestArray() As String = Split(returnData, ";")
I made the following program in python to read the broadcast feed on UPD port 4012, but was unable to achieve it with the following python program. The program is working and shows the cmd window message "waiting for 4012 localhost from 4012".
Can anybody help me out with this? If the code is correct then, how can i checked resolve this issue? i also want to read good material about socket programming in python specially about the UDP socket Broad Cast reading, if anybody can recommend any video or material for read.
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_host = 'localhost'
udp_port = 4012
s.connect((udp_host,udp_port))
print("waiting for 4012",udp_host, "from" ,udp_port)
data , addr= s.recvfrom(1024)
print("Received Messages: ", data ,"from", addr)
You should use broadcast IP to listen.
Currently you are listening 'localhost', but broadcast IP is usually your subnet maximum IP (for 255.255.255.0 mask it is IP with number 255 in last octet)
You need to get right IP from somewhere. Manually you can do it with ifconfig on *nix, or ipconfig on Win:
inet 192.168.100.7 netmask 0xffffff00 broadcast 192.168.100.255
so you need 192.168.100.255
Also, easy way is to listen all IP's. To listen all IP's you could bind socket to '0.0.0.0' or just ''. But in this case you'll catch both broadcast and direct packets.
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_host = ''
udp_port = 4012
s.connect((udp_host,udp_port))
print("waiting for 4012",udp_host, "from" ,udp_port)
data , addr= s.recvfrom(1024)
print("Received Messages: ", data ,"from", addr)
this snippet is something i use quite often do create basic socket server stuff...
socket_config = {
'udp_ip_address': 'your.ip.here.bla',
'udp_port_no': '6789',
'max_send_size': '1024'
}
#
# socket creation
#
serverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
serverSock.bind((socket_config['udp_ip_address'],
int(socket_config['udp_port_no'])))
def receive_loop():
# eternal loop starts here
while True:
data, addr = serverSock.recvfrom(int(socket_config['max_send_size']))
data = data.decode('utf-8')
logger.debug("Message:" + data)

Send data via UDP connection (Bridge)

I've been tasked to create a proof of concept with an Arduino Mega + Yun Shield. I've started from the Bridge sample and I can read my sensors and exposed the data through REST.
But, instead of REST, I want to send packets through UDP. I know there is samples around the web about UDP but I've have found nothing that use UDP with Bridge.
Is this feasible?
UPDATE #1
Ok, I read somewhere that is not possible. But I read also that is possible to run a Python script to send data through UDP.
I made that script:
import socket
import sys
# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('192.168.1.100', 9050)
message = 'This is the message. It will be repeated.'
try:
# Send data
print >>sys.stderr, 'sending "%s"' % message
sent = sock.sendto(message, server_address)
finally:
print >>sys.stderr, 'closing socket'
sock.close()
And call it from the Arduino this way:
Process p;
p.begin("python");
p.addParameter("/test/sendUDP.py");
p.run();
The code run without errors apparently, but my UDP server receive nothing. However, it works with PuTTY.
UPDATE #2
It works! I changed this line:
p.addParameter("/root/test/sendUDP.py");
I changed this line and it works like a charm:
p.addParameter("/root/test/sendUDP.py");

Basic TCP Connection

I am trying to control some test equipment with a TCP connection. The equipment comes with software that you are able to control over TCP. Basically, you can input the IP address and port of the client computer and there is also an indicator light that shows when there is an open listening session on that port (this is all on the equipment software interface)
I have tested this using SocketTest3 (free software) and am able to start a listening session as well as send commands from another computer. Now, I want to control the equipment with Python. I am running the code for the server and client on the same machine as the test equipment (using local IP address). When I simply run the code (with the equipment software closed) I am able to send, receive, and print the messages I send. When I have the equipment software open (necessary for control) I am able to start a listening session (indicator light shows up on equipment software), but nothing happens (no errors and nothing received) when I send commands. The messages are also not sent back to the client to print.
Any ideas? It's probably something very simple that I'm missing.
Server code:
import sys
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
TCP_IP = '127.0.0.1'
TCP_PORT = 8001
s.bind((TCP_IP, TCP_PORT))
s.listen(5)
connection, client_address = s.accept()
BUFFER_SIZE = 20
print 'Address: ', client_address
while 1:
print "receiving..."
data = connection.recv(BUFFER_SIZE)
print data
if not data: break
print "received data:", data
connection.send(data) # echo
connection.close()
Client code:
import socket
TCP_IP = '127.0.0.1'
TCP_PORT = 8001
BUFFER_SIZE = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
print "sending message..."
s.sendall('ST<CR>') # Send command
print "receiving message..."
data = s.recv(BUFFER_SIZE)
s.close()
print "received data:", data
For those wondering what is missing from camerausb's code, I think he was not seeing anything from the client's print statement because he did not use repr() to format the data. I had a similar problem, but this worked for me:
print 'Received', repr(data)

How to capture UDP packets with Python

Facts & context elements:
I need to capture data (latitude,longitude) coming out of a GPS device rework them and make them suitable for another application (QGIS). To this end I've tried to perform (What I thought at first would be a simple one) a python based module.
According to wire shark analysis.
Source Destination Protocol length info
192.168.0.1 225.2.5.1 UPD 136 source port : 1045 destination port:6495
I've tried this code found on various sources, like this one.
import socket
import os
UDP_IP = "225.2.5.1"
UDP_PORT = 6495
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(4096) # buffer size is 1024 bytes
print "received message:", data
os.system("pause")
The problem
This code doesn't work for me.The console windows whether collapse (despite the os.system("pause") or run indefinitely. As I'm not very skilled in python programming nor networking I've tested the provided code with the other IP address and port. As no result came from it I've also started to mix both of them. And finally, gave up and decided to share my issue with the community.
The aim :
I need to be able to access the data contains in this UDP frame with python 2.7 save them in a variable (data) for the next step of my programming project.
Thanks for reading and for your help
You should start your python program from the windows cmd-console or powershell, not from the explorer, then the window stays open and you see error messages. Remove the indentation error and the last line. Be sure, that your computer has the given IP-address. Bind your socket to any address:
import socket
UDP_IP = "0.0.0.0"
UDP_PORT = 6495
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(4096)
print "received message:", data

How sending and receiving works in Python sockets?

I'm working with python sockets for a while, and I wrote some simple programs.
The problem that I encountered is related to sending/receiving methods in python sockets.
Giving you a basic example:
This is the receiver (server):
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 4001))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.listen(5)
while True:
conn, addr = s.accept()
print conn, addr
data1 = conn.recv(64)
data2 = conn.recv(64)
print 'uname is %s , password is: %s' %(data1, data2, )
conn.close()
And this is the sender (or client):
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('', 4001))
uname = raw_input('enter username>')
passw = raw_input('enter password>')
s.send(uname)
s.send(passw)
print 'exiting ...'
s.close()
So the problem is: why server receives both uname and passw in first s.recv() method? It means data2 is always empty!
I have no idea what happens when client executes the s.send() method. I was thinking that each s.send() actually sends a "packet" to the destination (ip, port)!
Can someone explain to me why the second code is working correctly?
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('', 4001))
uname = raw_input('enter username>')
s.send(uname)
passw = raw_input('enter password>')
s.send(passw)
print 'exiting ...'
s.close()
socket.SOCK_STREAM means you're communicating via TCP . This means, if you call send, your data is pushed to the system's networking stack. Both send calls are called shortly one after another.
If you use TCP, your system decides about the packet size. So the uname and passw might be sent in one packet or even split in any other way.
On the receiver's side, data1 receives up to 64 bytes, which is enough for uname and passw.
The explanation above also shows, why the second one works:
Here you need some time between sending uname and passw. During this time, your OS decides to send the packet (i.e. to flush the network buffer).
When you are using streams, you should not think in terms of packets but in terms of streams. There a send call only means: push some data on my network stack(like a pipeline).
If you are interested in packets,
you might try to experiment with UDP:
socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
With such kind of socket your first sender would work as expected
I also faced similar problem. Then I implemented a protocol to send and receive one message at a time. Hope this link will help a lot : LINK

Categories

Resources