Curapica 发表于 2013-1-15 02:41:15

简单的串口通讯

java 打印机打印和开钱箱: http://leesonhomme.iteye.com/blog/548998
String os = System.getProperty("os.name");
        if(os.contains("Windows")){
            //Windows
            try {
                PrintWriter pw = new PrintWriter("lpt1");
                //普通打印
                pw.write("English-Windows"+"\n");
                pw.write("中文-Windows"+"\n");
                //打印机开钱箱指令
                char[] c = {27,'p',0,60,240};
                pw.write(c);
                pw.write("\n");
                pw.flush();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(PrintTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
            //Linux
            try {
                FileOutputStream fos = new FileOutputStream("/dev/lp0");
                //普通打印
                fos.write("English-Linux\n".getBytes());
                fos.write("中文-Linux\n".getBytes("GBK"));
                //打印机开钱箱指令
                char[] c = {27,'p',0,60,240};
                for(int i=0;i<c.length;i++){
                    fos.write(c);
                }
                fos.write("\n".getBytes());
                fos.flush();
            } catch (IOException ex) {
                Logger.getLogger(PrintTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

usb钱箱要装驱动。。。。我用的是CS350的。。。
 
 
不过C#用的
((char)27).ToString() + "p" + ((char)0).ToString() + ((char)60).ToString() + ((char)255).ToString()
也可以
 
网上查到的 javascript的 用MSCOMM32.OCX控件的怎么都不行, 不知道什么原因。MSCOMM32.OCX可以注册的
页: [1]
查看完整版本: 简单的串口通讯