[英]how to open cash drawers connected to computer
I recently created a java program which prints a receipt and open up the cash drawer that is connected to the printer. 我最近创建了一个java程序,它打印收据并打开连接到打印机的现金抽屉。
Now I need to open the cash drawer which will be directly connected to the computer via an RJ11. 现在我需要打开现金抽屉,它将通过RJ11直接连接到电脑。
Since I will not connect the cash drawer to the printer my feedPrinter
will be useless. 由于我不会将现金抽屉连接到打印机,因此我的
feedPrinter
将无用。 So how can I call and send the cash drawer the commands ? 那么如何调用现金抽屉并发送命令呢?
Here's my code 这是我的代码
public String openDrawer()
{
final byte[] openCD={27,112,0,60,120};
String s=new String(openCD);
commandSet+=s;
return s;
}
public static void main(String args[])
{
PrinterOptions p=new PrinterOptions();
p.openDrawer();
feedPrinter(p.finalCommandSet().getBytes());
}
private static boolean feedPrinter(byte[] b)
{
try
{
AttributeSet attrSet = new HashPrintServiceAttributeSet(new PrinterName("PRINTERNAME", null));
//what should I change PRINTERNAME to connect directly to cash drawer
DocPrintJob job = PrintServiceLookup.lookupPrintServices(null, attrSet)[0].createPrintJob();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(b, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(job);
job.print(doc, null);
pjDone.waitForDone();
System.out.println("Done !");
}
catch(javax.print.PrintException pex)
{
System.out.println("Printer Error " + pex.getMessage());
return false;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
public String finalCommandSet()
{
return commandSet;
}
First and foremost, get the drawer/cutter code for your printer from http://keyhut.com/popopen4.htm 首先,从http://keyhut.com/popopen4.htm获取打印机的抽屉/刀具代码
Drawer code for my printer is = 27,112,0,100,250
我的打印机的抽屉代码是= 27,112,0,100,250
Then, use the code below to open your cash drawer. 然后,使用下面的代码打开您的现金抽屉。
public void openCashDrawer() {
byte[] open = {27,112,0,100,(byte) 250};
// byte[] cutter = {29, 86,49};
PrintService pservice =
PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = pservice.createPrintJob();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(open,flavor,null);
PrintRequestAttributeSet aset = new
HashPrintRequestAttributeSet();
try {
job.print(doc, aset);
} catch (PrintException ex) {
System.out.println(ex.getMessage());
}
}
This should work. 这应该工作。
Thank you. 谢谢。
Not sure about the technicalities, but as code comment: 不确定技术细节,但作为代码评论:
String s = new String(openCD, StandardCharsets.ISO_8859_1);
feedPrinter(p.finalCommandSet().getBytes(StandardCharsets.ISO_8859_1));
Without specifying the encoding, the default platform encoding is used. 如果不指定编码,则使用默认平台编码。 Which is a non-portable way of programming.
这是一种不可移植的编程方式。 And on a Unicode, machine, say Linux with UTF-8 this would go wrong: the
openCD
is not a valid UTF-8 byte sequence. 在一台Unicode机器上,比如说带有UTF-8的Linux,这会出错:
openCD
不是一个有效的UTF-8字节序列。
But better of course: 但当然更好:
ByteArrayOuputStream commandSet;
public void openDrawer()
{
final byte[] openCD={27,112,0,60,120};
commandSet.write(openCD);
}
public static void main(String args[])
{
PrinterOptions p=new PrinterOptions();
p.openDrawer();
feedPrinter(p.commandSet.toByteArray());
}
Since the printer obviously is controlled by Esc sequences, it will work as is. 由于打印机显然是由Esc序列控制的,因此它将按原样工作。 For the printer, a sequence received via serial interface (or whatever) has exactly the same meaning as the same sequence received via a Centronics interface (or whatever).
对于打印机,通过串行接口(或其他)接收的序列与通过Centronics接口(或其他)接收的相同序列具有完全相同的含义。
You simply need to make sure that the new connect transports data correctly, which it has to anyways. 您只需要确保新连接正确传输数据,无论如何都要如此。
Update: 更新:
Be aware, however, that I possibly got you wrong: I thought you refer to connecting the printer via Centronics vs. connecting differently. 但请注意,我可能错了你:我认为你指的是通过Centronics连接打印机而不是连接。
You are talking about connecting the cash drawer directly to the computer without using the printer. 您正在谈论在不使用打印机的情况下将现金抽屉直接连接到计算机。 Then, of course, it will not work without further effort because then the Esc sequence won't be interpreted by the printer, or at least will not open the drawer.
然后,当然,如果没有进一步的努力它将无法工作,因为打印机不会解释Esc序列,或者至少不会打开抽屉。
If you provide details about the drawer interface, we all knew more. 如果您提供有关抽屉界面的详细信息,我们都知道更多。
The best is to connect the cash drawer to the receipt printer instead of directly to the computer, and sending and ESC command for open should work. 最好是将现金抽屉连接到收据打印机而不是直接连接到计算机,并且发送和ESC命令打开应该可以工作。 If however you cannot do that, I got it to work on some computers by using jnpout32pkg.dll to write directly to the RJ11-port.
但是,如果你不能这样做,我通过使用jnpout32pkg.dll直接写入RJ11端口让它在某些计算机上工作。 However, the adress of the RJ11 seems to be different for different hardware, so you have to find out which address to write to.
但是,RJ11的地址对于不同的硬件似乎有所不同,因此您必须找出要写入的地址。 But this approach might also enable you to read if the cash drawer is open or not (which is a new requirement here in norway as of now..)
但是这种方法也可以让你读取现金抽屉是否开放(这是挪威现在的新要求......)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.