-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlistener.java
228 lines (200 loc) · 8.33 KB
/
listener.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.io.*;
import java.util.*;
import javax.comm.*;
public class listener implements Runnable, SerialPortEventListener {
//public class listener {
public static String keyPadMsgs;
public static boolean listening;
static CommPortIdentifier portId;
static Enumeration portList;
/* static OutputStream outputStream;
static InputStream inputStream;
static SerialPort serialPort; */
static OutputStream outputStream;
static InputStream inputStream;
static SerialPort serialPort;
Thread readThread=null;
static PrintWriter outWml;
static PrintWriter outHtml;
static int SndCkSum;
static int SndKey;
static int SndKeyOff;
static int CkSum;
static int Key;
static int keyoff;
static int i;
public static void main(String[] args) {
// listener test = new listener();
listening=false;
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
//if (portId.getName().equals("/dev/term/a")) {
// listener reader = new listener();
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_EVEN);
} catch (UnsupportedCommOperationException e) {}
while (serialPort.isCTS() == false) {
System.out.print("CTS false, looping\n");
}
listener reader = new listener();
}
}
}
}
/************ Thread ***********/
public listener() {
if (!listening) {
System.out.println("Starting thread");
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
readThread = new Thread(this);
readThread.start();
listening=true;
}
else
System.out.println("Not starting thread");
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readByte = new byte[1];
byte[] readBuffer = new byte[34];
byte[] keyPad = new byte[32];
byte checkSum;
byte counter=0;
byte i=0;
boolean notDone=true;
boolean panel;
boolean status;
try {
panel=false;
status=false;
int numBytes = inputStream.read(readByte);
while (notDone && (inputStream.available() > 0)) {
if (readByte[0] == 3) {
inputStream.read(readByte);
if (readByte[0] == 0x25) {
notDone=false;
panel=true;
}
}
if (readByte[0] == 1) {
inputStream.read(readByte);
if (readByte[0] == 6) {
notDone=false;
status=true;
}
}
if (notDone) {
// Dump unrecognized data.
System.out.print(" Dumping " + Integer.toHexString((int) readByte[0] & 0xFF) );
inputStream.read(readByte);
}
}
if (notDone)
System.out.println("\nRan out of data??");
if (status) {
// Real time status message
i=0;
while (inputStream.available() < 4) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
counter++;
if (counter>100)
break;
}
if (counter<100) {
while (i < 4) {
inputStream.read(readByte);
readBuffer[i++]=readByte[0];
}
System.out.println("\nYeah!! Got a status msg!");
System.out.println(Integer.toHexString((int) readBuffer[0] & 0xFF) + " " + Integer.toHexString((int) readBuffer[1] & 0xFF) + " " + Integer.toHexString((int) readBuffer[2] & 0xFF) + " " + Integer.toHexString((int) readBuffer[3] & 0xFF));
System.out.println("Yeah!! Finished status msg!\n");
}
else
System.out.println("Timeout in status!");
}
if (panel) {
// Change in panel display
numBytes = inputStream.read(readByte); // Dump these two
numBytes = inputStream.read(readByte); // Dump these two
i=0;
while (inputStream.available() < keyPad.length) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
counter++;
if (counter>100)
break;
}
if (inputStream.available() >= keyPad.length + 1) {
numBytes = inputStream.read(keyPad);
inputStream.read(readByte);
checkSum=readByte[0];
}
if (counter<100) {
keyPadMsgs = new String(keyPad);
String firstLine = keyPadMsgs.substring(0,16);
String secondLine = keyPadMsgs.substring(16);
/* outWml = new PrintWriter(
new OutputStreamWriter(
new FileOutputStream("C:\\Program Files\\Apache Group\\Apache\\htdocs\\a.wml")));
outHtml = new PrintWriter(
new OutputStreamWriter(
new FileOutputStream("C:\\Program Files\\Apache Group\\Apache\\htdocs\\a.html")));
outHtml.write(firstLine + "<br>" + secondLine);
outWml.write("<?xml version=\"1.0\"?>");
outWml.write("<!DOCTYPE wml PUBLIC \"-//PHONE.COM//DTD WML 1.1//EN\"");
outWml.write("\"http://www.phone.com/dtd/wml11.dtd\"><wml>");
outWml.write("<head>");
outWml.write("<meta http-equiv=\"Cache_Control\" content=\"max-age=0\"/>");
outWml.write("</head>");
outWml.write("<card ontimer='a.wml'><timer value='30'/><p>");
outWml.write(firstLine + "<br/>" + secondLine);
outWml.write("</p></card></wml>");
outWml.close();
outHtml.close();
System.out.print(firstLine + "\n" + secondLine);
System.out.println("\n****************");*/
}
}
} catch (IOException e) {}
break;
}
}
}
// System.out.print (Integer.toHexString((int) readBuffer[i] & 0xFF) + "->" + (char) readBuffer[i] + " ");