Receive a response to sending a job to a printer via TCP

// Expert user has replied.
C C B 2 months 1 week ago
124 4 0

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?

Please Register or Login to post a reply

4 Replies

S Steven Si

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:

### Return printer model and firmware version
~hi

### Query printer status
~HS
C C B

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?

C C B

I tried sending ~HS to the printer, using the code (above), and it hangs at the same spot.  What am I missing?

C C B

I found a solution.  I made two modifications to the code:

I added a timeout:

[System.Net.Sockets.TcpClient]$client = [System.Net.Sockets.TcpClient]::new()
$client.SendTimeout = 500 # milliseconds
$client.ReceiveTimeout = 500 # milliseconds
$client.Connect($IpAddress, 9100)

Replaced ReadToEnd() with this:

$Response = $null
[char[]]$buffer = [char[]]::new(1024)

do
{
	try {
		# hangs when ReceiveTimeout is 0 and no additional data is present
		$size = $reader.Read($buffer, 0, $buffer.Length) 
		$Response += -join $buffer[0..($size - 1)]
	}
	catch [System.IO.IOException] {
		break
	}
} while ( $size -gt 0 )

$Response ? $Response.Trim() : $null

 

CONTACT
Can’t find what you’re looking for?