1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| Public Function GetPageCount(ByVal PrinterIP As String) As Integer
Dim tcpClient As TcpClient
Dim connector As New cliConnector
Try
tcpClient = connector.Connect(PrinterIP, 9100, 1000)
tcpClient.SendTimeout = 1000
tcpClient.ReceiveTimeout = 1000
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite And networkStream.CanRead Then
' Do a simple write.
Dim txt As String
txt = Chr(27) & "%-12345X@PJL" & vbLf & "@PJL INFO PAGECOUNT" & vbLf & Chr(27) & "%-12345X" & vbLf
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(txt)
networkStream.Write(sendBytes, 0, sendBytes.Length)
' Read the NetworkStream into a byte buffer.
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Output the data received from the host to the console.
Dim returndata As String = Encoding.ASCII.GetString(bytes)
tcpClient.Close()
Return returndata.Substring(returndata.IndexOf(vbNewLine) + 1, returndata.Substring(returndata.IndexOf(vbNewLine) + 1).IndexOf(vbNewLine))
Else
If Not networkStream.CanRead Then
' Console.WriteLine("cannot not write data to this stream")
tcpClient.Close()
Return False
Else
If Not networkStream.CanWrite Then
' Console.WriteLine("cannot read data from this stream")
tcpClient.Close()
Return False
End If
End If
End If
' pause so user can view the console output
tcpClient.Close()
Catch ex As Exception
' Console.WriteLine(ex.Message)
Return False
End Try
End Function |
Partager