0

Trying to send commands (not labels) to Zebra printers using Python.

On page 574 of the documentation it shows: enter image description here

Here's my code:

mysocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)          
host= "192.168.100.245" # verified IP address of Zebra printer
port = 9100
mysocket.connect((host, port)) 
name_string= '''"sgd.name":null'''
my_string= f'''{{}}{{{name_string}}}'''
x = json.dumps(obj=my_string)
mysocket.sendall(bytes(x,encoding="utf-8"))
data= mysocket.recv(1024)
print(data.decode('utf-8'))

The printer responds to pings and other non-JSON Zebra commands sent to it (i.e. mysocket.send(b"~hs")). However, with the code above I wait for a long time and no response returns from the printer.

Tried multiple variations of the JSON formatting, what should I try next?

Per @bruan comment I tried the following variations but did not work:

my_string= '''"sgd.name":null"'''

my_string= '''{}{"sgd.name":null}'''

3
  • 1
    It looks like you create the JSON/command manually, then dump it into JSON one more time. What you send now is '"{}{\\"sgd.name\\":null}"'. I think you should send my_string as command. That said, better create JSON used in the command properly Commented Feb 29, 2024 at 17:11
  • @buran tried two variations based on your feedback, no success. Commented Feb 29, 2024 at 17:25
  • 1
    but did you remove the x = json.dumps(obj=my_string) line? Second one looks right Commented Feb 29, 2024 at 17:28

1 Answer 1

1

You create the JSON/command manually, then dump it into JSON one more time. What you send now is '"{}{\\"sgd.name\\":null}"', i.e. the string "{}{\\"sgd.name\\":null}" Also, as I said in the comments, better don't create JSON manually. Use json module and it will be easier to expand the query dict and get multiple values at once if needed.

import json

# some lines skipped for brevity
query = {'sgd.name': None}
command = f'{{}}{json.dumps(query)}' # {}{"sgd.name": null}
mysocket.sendall(bytes(command, encoding="utf-8"))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.