List of usage examples for org.apache.poi.util HexDump toHex
public static String toHex(final byte[] value, final int bytesPerLine)
Converts the parameter to a hex value breaking the results into lines.
From source file:org.openconcerto.erp.core.sales.pos.io.ConcertProtocol.java
License:Open Source License
public boolean sendPrototolE(int posIndex, int amountInCents, boolean requireResponse, char mode, char type, String currency, String string) throws Exception { boolean result = false; if (posIndex > 99 || posIndex < 0) { throw new IllegalArgumentException("Pos index must be between 0 and 99"); }/*from w w w . ja v a 2 s.c o m*/ if (amountInCents < 0) { throw new IllegalArgumentException("Amount must be positive"); } if (currency.length() != 3) { throw new IllegalArgumentException("Bad currency code : " + currency); } final SerialPort serialPort = getSerialPort(); final OutputStream out = serialPort.getOutputStream(); final InputStream in = serialPort.getInputStream(); out.write(ENQ); byte[] buffer = new byte[512]; int nbRead = in.read(buffer); if (nbRead != 1 || buffer[0] != ACK) { String r = HexDump.toHex(buffer, nbRead); serialPort.close(); throw new IllegalStateException("Bad response received : " + r); } // final ByteArrayOutputStream bOut = new ByteArrayOutputStream(); bOut.write(STX); // POS Index String n = rightAlign(posIndex, 2, '0'); bOut.write(n.getBytes()); // Amount in cents bOut.write(rightAlign(amountInCents, 8, '0').getBytes()); if (requireResponse) { bOut.write('1'); } else { bOut.write('0'); } // Mode & type bOut.write(mode); bOut.write(type); // Currency bOut.write(currency.getBytes()); // Text bOut.write(leftAlign(string, 10, ' ').getBytes()); bOut.write(ETX); byte b = getLrc(bOut.toByteArray()); bOut.write(b); out.write(bOut.toByteArray()); // READ ACK nbRead = in.read(buffer); if (nbRead != 1 || buffer[0] != ACK) { String r = HexDump.toHex(buffer, nbRead); serialPort.close(); throw new IllegalStateException("Bad response received : " + nbRead + ": " + r); } // END out.write(EOT); // Wait reply int count = 0; final int maxCount = 60 * 5; while (count < maxCount) { nbRead = in.read(buffer); if (nbRead > 0) { if (buffer[0] == ENQ) { out.write(ACK); } else if (buffer[0] == STX) { if (buffer[3] == '0') { result = true; out.write(ACK); count = maxCount; } else if (buffer[3] == '7') { out.write(NACK); count = maxCount; } } else if (buffer[0] == EOT) { count = maxCount; } } Thread.sleep(200); count++; } out.close(); in.close(); serialPort.close(); return result; }