Class PrinterConnection


  • public class PrinterConnection
    extends ICommConnection
    A connection to a printer. This allows you to start print jobs using programs already supporting the ICommConnection API. The primary application of this connection type is for sending raw commands to raw printers supporting a printer language like ZPL. ZPL printers will often expose their ZPL command interface as TCP ports, serial connections or ordinary printer devices connected via USB. When setting up those printers as raw printers, the internal command language can still be accessed even though the device is not connected serially. Using a PrinterConnection, code that sends commands via one of the other methods (Ethernet, Serial) can also be used with ordinary USB-connected printers and the device may be identified by its user-specified name. Another advantage is the ability to react to errors that happen after spooling of the print job.

    While this connection is primarily for raw printing, it may also be used for ordinary printing.

    Due to restrictions of the Java printer APIs, this connection is unidirectional - data cannot be received from the printer and the connection behaves as if there is never any data to receive. Also, every send(byte[]) call will spool its own print job - it is not possible to combine multiple calls into one job since the ICommConnection API has no concept of "flushing" and spooling on disconnect() might not be what the user expects.

    • Constructor Detail

      • PrinterConnection

        public PrinterConnection​(java.lang.String name)
        Construct a new printer connection. The name used here is only expected to exist when the connection is connected via connect().
        Parameters:
        name - The "user-friendly" name of the printer. It can be one of the names returned by getPrinterList().
    • Method Detail

      • getPrinterList

        public static java.util.List<java.lang.String> getPrinterList()
        Return list of all possible printer names that can be passed to PrinterConnection(String). These are the "user-friendly" names, usually as set up by the user when installing the printer. Not all of them might be set up as raw printers and there is no way to identify raw printers programmatically (as this depends on the device type, operating system and driver used).
        Returns:
        List of all printer names set up on the system.
      • connect

        public void connect()
                     throws CommConnectionException
        Description copied from class: ICommConnection
        This method opens a connection. Parameters are passed through the constructor
        Specified by:
        connect in class ICommConnection
        Throws:
        CommConnectionException - possible Errorcodes:
        • for TCP Connection:
          • ETHERNET_UNKNOWN_HOST
          • ETHERNET_TIMEOUT
          • WRONG_PARAMETER
        • for USB Connection:
          • DEVICE_IN_USE
          • NO_LIBRARY_FOUND
          • NO_DEVICES_FOUND
          • USB_SET_BAUDRATE
          • USB_SET_DATA_CHARACTERISTICS
          • USB_SET_FLOWCONTROL
          • USB_SET_TIMEOUTS
        • for RS232 Connection:
          • NO_LIBRARY_FOUND
          • SERIAL_PORT_NOT_EXIST
          • SERIAL_PARAMETER_NOT_SET
          • DEVICE_IN_USE
          • SERIAL_NO_ACCESS
          • SERIAL_NO_INITIALISE
      • isConnected

        public boolean isConnected()
        Specified by:
        isConnected in class ICommConnection
        Returns:
        true if connected else false
      • getInfo

        public java.util.Hashtable<java.lang.String,​java.lang.Object> getInfo()
        Gets device information. For this implementation, all the attributes of the corresponding PrintService will be returned.
        Specified by:
        getInfo in class ICommConnection
        Returns:
        Hashtable with the information
      • setSettings

        public void setSettings​(java.util.Hashtable<java.lang.String,​java.lang.String> settings)
        Description copied from class: ICommConnection
        Sets connections settings
        Specified by:
        setSettings in class ICommConnection
        Parameters:
        settings - Hashtable with the special settings
      • send

        public void send​(byte[] senddata)
                  throws CommConnectionException
        Sends data to the connected device. On the printer implementation, each call to this method will spool a new print job.
        Specified by:
        send in class ICommConnection
        Parameters:
        senddata - Data to be sent to the device.
        Throws:
        CommConnectionException - Possible Errorcodes:
        • UNHANDLED_ERROR
        • NOT_INITIALISE
      • recv

        public int recv()
                 throws CommConnectionException
        Receives one character from the device. On this implementation, never will ever be returned, so it will wait for the receive timeout configured and always return -1.
        Specified by:
        recv in class ICommConnection
        Returns:
        Always -1 (timeout).
        Throws:
        CommConnectionException - When connection is uninitialized or the current thread is interrupted.
      • setRecvTimeout

        public void setRecvTimeout​(int timeout)
                            throws CommConnectionException
        Description copied from class: ICommConnection
        set the receive timeout for read data, if the timeout expires an CommConnectionException is raised with errorcode RECV_TIMEOUT, the connection is still valid
        Specified by:
        setRecvTimeout in class ICommConnection
        Parameters:
        timeout - time in milliseconds
        Throws:
        CommConnectionException - possible Errorcodes:
        • for all connections
          • SET_CONFIGURATION
      • setConnectionTimeout

        public void setConnectionTimeout​(int timeout)
        Description copied from class: ICommConnection
        Sets the waiting time for the connection
        Specified by:
        setConnectionTimeout in class ICommConnection
        Parameters:
        timeout - connection timeout
      • getInputStream

        public java.io.InputStream getInputStream()
        Specified by:
        getInputStream in class ICommConnection
        Returns:
        the InputStream of this connection
      • getOutputStream

        public java.io.OutputStream getOutputStream()
        Specified by:
        getOutputStream in class ICommConnection
        Returns:
        the OutputStream of this connection
      • recv

        public void recv​(byte[] b,
                         int off,
                         int len)
                  throws CommConnectionException
        Description copied from class: ICommConnection
        Receives until buffer is filled or timeout occurrs.
        Overrides:
        recv in class ICommConnection
        Parameters:
        b - Byte array to fill
        off - Offset into array (it is filled beginning with the offset)
        len - Number of bytes to read.
        Throws:
        CommConnectionException - possible Errorcodes:
        • for TCP Connection:
          • CONNECTION_LOST
          • RECV_TIMEOUT
          • UNHANDLED_ERROR
          • NOT_INITIALISE
        • for USB Connection:
          • CONNECTION_LOST
          • RECV_TIMEOUT
          • UNHANDLED_ERROR
          • NOT_INITIALISE
        • for RS232 Connection:
          • CONNECTION_LOST
          • RECV_TIMEOUT
          • UNHANDLED_ERROR
          • NOT_INITIALISE
      • receive

        public java.lang.StringBuilder receive​(int... terminators)
                                        throws CommConnectionException
        Description copied from class: ICommConnection
        Receives data from the connected device until one of the terminator signs is found. The result is returned as a StringBuilder for performance reasons.
        Overrides:
        receive in class ICommConnection
        Parameters:
        terminators - A list of terminator signs. Note that this may be a list of parameters or an array.
        Returns:
        a StringBuilder object, including the termination sign
        Throws:
        CommConnectionException - possible Errorcodes:
        • for TCP Connection:
          • CONNECTION_LOST
          • RECV_TIMEOUT
          • UNHANDLED_ERROR
          • NOT_INITIALISE
        • for USB Connection:
          • CONNECTION_LOST
          • RECV_TIMEOUT
          • UNHANDLED_ERROR
          • NOT_INITIALISE
        • for RS232 Connection:
          • CONNECTION_LOST
          • RECV_TIMEOUT
          • UNHANDLED_ERROR
          • NOT_INITIALISE
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object