title: How to send ESC/POS commands to an usb printer?
body: <p>My objective is opening a cash drawer programatically,  but I didn't  found detailed information about how Java interact with Windows ports so I couldn't get it working. These are the methods I tried(no errors in Java console):</p>
  
  <pre><code>public void cashdrawerOpen()   {
  
      String code1 = "27 112 0 150 250"; //decimal
      String code2 = "1B 70 00 96 FA"; //hexadecimal
      String code = "ESCp0û."; //ascii
  
       PrintService service = PrintServiceLookup.lookupDefaultPrintService();
       System.out.println(service.getName());
       DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
      DocPrintJob pj = service.createPrintJob();
       byte[] bytes;
       bytes=code2.getBytes();
       Doc doc=new SimpleDoc(bytes,flavor,null);
        try {
          pj.print(doc, null);
      } catch (PrintException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  
  
  public void cashdrawerOpen2(){
      String code1 = "27 112 0 150 250";
      String code2 = "1B 70 00 96 FA";
      String code = "ESCp0û.";
      FileOutputStream os = null;
      try {
          os = new FileOutputStream("USB001:POS-58");
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }
        PrintStream ps = new PrintStream(os);
        ps.print(code1.getBytes());
        ps.close();
  }
  </code></pre>
  
  <p>Then I started playing with cmd, specifically following <a href="https://stackoverflow.com/questions/11309054/opening-a-cash-drawer-using-java-or-batch-file">this</a> thread, but when I execute the command 'copy /b open.bat USB001' it just says: 'overwrite USB001 ? (yes/no/all)'</p>
  
  <p>Any idea?</p>