-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZebraKR203ReadStatus.java
65 lines (48 loc) · 1.96 KB
/
ZebraKR203ReadStatus.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
public class ZebraKR203ReadStatus {
public static void main(String[] args) throws Exception {
OutputStream oStream = new FileOutputStream("/dev/usb/lp0");
PrintWriter printer = new PrintWriter(new OutputStreamWriter(oStream, "ISO_8859_1"));
String kpl_str_full = "";
kpl_str_full += new StringBuilder().append((char) 0x1b).append((char) 0x26).append((char) 0x50).append((char) 0x41).append((char) 0x00).toString();
kpl_str_full += new StringBuilder().append((char) 0x1b).append((char) 0x26).append((char) 0x50).append((char) 0x42).append((char) 0x00).toString();
kpl_str_full += new StringBuilder().append((char) 0x1b).append((char) 0x05).append((char) 0x01).toString();
printer.print(kpl_str_full);
printer.flush();
printer.close();
oStream.close();
AsynchronousFileChannel afc = AsynchronousFileChannel.open(Paths.get("/dev/usb/lp0"), StandardOpenOption.READ);
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.clear();
byte[] receive;
Future future;
try {
future = afc.read(buffer, 0);
future.get(500, TimeUnit.MILLISECONDS);
// flip from filling to emptying
buffer.flip();
receive = new byte[ buffer.remaining() ];
buffer.get(receive);
for (byte b : receive) {
String st = String.format("%02X", b);
System.out.print(st);
}
buffer.clear();
} catch (TimeoutException e) {
System.out.println("Timeout");
}
}
}