# ZPL Reading printer status

**URL:** <https://forum.inductiveautomation.com/t/zpl-reading-printer-status/82890>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [December 8, 2023, 11:34am UTC](https://forum.inductiveautomation.com/t/zpl-reading-printer-status/82890 "2023-12-08T11:34:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakned](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@jakned](https://forum.inductiveautomation.com/u/jakned)\
**Post date:** [December 8, 2023, 11:34am UTC](https://forum.inductiveautomation.com/t/zpl-reading-printer-status/82890/1 "2023-12-08T11:34:06Z")

</div>

Hi, I am trying to get my script working where I am trying to query printer status using ZPL. The response consists of multiple lines. I successfully read all the data i need and when there there is no data to be read, the code just hangs probably waiting for more data to come. Now I dont know how to tell the code to not to wait anymore. Below is my code:

```auto
  client_socket = Socket(self.host, self.port)
  output_stream = DataOutputStream(client_socket.getOutputStream())
  output_stream.writeBytes(command)
  input_stream = BufferedReader(InputStreamReader(client_socket.getInputStream()))

  buf = ""
  while True:
      line = input_stream.readLine()
      if line:
          buf += line
      else:
          break
   
  input_stream.close()
  client_socket.close()

print buf

```

In my case, printer responds to ~HS with three lines. The loop runs three time and gets stuck on the fourth run on line = input\_stream.readLine().

Could anyone advise?

Thank you

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 8, 2023, 12:44pm UTC](https://forum.inductiveautomation.com/t/zpl-reading-printer-status/82890/2 "2023-12-08T12:44:52Z")

</div>

Count the lines and exit after the third.

A more robust solution would use a separate thread for this, with code that examines each line, extracts the information as it arrives, and places it in appropriate tags for the rest of the system to consume.

---

<div class="post-metadata">

**Author:** ![jakned](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@jakned](https://forum.inductiveautomation.com/u/jakned)\
**Post date:** [December 8, 2023, 1:46pm UTC](https://forum.inductiveautomation.com/t/zpl-reading-printer-status/82890/3 "2023-12-08T13:46:50Z")

</div>

I could count to three and then exit the loop but I will be using this for calling other commands that might return more or less than three lines. Is there a way to check if there is a text to read and if not then exit the loop?

I found a example for BufferedReader to do this but It was Java example and I dont know how to use it in Jython.

### **3.2. Reading Line-by-Line**

[https://www.baeldung.com/java-buffered-reader](https://www.baeldung.com/java-buffered-reader)

I copied out only part of my code for this post. I will be calling this in separate thread.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 8, 2023, 3:56pm UTC](https://forum.inductiveautomation.com/t/zpl-reading-printer-status/82890/4 "2023-12-08T15:56:00Z")

</div>

> [@jakned](#):
>
> Is there a way to check if there is a text to read and if not then exit the loop?

You can use a timeout, but such techniques are unreliable for network traffic.

> [@jakned](#):
>
> calling other commands

Sounds like you need a long-lived thread and socket. This is tricky. Some discussions for you:

[https://forum.inductiveautomation.com/search?q=long-lived%20thread%20order%3Alatest](https://forum.inductiveautomation.com/search?q=long-lived%20thread%20order%3Alatest)
