BufferedReader em comando enviado a impressora USB

2 respostas
cardosao

Ola, amigos.

Estou a 3 dias procurando uma forma de executar comando PJL em uma impressora USB. Porem sem sucesso. Este comando eu ja executo via rede abrindo um socket, porem como o socket não funciona com USB, não consegui executar. O detalhe é que ao executar o comando ele me retorna um valor, por exemplo qtdade de paginas impressas, ja tentei fazer com o jUSB, porem nao consigo usar o bufferedReader pra ler o valor que esta chegando. Abaixo colarei o codigo que uso pra fazer em rede e tbm o que estou usando pra tentar em USB:

rede ok
Socket s = new Socket(InetAddress.getByName("10.1.1.111").getHostName(),9100);
          File f = new File("caminhoDoArquivo");
          DataOutputStream os = new DataOutputStream(s.getOutputStream());
          os.writeBytes("comando @PJL");
          BufferedReader in = in = new BufferedReader(new InputStreamReader(s.getInputStream()));
          BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (new FileOutputStream(f)));
          for (String linha = in.readLine(); linha != null   ; linha = in.readLine())
            {
              bw.write (linha); bw.newLine();
              pw.write(linha);
 
            }
usb retornando null
public class MainTesteJusb {

    private static String vendID = "";
    private String prodID = "";

    public static void main(String[] args) {

        
        try {
            // Bootstrap by getting the USB Host from the HostFactory.
            Host   host = HostFactory.getHost();
            // Obtain a list of the USB buses available on the Host.
            Bus[]  bus  = host.getBusses();
            //System.out.println("BUSSES : " + host.getDevice(path));
            int    total_bus = bus.length;

            System.out.println( "Total bus: " + total_bus );

            // Traverse through all the USB buses.
            for (int i=0; i <total_bus; i++)  {

                System.out.println( "BUS: " + i );

                // Access the root hub on the USB bus and obtain the
                // number of USB ports available on the root hub.
                Device root = bus[i].getRootHub();
                int total_port = root.getNumPorts();

                System.out.println( "Total port: " + total_port );
                System.out.println( "identifier: " + root.getPortIdentifier() );
                System.out.println( "address: " + root.getAddress() );
                System.out.println( "speed: " + root.getSpeed() );
                System.out.println("___________________________"+"\n");

                // Traverse through all the USB ports available on the
                // root hub. It should be mentioned that the numbering
                // starts from 1, not 0.


                for (int j=1; j><=total_port; j++) {
                    // Obtain the Device connected to the port.
                    Device device = root.getChild(j);
                    System.out.println( "child: " + j );

                  //  try{

                        if (device != null) {
                            System.out.println( "device: " + device.getPortIdentifier() );
                            DeviceDescriptor d = device.getDeviceDescriptor();
                            System.out.println(  "getManufacturerStringId: " +  d.getManufacturerStringId() );
                            System.out.println(  "getVendorId: " +  d.getVendorId() );
                            System.out.println(  "getDeviceId: " +  d.getDeviceId() );
                            System.out.println(  "getProduct: " +  d.getProduct(0) );
                            System.out.println("bcdUSB                   : " + d.getUSBVersion());
                            System.out.println("Device Class             : " + d.getDeviceClass()  + " ( " + d.getDeviceClassName() + " )");
                            System.out.println("Device Subclass          : " + d.getDeviceSubClass());
                            System.out.println("Device Protocol          : " + d.getDeviceProtocol());
                            System.out.println("Max Packet Size          : " + d.getMaxPacketSize0());
                            MainTesteJusb.vendID = "0x" + Integer.toHexString(d.getVendorId());
                            System.out.println("idVendor                 : " + d.getVendorId() + "      0x" + Integer.toHexString(d.getVendorId()) + "  " + d.getManufacturer(1));
                            System.out.println("idProduct                : " + d.getProductId()+ "      0x" + Integer.toHexString(d.getProductId()) + "  " + d.getProduct(0));
                            System.out.println("bcdDevice release number : " + d.getDeviceId());
                            System.out.println("iManufacturer            : " + d.getManufacturerStringId());
                            System.out.println("iProduct                 : " + d.getProductStringId());
                            System.out.println("iSerialNumber            : " + d.getSerialStringId() + "  " + d.getSerial(0));
                            System.out.println("NumConfiguration         : " + d.getNumConfigurations());
                            System.out.println(  "\n"+"_______________________________");
                            // Obtain the current Configuration of the device and the number of
                           // Interfaces available under the current Configuration.
                           Configuration config = device.getConfiguration();
                           int total_interface = config.getNumInterfaces();

                           //System.out.println( "total_interface: " + total_interface );

                           // Traverse through the Interfaces
                           for (int k=0; k<total_interface; k++) {
                               // Access the currently Interface and obtain the number of
                               // endpoints available on the Interface.
                               Interface itf = config.getInterface(k, 0);
                               int total_ep  = itf.getNumEndpoints();

                               // Traverse through all the endpoints.
                               for (int l=0; l><total_ep; l++){
                                   // Access the endpoint, and obtain its I/O type.
                                   Endpoint ep = itf.getEndpoint(l);
                                   String io_type = ep.getType();
                                   boolean input  = ep.isInput();

                                   System.out.println( "io_type: " + io_type );
                                   System.out.println( "total End Points: " + total_ep );
                                   System.out.println( "INput ?: " + input );
                                   System.out.println( "Type : " + ep.getType() );


                                   // If the endpoint is an input endpoint, obtain its
                                   // InputStream and read in data.
System.out.println( "Validação    " + input+ " " + ep.getType() + " " + vendID);
                                   if ( input && vendID.equals("0x43d") && ep.getType().contains("bulk")) {
//                                         //System.out.println( vendID );
//                                       File file = new File("C:/Print Evo/USBPJL.prime");
//                                       BufferedReader ins =  new BufferedReader(new InputStreamReader(ep.getInputStream()));
//                                       BufferedWriter bw2 = new BufferedWriter (new OutputStreamWriter (new FileOutputStream(file)));
//                                       for (String linha = ins.readLine(); linha != null   ; linha = ins.readLine())
//                                          {
//                                              bw2.write (linha); bw2.newLine();
//                                              System.out.println("1 "+linha);
//                                          }
//                                       bw2.close();
//                                       // Read in data here
//                                       ins.close();
                                         try {
                                          File f = new File("C:/Print Evo/USBPJL2.prime");
                                          DataOutputStream os = new DataOutputStream(ep.getOutputStream());
                                          //os.writeBytes("[code]

public class MainTesteJusb {

    private static String vendID = "";
    private String prodID = "";

    public static void main(String[] args) {

        
        try {
            // Bootstrap by getting the USB Host from the HostFactory.
            Host   host = HostFactory.getHost();
            // Obtain a list of the USB buses available on the Host.
            Bus[]  bus  = host.getBusses();
            //System.out.println("BUSSES : " + host.getDevice(path));
            int    total_bus = bus.length;

            System.out.println( "Total bus: " + total_bus );

            // Traverse through all the USB buses.
            for (int i=0; i &lt;total_bus; i++)  {

                System.out.println( "BUS: " + i );

                // Access the root hub on the USB bus and obtain the
                // number of USB ports available on the root hub.
                Device root = bus[i].getRootHub();
                int total_port = root.getNumPorts();

                System.out.println( "Total port: " + total_port );
                System.out.println( "identifier: " + root.getPortIdentifier() );
                System.out.println( "address: " + root.getAddress() );
                System.out.println( "speed: " + root.getSpeed() );
                System.out.println("___________________________"+"\n");

                // Traverse through all the USB ports available on the
                // root hub. It should be mentioned that the numbering
                // starts from 1, not 0.


                for (int j=1; j&gt;&lt;=total_port; j++) {
                    // Obtain the Device connected to the port.
                    Device device = root.getChild(j);
                    System.out.println( &quot;child: &quot; + j );

                  //  try{

                        if (device != null) {
                            System.out.println( &quot;device: &quot; + device.getPortIdentifier() );
                            DeviceDescriptor d = device.getDeviceDescriptor();
                            System.out.println(  &quot;getManufacturerStringId: &quot; +  d.getManufacturerStringId() );
                            System.out.println(  &quot;getVendorId: &quot; +  d.getVendorId() );
                            System.out.println(  &quot;getDeviceId: &quot; +  d.getDeviceId() );
                            System.out.println(  &quot;getProduct: &quot; +  d.getProduct(0) );
                            System.out.println(&quot;bcdUSB                   : &quot; + d.getUSBVersion());
                            System.out.println(&quot;Device Class             : &quot; + d.getDeviceClass()  + &quot; ( &quot; + d.getDeviceClassName() + &quot; )&quot;);
                            System.out.println(&quot;Device Subclass          : &quot; + d.getDeviceSubClass());
                            System.out.println(&quot;Device Protocol          : &quot; + d.getDeviceProtocol());
                            System.out.println(&quot;Max Packet Size          : &quot; + d.getMaxPacketSize0());
                            MainTesteJusb.vendID = &quot;0x&quot; + Integer.toHexString(d.getVendorId());
                            System.out.println(&quot;idVendor                 : &quot; + d.getVendorId() + &quot;      0x&quot; + Integer.toHexString(d.getVendorId()) + &quot;  &quot; + d.getManufacturer(1));
                            System.out.println(&quot;idProduct                : &quot; + d.getProductId()+ &quot;      0x&quot; + Integer.toHexString(d.getProductId()) + &quot;  &quot; + d.getProduct(0));
                            System.out.println(&quot;bcdDevice release number : &quot; + d.getDeviceId());
                            System.out.println(&quot;iManufacturer            : &quot; + d.getManufacturerStringId());
                            System.out.println(&quot;iProduct                 : &quot; + d.getProductStringId());
                            System.out.println(&quot;iSerialNumber            : &quot; + d.getSerialStringId() + &quot;  &quot; + d.getSerial(0));
                            System.out.println(&quot;NumConfiguration         : &quot; + d.getNumConfigurations());
                            System.out.println(  &quot;\n&quot;+&quot;_______________________________&quot;);
                            // Obtain the current Configuration of the device and the number of
                           // Interfaces available under the current Configuration.
                           Configuration config = device.getConfiguration();
                           int total_interface = config.getNumInterfaces();

                           //System.out.println( &quot;total_interface: &quot; + total_interface );

                           // Traverse through the Interfaces
                           for (int k=0; k&lt;total_interface; k++) {
                               // Access the currently Interface and obtain the number of
                               // endpoints available on the Interface.
                               Interface itf = config.getInterface(k, 0);
                               int total_ep  = itf.getNumEndpoints();

                               // Traverse through all the endpoints.
                               for (int l=0; l&gt;&lt;total_ep; l++){
                                   // Access the endpoint, and obtain its I/O type.
                                   Endpoint ep = itf.getEndpoint(l);
                                   String io_type = ep.getType();
                                   boolean input  = ep.isInput();

                                   System.out.println( "io_type: " + io_type );
                                   System.out.println( "total End Points: " + total_ep );
                                   System.out.println( "INput ?: " + input );
                                   System.out.println( "Type : " + ep.getType() );


                                   // If the endpoint is an input endpoint, obtain its
                                   // InputStream and read in data.
System.out.println( "Validação    " + input+ " " + ep.getType() + " " + vendID);
                                   if ( input && vendID.equals("0x43d") && ep.getType().contains("bulk")) {
//                                         //System.out.println( vendID );
//                                       File file = new File("C:/Print Evo/USBPJL.prime");
//                                       BufferedReader ins =  new BufferedReader(new InputStreamReader(ep.getInputStream()));
//                                       BufferedWriter bw2 = new BufferedWriter (new OutputStreamWriter (new FileOutputStream(file)));
//                                       for (String linha = ins.readLine(); linha != null   ; linha = ins.readLine())
//                                          {
//                                              bw2.write (linha); bw2.newLine();
//                                              System.out.println("1 "+linha);
//                                          }
//                                       bw2.close();
//                                       // Read in data here
//                                       ins.close();
                                         try {
                                          File f = new File("C:/Print Evo/USBPJL2.prime");
                                          DataOutputStream os = new DataOutputStream(ep.getOutputStream());
                                          //os.writeBytes("\033%-12345X@PJL JOB\r\n@PJL INFO PAGECOUNT\r\n@PJL EOJ\r\n\033%-12345X\r\n");
                                          os.writeBytes("\033%-12345X@PJL JOB\r\n@PJL ECHO PAGECOUNT\r\n@PJL EOJ\r\n\033%-12345X\r\n");
                                          BufferedReader in = new BufferedReader(new InputStreamReader(ep.getInputStream()));
                                          BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (new FileOutputStream(f)));
                                          for (String linha = in.readLine(); linha != null   ; linha = in.readLine())
                                            {
                                              bw.write (linha); bw.newLine();
                                              System.out.println("1 "+linha);
                                            }
                                          bw.close();
                                          System.out.println("3 ");
                                          in.close();

                                         // System.console();
                                          System.out.println("4 ");
                                          os.flush(); os.close();
                                          //ep.close();
                                          } catch (Exception e) {
                                              System.out.println(e.getMessage()+" ERRO AQUI !"+e.getLocalizedMessage());
                                              e.printStackTrace();
                                           }


                                   }
                                   //else {
                                   // If the Endpoint is and output Endpoint, obtain its
                                   // OutputStream and write out data.
//                                       OutputStream out;
//                                       System.out.println("Erro !!!!");
//                                       out = ep.getOutputStream();
//                                       // Write out data here.
//                                       out.close();
                                   //}
                               }
                           }
                        } else {
                            System.out.println( "Device nulo" );
                        }
               //     }catch ( Exception e ){
             //           e.printStackTrace();
              //      }
                }
            }
        } catch (Exception e)  {
            e.printStackTrace();
        }
        

    }

}
33%-12345X@PJL JOB\r\n@PJL INFO PAGECOUNT\r\n@PJL EOJ\r\n
public class MainTesteJusb {

    private static String vendID = "";
    private String prodID = "";

    public static void main(String[] args) {

        
        try {
            // Bootstrap by getting the USB Host from the HostFactory.
            Host   host = HostFactory.getHost();
            // Obtain a list of the USB buses available on the Host.
            Bus[]  bus  = host.getBusses();
            //System.out.println("BUSSES : " + host.getDevice(path));
            int    total_bus = bus.length;

            System.out.println( "Total bus: " + total_bus );

            // Traverse through all the USB buses.
            for (int i=0; i &lt;total_bus; i++)  {

                System.out.println( "BUS: " + i );

                // Access the root hub on the USB bus and obtain the
                // number of USB ports available on the root hub.
                Device root = bus[i].getRootHub();
                int total_port = root.getNumPorts();

                System.out.println( "Total port: " + total_port );
                System.out.println( "identifier: " + root.getPortIdentifier() );
                System.out.println( "address: " + root.getAddress() );
                System.out.println( "speed: " + root.getSpeed() );
                System.out.println("___________________________"+"\n");

                // Traverse through all the USB ports available on the
                // root hub. It should be mentioned that the numbering
                // starts from 1, not 0.


                for (int j=1; j&gt;&lt;=total_port; j++) {
                    // Obtain the Device connected to the port.
                    Device device = root.getChild(j);
                    System.out.println( &quot;child: &quot; + j );

                  //  try{

                        if (device != null) {
                            System.out.println( &quot;device: &quot; + device.getPortIdentifier() );
                            DeviceDescriptor d = device.getDeviceDescriptor();
                            System.out.println(  &quot;getManufacturerStringId: &quot; +  d.getManufacturerStringId() );
                            System.out.println(  &quot;getVendorId: &quot; +  d.getVendorId() );
                            System.out.println(  &quot;getDeviceId: &quot; +  d.getDeviceId() );
                            System.out.println(  &quot;getProduct: &quot; +  d.getProduct(0) );
                            System.out.println(&quot;bcdUSB                   : &quot; + d.getUSBVersion());
                            System.out.println(&quot;Device Class             : &quot; + d.getDeviceClass()  + &quot; ( &quot; + d.getDeviceClassName() + &quot; )&quot;);
                            System.out.println(&quot;Device Subclass          : &quot; + d.getDeviceSubClass());
                            System.out.println(&quot;Device Protocol          : &quot; + d.getDeviceProtocol());
                            System.out.println(&quot;Max Packet Size          : &quot; + d.getMaxPacketSize0());
                            MainTesteJusb.vendID = &quot;0x&quot; + Integer.toHexString(d.getVendorId());
                            System.out.println(&quot;idVendor                 : &quot; + d.getVendorId() + &quot;      0x&quot; + Integer.toHexString(d.getVendorId()) + &quot;  &quot; + d.getManufacturer(1));
                            System.out.println(&quot;idProduct                : &quot; + d.getProductId()+ &quot;      0x&quot; + Integer.toHexString(d.getProductId()) + &quot;  &quot; + d.getProduct(0));
                            System.out.println(&quot;bcdDevice release number : &quot; + d.getDeviceId());
                            System.out.println(&quot;iManufacturer            : &quot; + d.getManufacturerStringId());
                            System.out.println(&quot;iProduct                 : &quot; + d.getProductStringId());
                            System.out.println(&quot;iSerialNumber            : &quot; + d.getSerialStringId() + &quot;  &quot; + d.getSerial(0));
                            System.out.println(&quot;NumConfiguration         : &quot; + d.getNumConfigurations());
                            System.out.println(  &quot;\n&quot;+&quot;_______________________________&quot;);
                            // Obtain the current Configuration of the device and the number of
                           // Interfaces available under the current Configuration.
                           Configuration config = device.getConfiguration();
                           int total_interface = config.getNumInterfaces();

                           //System.out.println( &quot;total_interface: &quot; + total_interface );

                           // Traverse through the Interfaces
                           for (int k=0; k&lt;total_interface; k++) {
                               // Access the currently Interface and obtain the number of
                               // endpoints available on the Interface.
                               Interface itf = config.getInterface(k, 0);
                               int total_ep  = itf.getNumEndpoints();

                               // Traverse through all the endpoints.
                               for (int l=0; l&gt;&lt;total_ep; l++){
                                   // Access the endpoint, and obtain its I/O type.
                                   Endpoint ep = itf.getEndpoint(l);
                                   String io_type = ep.getType();
                                   boolean input  = ep.isInput();

                                   System.out.println( "io_type: " + io_type );
                                   System.out.println( "total End Points: " + total_ep );
                                   System.out.println( "INput ?: " + input );
                                   System.out.println( "Type : " + ep.getType() );


                                   // If the endpoint is an input endpoint, obtain its
                                   // InputStream and read in data.
System.out.println( "Validação    " + input+ " " + ep.getType() + " " + vendID);
                                   if ( input && vendID.equals("0x43d") && ep.getType().contains("bulk")) {
//                                         //System.out.println( vendID );
//                                       File file = new File("C:/Print Evo/USBPJL.prime");
//                                       BufferedReader ins =  new BufferedReader(new InputStreamReader(ep.getInputStream()));
//                                       BufferedWriter bw2 = new BufferedWriter (new OutputStreamWriter (new FileOutputStream(file)));
//                                       for (String linha = ins.readLine(); linha != null   ; linha = ins.readLine())
//                                          {
//                                              bw2.write (linha); bw2.newLine();
//                                              System.out.println("1 "+linha);
//                                          }
//                                       bw2.close();
//                                       // Read in data here
//                                       ins.close();
                                         try {
                                          File f = new File("C:/Print Evo/USBPJL2.prime");
                                          DataOutputStream os = new DataOutputStream(ep.getOutputStream());
                                          //os.writeBytes("\033%-12345X@PJL JOB\r\n@PJL INFO PAGECOUNT\r\n@PJL EOJ\r\n\033%-12345X\r\n");
                                          os.writeBytes("\033%-12345X@PJL JOB\r\n@PJL ECHO PAGECOUNT\r\n@PJL EOJ\r\n\033%-12345X\r\n");
                                          BufferedReader in = new BufferedReader(new InputStreamReader(ep.getInputStream()));
                                          BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (new FileOutputStream(f)));
                                          for (String linha = in.readLine(); linha != null   ; linha = in.readLine())
                                            {
                                              bw.write (linha); bw.newLine();
                                              System.out.println("1 "+linha);
                                            }
                                          bw.close();
                                          System.out.println("3 ");
                                          in.close();

                                         // System.console();
                                          System.out.println("4 ");
                                          os.flush(); os.close();
                                          //ep.close();
                                          } catch (Exception e) {
                                              System.out.println(e.getMessage()+" ERRO AQUI !"+e.getLocalizedMessage());
                                              e.printStackTrace();
                                           }


                                   }
                                   //else {
                                   // If the Endpoint is and output Endpoint, obtain its
                                   // OutputStream and write out data.
//                                       OutputStream out;
//                                       System.out.println("Erro !!!!");
//                                       out = ep.getOutputStream();
//                                       // Write out data here.
//                                       out.close();
                                   //}
                               }
                           }
                        } else {
                            System.out.println( "Device nulo" );
                        }
               //     }catch ( Exception e ){
             //           e.printStackTrace();
              //      }
                }
            }
        } catch (Exception e)  {
            e.printStackTrace();
        }
        

    }

}
33%-12345X\r\n"); os.writeBytes("
public class MainTesteJusb {

    private static String vendID = "";
    private String prodID = "";

    public static void main(String[] args) {

        
        try {
            // Bootstrap by getting the USB Host from the HostFactory.
            Host   host = HostFactory.getHost();
            // Obtain a list of the USB buses available on the Host.
            Bus[]  bus  = host.getBusses();
            //System.out.println("BUSSES : " + host.getDevice(path));
            int    total_bus = bus.length;

            System.out.println( "Total bus: " + total_bus );

            // Traverse through all the USB buses.
            for (int i=0; i &lt;total_bus; i++)  {

                System.out.println( "BUS: " + i );

                // Access the root hub on the USB bus and obtain the
                // number of USB ports available on the root hub.
                Device root = bus[i].getRootHub();
                int total_port = root.getNumPorts();

                System.out.println( "Total port: " + total_port );
                System.out.println( "identifier: " + root.getPortIdentifier() );
                System.out.println( "address: " + root.getAddress() );
                System.out.println( "speed: " + root.getSpeed() );
                System.out.println("___________________________"+"\n");

                // Traverse through all the USB ports available on the
                // root hub. It should be mentioned that the numbering
                // starts from 1, not 0.


                for (int j=1; j&gt;&lt;=total_port; j++) {
                    // Obtain the Device connected to the port.
                    Device device = root.getChild(j);
                    System.out.println( &quot;child: &quot; + j );

                  //  try{

                        if (device != null) {
                            System.out.println( &quot;device: &quot; + device.getPortIdentifier() );
                            DeviceDescriptor d = device.getDeviceDescriptor();
                            System.out.println(  &quot;getManufacturerStringId: &quot; +  d.getManufacturerStringId() );
                            System.out.println(  &quot;getVendorId: &quot; +  d.getVendorId() );
                            System.out.println(  &quot;getDeviceId: &quot; +  d.getDeviceId() );
                            System.out.println(  &quot;getProduct: &quot; +  d.getProduct(0) );
                            System.out.println(&quot;bcdUSB                   : &quot; + d.getUSBVersion());
                            System.out.println(&quot;Device Class             : &quot; + d.getDeviceClass()  + &quot; ( &quot; + d.getDeviceClassName() + &quot; )&quot;);
                            System.out.println(&quot;Device Subclass          : &quot; + d.getDeviceSubClass());
                            System.out.println(&quot;Device Protocol          : &quot; + d.getDeviceProtocol());
                            System.out.println(&quot;Max Packet Size          : &quot; + d.getMaxPacketSize0());
                            MainTesteJusb.vendID = &quot;0x&quot; + Integer.toHexString(d.getVendorId());
                            System.out.println(&quot;idVendor                 : &quot; + d.getVendorId() + &quot;      0x&quot; + Integer.toHexString(d.getVendorId()) + &quot;  &quot; + d.getManufacturer(1));
                            System.out.println(&quot;idProduct                : &quot; + d.getProductId()+ &quot;      0x&quot; + Integer.toHexString(d.getProductId()) + &quot;  &quot; + d.getProduct(0));
                            System.out.println(&quot;bcdDevice release number : &quot; + d.getDeviceId());
                            System.out.println(&quot;iManufacturer            : &quot; + d.getManufacturerStringId());
                            System.out.println(&quot;iProduct                 : &quot; + d.getProductStringId());
                            System.out.println(&quot;iSerialNumber            : &quot; + d.getSerialStringId() + &quot;  &quot; + d.getSerial(0));
                            System.out.println(&quot;NumConfiguration         : &quot; + d.getNumConfigurations());
                            System.out.println(  &quot;\n&quot;+&quot;_______________________________&quot;);
                            // Obtain the current Configuration of the device and the number of
                           // Interfaces available under the current Configuration.
                           Configuration config = device.getConfiguration();
                           int total_interface = config.getNumInterfaces();

                           //System.out.println( &quot;total_interface: &quot; + total_interface );

                           // Traverse through the Interfaces
                           for (int k=0; k&lt;total_interface; k++) {
                               // Access the currently Interface and obtain the number of
                               // endpoints available on the Interface.
                               Interface itf = config.getInterface(k, 0);
                               int total_ep  = itf.getNumEndpoints();

                               // Traverse through all the endpoints.
                               for (int l=0; l&gt;&lt;total_ep; l++){
                                   // Access the endpoint, and obtain its I/O type.
                                   Endpoint ep = itf.getEndpoint(l);
                                   String io_type = ep.getType();
                                   boolean input  = ep.isInput();

                                   System.out.println( "io_type: " + io_type );
                                   System.out.println( "total End Points: " + total_ep );
                                   System.out.println( "INput ?: " + input );
                                   System.out.println( "Type : " + ep.getType() );


                                   // If the endpoint is an input endpoint, obtain its
                                   // InputStream and read in data.
System.out.println( "Validação    " + input+ " " + ep.getType() + " " + vendID);
                                   if ( input && vendID.equals("0x43d") && ep.getType().contains("bulk")) {
//                                         //System.out.println( vendID );
//                                       File file = new File("C:/Print Evo/USBPJL.prime");
//                                       BufferedReader ins =  new BufferedReader(new InputStreamReader(ep.getInputStream()));
//                                       BufferedWriter bw2 = new BufferedWriter (new OutputStreamWriter (new FileOutputStream(file)));
//                                       for (String linha = ins.readLine(); linha != null   ; linha = ins.readLine())
//                                          {
//                                              bw2.write (linha); bw2.newLine();
//                                              System.out.println("1 "+linha);
//                                          }
//                                       bw2.close();
//                                       // Read in data here
//                                       ins.close();
                                         try {
                                          File f = new File("C:/Print Evo/USBPJL2.prime");
                                          DataOutputStream os = new DataOutputStream(ep.getOutputStream());
                                          //os.writeBytes("\033%-12345X@PJL JOB\r\n@PJL INFO PAGECOUNT\r\n@PJL EOJ\r\n\033%-12345X\r\n");
                                          os.writeBytes("\033%-12345X@PJL JOB\r\n@PJL ECHO PAGECOUNT\r\n@PJL EOJ\r\n\033%-12345X\r\n");
                                          BufferedReader in = new BufferedReader(new InputStreamReader(ep.getInputStream()));
                                          BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (new FileOutputStream(f)));
                                          for (String linha = in.readLine(); linha != null   ; linha = in.readLine())
                                            {
                                              bw.write (linha); bw.newLine();
                                              System.out.println("1 "+linha);
                                            }
                                          bw.close();
                                          System.out.println("3 ");
                                          in.close();

                                         // System.console();
                                          System.out.println("4 ");
                                          os.flush(); os.close();
                                          //ep.close();
                                          } catch (Exception e) {
                                              System.out.println(e.getMessage()+" ERRO AQUI !"+e.getLocalizedMessage());
                                              e.printStackTrace();
                                           }


                                   }
                                   //else {
                                   // If the Endpoint is and output Endpoint, obtain its
                                   // OutputStream and write out data.
//                                       OutputStream out;
//                                       System.out.println("Erro !!!!");
//                                       out = ep.getOutputStream();
//                                       // Write out data here.
//                                       out.close();
                                   //}
                               }
                           }
                        } else {
                            System.out.println( "Device nulo" );
                        }
               //     }catch ( Exception e ){
             //           e.printStackTrace();
              //      }
                }
            }
        } catch (Exception e)  {
            e.printStackTrace();
        }
        

    }

}
33%-12345X@PJL JOB\r\n@PJL ECHO PAGECOUNT\r\n@PJL EOJ\r\n
public class MainTesteJusb {

    private static String vendID = "";
    private String prodID = "";

    public static void main(String[] args) {

        
        try {
            // Bootstrap by getting the USB Host from the HostFactory.
            Host   host = HostFactory.getHost();
            // Obtain a list of the USB buses available on the Host.
            Bus[]  bus  = host.getBusses();
            //System.out.println("BUSSES : " + host.getDevice(path));
            int    total_bus = bus.length;

            System.out.println( "Total bus: " + total_bus );

            // Traverse through all the USB buses.
            for (int i=0; i &lt;total_bus; i++)  {

                System.out.println( "BUS: " + i );

                // Access the root hub on the USB bus and obtain the
                // number of USB ports available on the root hub.
                Device root = bus[i].getRootHub();
                int total_port = root.getNumPorts();

                System.out.println( "Total port: " + total_port );
                System.out.println( "identifier: " + root.getPortIdentifier() );
                System.out.println( "address: " + root.getAddress() );
                System.out.println( "speed: " + root.getSpeed() );
                System.out.println("___________________________"+"\n");

                // Traverse through all the USB ports available on the
                // root hub. It should be mentioned that the numbering
                // starts from 1, not 0.


                for (int j=1; j&gt;&lt;=total_port; j++) {
                    // Obtain the Device connected to the port.
                    Device device = root.getChild(j);
                    System.out.println( &quot;child: &quot; + j );

                  //  try{

                        if (device != null) {
                            System.out.println( &quot;device: &quot; + device.getPortIdentifier() );
                            DeviceDescriptor d = device.getDeviceDescriptor();
                            System.out.println(  &quot;getManufacturerStringId: &quot; +  d.getManufacturerStringId() );
                            System.out.println(  &quot;getVendorId: &quot; +  d.getVendorId() );
                            System.out.println(  &quot;getDeviceId: &quot; +  d.getDeviceId() );
                            System.out.println(  &quot;getProduct: &quot; +  d.getProduct(0) );
                            System.out.println(&quot;bcdUSB                   : &quot; + d.getUSBVersion());
                            System.out.println(&quot;Device Class             : &quot; + d.getDeviceClass()  + &quot; ( &quot; + d.getDeviceClassName() + &quot; )&quot;);
                            System.out.println(&quot;Device Subclass          : &quot; + d.getDeviceSubClass());
                            System.out.println(&quot;Device Protocol          : &quot; + d.getDeviceProtocol());
                            System.out.println(&quot;Max Packet Size          : &quot; + d.getMaxPacketSize0());
                            MainTesteJusb.vendID = &quot;0x&quot; + Integer.toHexString(d.getVendorId());
                            System.out.println(&quot;idVendor                 : &quot; + d.getVendorId() + &quot;      0x&quot; + Integer.toHexString(d.getVendorId()) + &quot;  &quot; + d.getManufacturer(1));
                            System.out.println(&quot;idProduct                : &quot; + d.getProductId()+ &quot;      0x&quot; + Integer.toHexString(d.getProductId()) + &quot;  &quot; + d.getProduct(0));
                            System.out.println(&quot;bcdDevice release number : &quot; + d.getDeviceId());
                            System.out.println(&quot;iManufacturer            : &quot; + d.getManufacturerStringId());
                            System.out.println(&quot;iProduct                 : &quot; + d.getProductStringId());
                            System.out.println(&quot;iSerialNumber            : &quot; + d.getSerialStringId() + &quot;  &quot; + d.getSerial(0));
                            System.out.println(&quot;NumConfiguration         : &quot; + d.getNumConfigurations());
                            System.out.println(  &quot;\n&quot;+&quot;_______________________________&quot;);
                            // Obtain the current Configuration of the device and the number of
                           // Interfaces available under the current Configuration.
                           Configuration config = device.getConfiguration();
                           int total_interface = config.getNumInterfaces();

                           //System.out.println( &quot;total_interface: &quot; + total_interface );

                           // Traverse through the Interfaces
                           for (int k=0; k&lt;total_interface; k++) {
                               // Access the currently Interface and obtain the number of
                               // endpoints available on the Interface.
                               Interface itf = config.getInterface(k, 0);
                               int total_ep  = itf.getNumEndpoints();

                               // Traverse through all the endpoints.
                               for (int l=0; l&gt;&lt;total_ep; l++){
                                   // Access the endpoint, and obtain its I/O type.
                                   Endpoint ep = itf.getEndpoint(l);
                                   String io_type = ep.getType();
                                   boolean input  = ep.isInput();

                                   System.out.println( "io_type: " + io_type );
                                   System.out.println( "total End Points: " + total_ep );
                                   System.out.println( "INput ?: " + input );
                                   System.out.println( "Type : " + ep.getType() );


                                   // If the endpoint is an input endpoint, obtain its
                                   // InputStream and read in data.
System.out.println( "Validação    " + input+ " " + ep.getType() + " " + vendID);
                                   if ( input && vendID.equals("0x43d") && ep.getType().contains("bulk")) {
//                                         //System.out.println( vendID );
//                                       File file = new File("C:/Print Evo/USBPJL.prime");
//                                       BufferedReader ins =  new BufferedReader(new InputStreamReader(ep.getInputStream()));
//                                       BufferedWriter bw2 = new BufferedWriter (new OutputStreamWriter (new FileOutputStream(file)));
//                                       for (String linha = ins.readLine(); linha != null   ; linha = ins.readLine())
//                                          {
//                                              bw2.write (linha); bw2.newLine();
//                                              System.out.println("1 "+linha);
//                                          }
//                                       bw2.close();
//                                       // Read in data here
//                                       ins.close();
                                         try {
                                          File f = new File("C:/Print Evo/USBPJL2.prime");
                                          DataOutputStream os = new DataOutputStream(ep.getOutputStream());
                                          //os.writeBytes("\033%-12345X@PJL JOB\r\n@PJL INFO PAGECOUNT\r\n@PJL EOJ\r\n\033%-12345X\r\n");
                                          os.writeBytes("\033%-12345X@PJL JOB\r\n@PJL ECHO PAGECOUNT\r\n@PJL EOJ\r\n\033%-12345X\r\n");
                                          BufferedReader in = new BufferedReader(new InputStreamReader(ep.getInputStream()));
                                          BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (new FileOutputStream(f)));
                                          for (String linha = in.readLine(); linha != null   ; linha = in.readLine())
                                            {
                                              bw.write (linha); bw.newLine();
                                              System.out.println("1 "+linha);
                                            }
                                          bw.close();
                                          System.out.println("3 ");
                                          in.close();

                                         // System.console();
                                          System.out.println("4 ");
                                          os.flush(); os.close();
                                          //ep.close();
                                          } catch (Exception e) {
                                              System.out.println(e.getMessage()+" ERRO AQUI !"+e.getLocalizedMessage());
                                              e.printStackTrace();
                                           }


                                   }
                                   //else {
                                   // If the Endpoint is and output Endpoint, obtain its
                                   // OutputStream and write out data.
//                                       OutputStream out;
//                                       System.out.println("Erro !!!!");
//                                       out = ep.getOutputStream();
//                                       // Write out data here.
//                                       out.close();
                                   //}
                               }
                           }
                        } else {
                            System.out.println( "Device nulo" );
                        }
               //     }catch ( Exception e ){
             //           e.printStackTrace();
              //      }
                }
            }
        } catch (Exception e)  {
            e.printStackTrace();
        }
        

    }

}
33%-12345X\r\n"); BufferedReader in = new BufferedReader(new InputStreamReader(ep.getInputStream())); BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (new FileOutputStream(f))); for (String linha = in.readLine(); linha != null ; linha = in.readLine()) { bw.write (linha); bw.newLine(); System.out.println("1 "+linha); } bw.close(); System.out.println("3 "); in.close();

// System.console();
System.out.println("4 ");
os.flush(); os.close();
//ep.close();
} catch (Exception e) {
System.out.println(e.getMessage()+" ERRO AQUI !"+e.getLocalizedMessage());
e.printStackTrace();
}

}
//else {
// If the Endpoint is and output Endpoint, obtain its
// OutputStream and write out data.
// OutputStream out;
// System.out.println("Erro !!!!");
// out = ep.getOutputStream();
// // Write out data here.
// out.close();
//}
}
}
} else {
System.out.println( "Device nulo" );
}
// }catch ( Exception e ){
// e.printStackTrace();
// }
}
}
} catch (Exception e) {
e.printStackTrace();
}

}

}

[/code]

Abraços.

>

2 Respostas

Thiago_Peter

Dê uma olhada no código disponível nesse site:

http://www.pikopong.com/blog/2008/05/11/printing-to-zebra-s4m-using-java-and-zpl-ii/

É para utilização com impresora Zebra, mas acho que serve para o seu caso, ao menos você irá conseguir localizar a impressora.

cardosao

Ola Thiago,

Obrigado pela resposta, porem ja testei PrintService[] na USB e nao consegui tambem fazer BufferedReader do comando que enviei.

To achando que vou ter que procurar algo em outra linguagem e usar wrapper.

Porem se mais alguem quiser tentar ajudar agradeço.

abraços.

Criado 28 de janeiro de 2011
Ultima resposta 30 de jan. de 2011
Respostas 2
Participantes 2