I'm using PowerShell to send ZPL to a ZD421.
Sending the ZPL works correctly, but I'm unable to get a response ($reader.ReadToEnd()
line).
Here's the code:
function Out-ZplPrinter {
param (
[Parameter(Mandatory,ValueFromPipeline)]
[string]$Command,
[Parameter(Mandatory)]
[string]$IpAddress,
[int]$Port = 9100
)
try {
[System.Net.Sockets.TcpClient]$client = [System.Net.Sockets.TcpClient]::new()
$client.Connect($IpAddress, $Port)
[System.IO.StreamWriter]$writer = [System.IO.StreamWriter]::new($client.GetStream())
[System.IO.StreamReader]$reader = [System.IO.StreamReader]::new($client.GetStream())
# send the ZPL
$writer.Write($Command)
$writer.Flush()
# read the response
$response = $reader.ReadToEnd() # <--- hangs here
return $response
}
catch {
Write-Host "ERROR: $( $_.Exception.Message )" -ForegroundColor Red
}
finally {
if ($null -ne $reader) {
$reader.Close()
$reader.Dispose()
}
if ($null -ne $writer)
{
$writer.Close()
$writer.Dispose()
}
if ($null -ne $client)
{
$client.Close()
$client.Dispose()
}
}
}
# send a label to the ZD421
"^XA^FO235,25,0^FDFIRST LAST^FS^XZ" | Out-ZplPrinter '192.168.19.112'
What am I missing?
4 Replies
If the ZPL is just to print a label, then there is no response. Printers do not respond to label printings. They only respond to the status query related ZPL commands. The ZPL shown in your post is a pure label formatting ZPL for printing the label and it doesn't trigger a response. Try the other ZPL commands for response, for example:
Is there a way to determine if the printer's labels or ink need to be changed?
Does the HTTP POST method give more details?
I tried sending
~HS
to the printer, using the code (above), and it hangs at the same spot. What am I missing?I found a solution. I made two modifications to the code:
I added a timeout:
Replaced
ReadToEnd()
with this: