简体   繁体   English

通过Java中的套接字发送打印机命令

[英]Send printer commands via socket in Java

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. 现在,我有一个用php开发的程序,正在运行,但是我需要使其在Java中工作。 The program in PHP is something like this: PHP中的程序如下所示:

$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 在Java中,我正在做这样的事情

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. 我尝试了不同类型的数据:int,UTF-8,long等,但似乎不起作用。 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. 让我总结一下:PHP中的代码运行良好,但是当我尝试将该代码转换为Java,并通过套接字将命令发送到打印机时,却根本无法正常工作。 I already read the manual, but doesn't help at all 我已经阅读了手册,但是完全没有帮助

PD The brand of the printer is Evolis PD打印机的品牌是Evolis

PD 2 Sorry if i am not explained very well, but english isn't my first language PD 2对不起,如果我不能很好地解释我,但英语不是我的母语

Thanks in advance 提前致谢

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(); 此解决方案中的关键是方法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 谢谢大家的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM