M1Demo.java
4.22 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package d8demo;
import com.sun.jna.Native;
/**
* m1¿¨²Ù×÷demo
* @author decard
*/
public class M1Demo {
public static void print_bytes(byte[] b, int length) {
for (int i = 0; i < length; ++i) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
System.out.print(hex.toUpperCase());
}
}
public static void main(String[] args) {
Dcrf32_h dcrf32;
dcrf32 = (Dcrf32_h) Native.loadLibrary("./dcrf32.dll", Dcrf32_h.class);
int result;
int handle;
int[] snr = new int[1];
byte[] send_buffer = new byte[2048];
byte[] recv_buffer = new byte[2048];
System.out.print("dc_init ... ");
result = dcrf32.dc_init((short) 100, 115200);
if (result < 0) {
System.out.println("error!");
return;
}
System.out.println("ok!");
handle = result;
System.out.print("dc_card ... ");
result = dcrf32.dc_config_card(handle, (byte) 0x41);//ÉèÖ÷ǽӿ¨ÐÍΪA
result = dcrf32.dc_card(handle, (byte) 0, snr);
if (result != 0) {
System.out.println(result);
System.out.println("error!");
dcrf32.dc_exit(handle);
return;
}
System.out.println("ok!");
recv_buffer[0] = (byte) ((snr[0] >>> 24) & 0xff);
recv_buffer[1] = (byte) ((snr[0] >>> 16) & 0xff);
recv_buffer[2] = (byte) ((snr[0] >>> 8) & 0xff);
recv_buffer[3] = (byte) ((snr[0] >>> 0) & 0xff);
print_bytes(recv_buffer, 4);
System.out.println("");
byte[] pass = new byte[6];
pass[0] = (byte) 0xff;
pass[1] = (byte) 0xff;
pass[2] = (byte) 0xff;
pass[3] = (byte) 0xff;
pass[4] = (byte) 0xff;
pass[5] = (byte) 0xff;
System.out.print("dc_load_key ... ");
result = dcrf32.dc_load_key(handle, (byte) 0, (byte) 0, pass);
if (result != 0) {
System.out.println("error!");
dcrf32.dc_exit(handle);
return;
}
System.out.println("ok!");
System.out.print("dc_authentication ... ");
result = dcrf32.dc_authentication(handle, (byte) 0, (byte) 0);
if (result != 0) {
System.out.println("error!");
dcrf32.dc_exit(handle);
return;
}
System.out.println("ok!");
System.out.print("dc_write ... ");
send_buffer[0] = (byte) 0x41;
send_buffer[1] = (byte) 0x42;
send_buffer[2] = (byte) 0x43;
send_buffer[3] = (byte) 0x44;
send_buffer[4] = (byte) 0x45;
send_buffer[5] = (byte) 0x46;
send_buffer[6] = (byte) 0x47;
send_buffer[7] = (byte) 0x48;
send_buffer[8] = (byte) 0x49;
send_buffer[9] = (byte) 0x4A;
send_buffer[10] = (byte) 0x4B;
send_buffer[11] = (byte) 0x4C;
send_buffer[12] = (byte) 0x4D;
send_buffer[13] = (byte) 0x4E;
send_buffer[14] = (byte) 0x4F;
send_buffer[15] = (byte) 0x50;
result = dcrf32.dc_write(handle, (byte) 2, send_buffer);
if (result != 0) {
System.out.println("error!");
dcrf32.dc_exit(handle);
return;
}
System.out.println("ok!");
System.out.print("dc_read ... ");
result = dcrf32.dc_read(handle, (byte) 2, recv_buffer);
if (result != 0) {
System.out.println("error!");
dcrf32.dc_exit(handle);
return;
}
System.out.println("ok!");
print_bytes(recv_buffer, 16);
System.out.println("");
System.out.print("dc_beep ... ");
result = dcrf32.dc_beep(handle, (short) 10);
if (result != 0) {
System.out.println("error!");
dcrf32.dc_exit(handle);
return;
}
System.out.println("ok!");
System.out.print("dc_exit ... ");
result = dcrf32.dc_exit(handle);
if (result != 0) {
System.out.println("error!");
return;
}
System.out.println("ok!");
}
}