Ver Mensaje Individual
  #10 (permalink)  
Antiguo 23/04/2011, 17:06
geminis19
 
Fecha de Ingreso: febrero-2006
Mensajes: 58
Antigüedad: 18 años, 2 meses
Puntos: 0
Respuesta: imprimiendo en Impresora ibm 4610 1NR

Hola muchas gracias HackmanC por tu ayuda, me sirvio mucho.

Te cuento que logre imprimir, de echo ya imprime una boleta, informe x, z, comentarios, abrir la caja registradora y varias cosas mas.

Ahora comparto el código para quienes lo necesiten:

Código Javascript:
Ver original
  1. import java.util.List;
  2. import giovynet.nativelink.SerialPort;
  3. import giovynet.serial.Baud;
  4. import giovynet.serial.Com;
  5. import giovynet.serial.Parameters;
  6.  
  7. public class LayoutBoleta
  8. {
  9.     Com com;
  10.     Boolean sw=false;
  11.    
  12.     public static void main(String[] args)
  13.     {
  14.         new LayoutBoleta();
  15.     }
  16.    
  17.     public LayoutBoleta()
  18.     {
  19.         Boolean estadoImpresion=true;
  20.         estadoImpresion=Imprimir("BOL");
  21.         estadoImpresion=Imprimir("X");
  22.         estadoImpresion=Imprimir("Z");
  23.        
  24.         if(estadoImpresion)
  25.             System.out.println("Impresión realizado con exito.");  
  26.         else
  27.             System.out.println("Error al imprimir.");  
  28.     }
  29.    
  30.     public Boolean Imprimir(String tipo)
  31.     {
  32.         if(tipo.equals("X"))
  33.         {
  34.             if(informeX())
  35.                 return true;
  36.             else
  37.                 return false;
  38.         }
  39.         else if(tipo.equals("Z"))
  40.         {
  41.             if(informeZ())
  42.                 return true;
  43.             else
  44.                 return false;
  45.         }
  46.         else if(tipo.equals("BOL"))
  47.         {
  48.             if(imprimirBoleta())
  49.                 return true;
  50.             else
  51.                 return false;
  52.         }
  53.         else
  54.             return false;
  55.     }
  56.    
  57.     public Boolean informeX()
  58.     {
  59.         if(openCom("COM1"))
  60.         {
  61.             //Linea Informe X
  62.             if(setCommand("01", ""))
  63.             {
  64.                 if(closeCom())
  65.                     return true;
  66.                 else
  67.                     return false;
  68.             }
  69.             else
  70.             {
  71.                 closeCom();
  72.                 return false;
  73.             }
  74.         }
  75.         else
  76.            return false;
  77.     }
  78.    
  79.     public Boolean informeZ()
  80.     {
  81.         if(openCom("COM1"))
  82.         {
  83.             //Linea Informe Z
  84.             if(setCommand("021", ""))
  85.             {
  86.                 //Línea Apertura Período de Ventas, Valida modo entrenamiento,
  87.                 //en modo fiscal retorna error en caso de haber pasado 27 horas
  88.                 setCommand("32", "");
  89.                
  90.                 if(closeCom())
  91.                     return true;
  92.                 else
  93.                     return false;
  94.             }
  95.             else
  96.             {
  97.                 closeCom();
  98.                 return false;
  99.             }
  100.         }
  101.         else
  102.            return false;
  103.     }
  104.        
  105.     public Boolean imprimirBoleta()
  106.     {
  107.         if(openCom("COM1"))
  108.         {
  109.            
  110.             //Línea Apertura Período de Ventas, Valida modo entrenamiento,
  111.             //en modo fiscal retorna error error en caso de haber pasado 27 horas
  112.             //sw=setCommand("32", "");
  113.  
  114.             //Linea inicio Boleta
  115.             if(!setCommand("001", ""))
  116.                 return false;
  117.            
  118.             //Línea Inicio Comentario
  119.             if(!setCommand("60", ""))
  120.                 return false;
  121.            
  122.             //Línea Salto de Línea
  123.             if(!setCommand("30", ""))
  124.                 return false;
  125.                
  126.             //Linea Comentario
  127.             if(!setCommand("111", "BOLETA FISCAL"))
  128.                 return false;
  129.            
  130.             //Línea Salto de Línea
  131.             if(!setCommand("30", ""))
  132.                 return false;
  133.            
  134.             //Línea Fin Comentario
  135.             if(!setCommand("61", ""))
  136.                 return false;
  137.            
  138.             //Línea Boleta Caja
  139.             if(!setCommand("12", ""))
  140.                 return false;
  141.            
  142.             //Línea venta Item/*
  143.             long totalBoleta=0;
  144.             for(int i=0; i<2; i++)
  145.             {
  146.                 //Agregando ceros a la izquierda del código            
  147.                 String codigo=formatNumber(13, "123"+i);
  148.                
  149.                 //Agregando ceros a la izquierda de la cantidad
  150.                 String cantidad=formatNumber(6, "100")+"000";
  151.                
  152.                 //Agregando ceros a la izquierda del precio unitario
  153.                 String precioUnitario=formatNumber(9, "200");
  154.                
  155.                 //Agregando ceros a la izquierda del precio total
  156.                 String precioTotal=formatNumber(9, "20000");
  157.                
  158.                 //Sumando el producto al total de la boleta
  159.                 totalBoleta+=20000;
  160.                    
  161.                 //Descripción Producto
  162.                 String descripcion="Producto 0"+i;
  163.                
  164.                 //Linea Agregar Item
  165.                 if(!setCommand("13111", codigo+""+cantidad+""+precioUnitario+""+precioTotal+""+descripcion))
  166.                     return false;
  167.             }
  168.            
  169.             //Linea Descuento Total
  170.             // if(!setCommand("1711", formatNumber(9, "10")))
  171.              // return false;
  172.            
  173.             //Agregando ceros a la izquierda al total de la boleta
  174.             String totalString=formatNumber(10, totalBoleta+"");
  175.            
  176.             //Línea Sub/total
  177.             if(!setCommand("19", totalString))
  178.                 return false;
  179.                
  180.             //Línea Total
  181.             if(!setCommand("20", totalString))
  182.                 return false;
  183.            
  184.             //Linea Formas pagos
  185.             if(!setCommand("2611", "01"+formatNumber(10, "50000")))//Efectivo
  186.                 return false;
  187.            
  188.            //Linea Fin pagos
  189.            if(!setCommand("271", formatNumber(9, "5000"))) //Donación
  190.                return false;
  191.            
  192.             //Línea Inicio Comentario
  193.             if(!setCommand("60", ""))
  194.                 return false;
  195.                
  196.             //Linea Comentario
  197.             if(!setCommand("111", "Rut Cajero(a): 3.333.333-3"))
  198.                 return false;
  199.            
  200.             //Linea Comentario
  201.             if(!setCommand("111", "Nombre Cajero(a): Ernesto Gonzales"))
  202.                 return false;    
  203.            
  204.             //Línea Fin Comentario
  205.             if(!setCommand("61", ""))
  206.                 return false;
  207.            
  208.             //Línea Fin, con corte de papel
  209.             if(!setCommand("99", ""))
  210.                 return false;
  211.            
  212.             //Línea Apertura de Cajón
  213.             if(!setCommand("34", "255255"))
  214.                 return false;
  215.                          
  216.             //Cerrando el puerto COM
  217.             closeCom();
  218.            
  219.             return true;
  220.         }
  221.         else
  222.            return false;
  223.     }
  224.    
  225.     public boolean openCom(String puerto)
  226.     {
  227.         SerialPort serialPort = new SerialPort();
  228.         List<String> portsFree = null;
  229.         try {
  230.             portsFree=serialPort.getFreeSerialPort();
  231.         }
  232.         catch (Exception e){
  233.             return false;
  234.         }
  235.  
  236.         if(portsFree!=null&&portsFree.size()>0)
  237.         {              
  238.             Parameters parameters = null;
  239.             try{
  240.                 parameters = new Parameters();
  241.             }
  242.             catch (Exception e){
  243.                 return false;
  244.             }
  245.            
  246.             parameters.setPort(puerto);
  247.             parameters.setBaudRate(Baud._19200);
  248.             parameters.setByteSize("8");
  249.             parameters.setParity("n");
  250.                
  251.             try{
  252.                 com=new Com(parameters);
  253.                 return true;
  254.             }
  255.             catch(Exception e) {
  256.                 System.out.println(e.getMessage());
  257.                 return false;
  258.             }
  259.         }
  260.         else
  261.             return false;
  262.     }
  263.    
  264.     public boolean closeCom()
  265.     {
  266.         try {
  267.             com.close();
  268.             return true;
  269.         }
  270.         catch (Exception e) {
  271.             return false;
  272.         }
  273.     }
  274.    
  275.     public boolean setCommand(String line, String comand)
  276.     {
  277.         try{
  278.             com.sendSingleData(135);
  279.         }
  280.         catch (Exception e) {
  281.             return false;
  282.         }
  283.        
  284.         String finalComand=line+comand;
  285.         for(int i=0; i<finalComand.toCharArray().length; i++)
  286.         {
  287.             try{
  288.                 com.sendSingleData(finalComand.toCharArray()[i]);
  289.             }catch(Exception e){
  290.                 return false;
  291.             }
  292.         }      
  293.        
  294.         try{
  295.             com.sendSingleData(136);
  296.            
  297.             if(line.equals("1711"))
  298.                 sw=true;
  299.             else
  300.                 sw=false;
  301.            
  302.             return getResponse(sw);
  303.         }
  304.         catch (Exception e) {
  305.             return false;
  306.         }
  307.     }
  308.    
  309.     public Boolean getResponse(Boolean sw)
  310.     {
  311.         byte bytes=0;
  312.         String response=new String();
  313.                
  314.         while(bytes!=13 && bytes!=12)
  315.         {
  316.             try{
  317.                 bytes=(byte)com.receiveSingleChar();
  318.             }catch(Exception e){
  319.                 return false;
  320.             }
  321.        
  322.             if(bytes==10)
  323.                 return true;
  324.             else
  325.                 response+=bytes; //En el objeto response de tipo String se alamace todo el código de retorna de la impresora en formato INT
  326.            
  327.             if(sw)
  328.            System.out.println("bytes: "+bytes);
  329.         }
  330.        
  331.         /**
  332.          * la variable response contiene
  333.          * la respuesta de impresora por el ultimo comando ingresadoo
  334.          * esta variable o objeto debe ser tratado según el manual de usuario
  335.          */
  336.          
  337.         return true;
  338.     }
  339.    
  340.     public String formatNumber(int largo, String number)
  341.     {
  342.         String cerosIzqierda=new String();
  343.         for(int i=0; i<(largo-number.length()); i++)
  344.             cerosIzqierda+="0";
  345.        
  346.         return cerosIzqierda+number;
  347.     }
  348. }

Requisitos:
1. Utilizar la siguiente libreria dll y Jar para acceder al puerto -> Descargar
2. Compilar javac -cp .;GiovynetDriver.jar LayoutBoleta.java
3. Ejecutar java -cp .;GiovynetDriver.jar LayoutBoleta

Nota: Ejecutar este comando una vez emitodo el informe Z
//Tipo 32 : Línea Apertura Período de Venta
setCommand("32", "")

Saludos...

Última edición por geminis19; 12/12/2011 a las 08:11