Ver Mensaje Individual
  #1 (permalink)  
Antiguo 06/04/2015, 14:41
dragonfire256
 
Fecha de Ingreso: septiembre-2011
Ubicación: Caracas
Mensajes: 240
Antigüedad: 12 años, 7 meses
Puntos: 9
Enviar comandos a la impresora via socket

Buenas tardes

Estoy intentando enviar comandos a una impresora de tarjetas via socket, pero no he logrado que funcione... Tengo un ejemplo en PHP, que es mas o menos asi:

Código PHP:
Ver original
  1. $ESC = chr(27); //Ascii character for Escape
  2. $CR = chr(13); // Ascii character for Carriage Return
  3. $cmd =  $ESC .  $command .  $CR; //Command is received as parameter
  4. socket_send($socket, $cmd, strlen($cmd), 0);
  5. socket_recv($socket, $respuesta, strlen($respuesta), 0);

Dicho ejemplo, esta funcionando perfectamente, pero el problema no es ese; el problema radica en que debo realizarlo en Java, y no esta funcionando con lo que he realizado, que es mas o menos asi:

Código Java:
Ver original
  1. char ESC = (char)27; //Ascii character for Escape
  2. char CR = (char) 13; //Ascii character for Carriage Return
  3. //I make another stuff here
  4. Socket socket = null;
  5. DataInputStream input = null;
  6. DataOutputStream output = null;
  7. // I make another stuff here
  8. socket = new Socket(address,port);
  9. input = new DataInputStream (socket.getInputStream());
  10. output  = new DataOutputStream (socket.getOutputStream());
  11. //I make another stuff here
  12. if (socket != null && input != null && output != null)
  13.             {
  14.                 try
  15.                 {
  16.                     String cmd=ESC+command+CR;
  17.                     byte[] message = cmd.getBytes();              
  18.                     output.writeShort(cmd.length());                                    
  19.                     output.writeBytes(cmd);
  20.  
  21.                     message = new byte[input.readShort()];                            
  22.                     input.readFully(message);                                                      
  23.                     response = new String(message);
  24.  
  25.                     salida.close();
  26.                     entrada.close();
  27.                     conec.close();
  28.                 }
  29.                 catch (Exception e)
  30.                 {
  31.                     System.out.println(e.toString());
  32.  
  33.                 }
  34.             }

He intentado enviando y recibiendo diferentes tipos de datos, aparte del tipo Short, pero no he podido hacer que funcione; no se si se debe a que estoy enviando la informacion de manera incorrecta, o estoy tratando de leer un tipo y la impresora me responde otro

Espero que hayan podido entender mi inquietud

Muchas gracias por su tiempo