通过Java中的socket发送打印机命令 [英] Send printer commands via socket in Java

查看:917
本文介绍了通过Java中的socket发送打印机命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过套接字向卡片打印机发送命令,但我无法使其正常工作。现在,我有一个用PHP开发的程序,它正在运行,但我需要让它在Java中运行。 PHP中的程序是这样的:

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);

在Java中,我正在做这样的事情

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());

                }
            }

我试过不同类型的数据:int,UTF-8,long等,但似乎不起作用。我不知道打印机是否需要更多信息,或者我发送错误类型的数据

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

让我总结一下:PHP中的代码运行良好,但是当我试图将该代码转换为Java,并通过套接字将命令发送到打印机,但根本不工作。我已经阅读了手册,但根本没有帮助

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。打印机的品牌是Evolis

P.D. The brand of the printer is Evolis

P.D。 2对不起,如果我的解释不是很好,但英语不是我的第一语言

P.D. 2 Sorry if i am not explained very well, but english isn't my first language

提前致谢

推荐答案

我终于找到了解决方案。让我与你分享

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());

                }
            }

此解决方案的关键是方法 socket.shutdownOutput(); ,因为我认为设备无法同时发送和接收数据,因此如果您想要设备的响应,则必须先关闭输出(手册根本没有帮助,所以我现在正在猜测。)

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)

感谢大家的帮助

这篇关于通过Java中的socket发送打印机命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆