问题
i'm trying to send commands to a card printer via socket, but i am unable to make it work. Right now, i have a program developed in php, that is working, but i need to make it work in Java. The program in PHP is something like this:
$ESC = chr(27); //Ascii character for Escape
$CR = chr(13); // Ascii character for Carriage Return
$cmd = $ESC . $command . $CR;
socket_send($socket, $cmd, strlen($cmd), 0);
socket_recv($socket, $respuesta, strlen($respuesta), 0);
In Java, i am making something like this
char ESC = (char)27; //Ascii character for Escape
char CR = (char) 13; //Ascii character for Carriage Return
//////////////
Socket socket = null;
DataInputStream input = null;
DataOutputStream output = null;
// I make another stuff here
socket = new Socket(address,port);
input = new DataInputStream (socket.getInputStream());
output = new DataOutputStream (socket.getOutputStream());
//I make another stuff here
if (socket != null && input != null && output != null)
{
try
{
String cmd=ESC+command+CR;
byte[] message = cmd.getBytes();
output.writeShort(cmd.length());
output.writeBytes(cmd);
message = new byte[input.readShort()];
input.readFully(message);
response = new String(message);
salida.close();
entrada.close();
conec.close();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
I tried with different type of data: int, UTF-8, long, etc, but doesn't seem to work. I don't know if the printer is expecting more information, or i am sending the wrong type of data
Let me summarize: the code in PHP is working great, but when i am trying to translate that code to Java, and send the commands to the printer via socket, but isn't working at all. I already read the manual, but doesn't help at all
P.D. The brand of the printer is Evolis
P.D. 2 Sorry if i am not explained very well, but english isn't my first language
Thanks in advance
回答1:
I finally found a solution. Let me share it with you
char ESC = (char)27; //Ascii character for ESCAPE
char CR = (char) 13; //Ascii character for Carriage Return
/////////////
Socket socket = null;
OutputStream output= null;
BufferedReader reader= null;
///////////
try
{
socket = new Socket(address,port);
socket.setSoTimeout(5000);
output = socket.getOutputStream();
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch (UnknownHostException e) {etc... }
/////////
if (socket != null && output != null)
{
try
{
String cmd=ESC+command+CR;
output.write(cmd.getBytes());
output.flush();
socket.shutdownOutput();
response = reader.readLine();
System.out.println(response.toString());
output.close();
socket.close();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
The key in this solution was the method socket.shutdownOutput();, because i think that the device is unable to send and receive data at the same time, so if you want a response from the device, you must close the output first (the manual doesn't help at all, so i am guessing right now)
Thanks everybody for your help
来源:https://stackoverflow.com/questions/29472618/send-printer-commands-via-socket-in-java