ee78a24c8d9d17145816724a846e026b1e0bfd9c.svn-base
9.77 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*package com.thinkgem.jeesite.modules.reg.utils;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import org.springframework.beans.factory.annotation.Autowired;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.reg.entity.bus.RegBusSlsq;
import com.thinkgem.jeesite.modules.reg.service.bus.RegBusSlsqService;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
public class SerialPortUtilityNew implements Runnable,SerialPortEventListener {
// 检测系统中可用的通讯端口类
private CommPortIdentifier portId;
// Enumeration 为枚举型类
private Enumeration<CommPortIdentifier> portList;
// 输入输出流
private InputStream inputStream;
private OutputStream outputStream;
// RS-232的串行口
private static SerialPort serialPort;
//保存串口返回信息
public String Serialinfo = "";
//短信内容
String dxinfo = "" ;
//字符位数
int zfint = 0;
//受理控制类
RegBusSlsq slsq = null ;
//ctrl+z 指令
private final char symbol3 = 26;
//本来是static类型的
Thread readThread;
@Autowired
private RegBusSlsqService regBusSlsqService;
*//**
* 初始化串口
*//*
public void initPort() {
// 获取系统中所有的通讯端口
portList = CommPortIdentifier.getPortIdentifiers();
// 用循环结构找出串口
while (portList.hasMoreElements()) {
// 强制转换为通讯端口类型
portId = (CommPortIdentifier) portList.nextElement();
// 判断是否为串口
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
//比较串口名称是否为“COM3”
if (portId.getName().equals("COM3")) {
// 打开串口
try {
// open参数:(应用程序名【随意命名】,阻塞时等待的毫秒数)
serialPort = (SerialPort) portId.open("GSM", 2000);
设置串口通讯参数
// 波特率,数据位,停止位和校验方式
// 波特率115200,偶校验
serialPort.setSerialPortParams(115200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
Serialinfo = "";
outputStream = serialPort.getOutputStream();
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (PortInUseException e) {
e.printStackTrace();
}
}else{
System.out.println("没有找端口!!!");
}
}
}
}
*//**
* 设置监听端口
* @param serialPort
*//*
private void listenSerialPort(SerialPort serialPort) {
if (serialPort != null){
try {
// 设置串口监听
serialPort.addEventListener(this);
// 设置可监听
serialPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
}
}
*//**
* 判断返回信息
* @param data
* @return
*//*
public boolean isRecOK(String data) {
final String OK_FLAG = "OK";
int index1 = 0;
if (data != null) {
index1 = data.indexOf(OK_FLAG);
if (index1 >= 0 && index1 + 4 <= data.length()) {
String t = data.substring(index1 + 2);
byte[] b = t.getBytes();
if (b.length >= 2) {
if (b[0] == 0x0D && b[1] == 0x0A)
return true;
}
}
}
return false;
}
*//**
* 实现接口SerialPortEventListener中的方法 读取从串口中接收的数据
BI - 通讯中断.
CD - 载波检测.
CTS - 清除发送.
DATA_AVAILABLE - 有数据到达.
DSR - 数据设备准备好.
FE - 帧错误.
OE - 溢位错误
OUTPUT_BUFFER_EMPTY - 输出缓冲区已清空.
PE - 奇偶校验错.
RI - 振铃指示.
*//*
public synchronized void serialEvent(SerialPortEvent event) {
//ctrl+z 指令
final char ctrlz = 26;
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: //有数据到达
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
readComm();
System.out.println("有消息来了,被触发了!返回值是"+Serialinfo+"有问题吗?");
System.out.println(Serialinfo.indexOf("+CMGS:"));
if(Serialinfo.contains("OK") && Serialinfo.indexOf("+CMGS:") == -1){
Serialinfo = "";
fsinfo("AT+CMGS="+zfint+"\r");
break;
}
if(Serialinfo.contains(">")){
zfint = 0;
Serialinfo = "";
fsinfo(dxinfo+String.valueOf(ctrlz));
dxinfo="";
break;
}
//最后一笔消息反馈比较慢
if(Serialinfo.indexOf("+CMGS:") != -1){
System.out.println("短信发送成功啦!!!");
Serialinfo = "";
slsq= null;
break;
}
default:
break;
}
}
*//**
* 读取串口返回信息
*//*
public void readComm(){
DataInputStream in;
int c = 0;
StringBuffer sb = new StringBuffer();
if (serialPort != null){
try {
in = new DataInputStream(serialPort.getInputStream());
while ((c = in.read()) != -1) {
sb.append((char) c);
}
Serialinfo = sb.toString();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void startinit(RegBusSlsq regBusSlsq){
slsq = regBusSlsq;
readThread = new Thread(this);
readThread.start();
}
public void run(){
//动态字符串
StringBuffer sb = new StringBuffer();
//获取当前业务号查询申请人电话号码
String yddh = slsq.getTzryddh();
String jzzh = "";
String tzrxm = slsq.getTzrxm().replace(" ", "");
tzrxm = tzrxm.replace(" ", "");
//通知人姓名
if(StringUtils.isNotBlank(yddh)){
//开始拼接要发送的短信内容
if(yddh.startsWith("1") && yddh.length() == 11){
//电话号码两两互换位置
String[] strdyhm = {yddh.substring(1,2),yddh.substring(0,1),yddh.substring(3,4),yddh.substring(2,3)
,yddh.substring(5,6),yddh.substring(4,5),yddh.substring(7,8),yddh.substring(6,7)
,yddh.substring(9,10),yddh.substring(8,9),"F",yddh.substring(10,11)};
for(int i = 0; i < strdyhm.length; i++){
sb. append(strdyhm[i]);
}
yddh = sb.toString();
//转换unico码
String dxnrascii = stringToUnicode(tzrxm.trim()+"您好,您申请办理的不动产登记业务已完成。请您携本人身份证,受理通知单至市不动产交易服务中心缴费领证。(汉中市不动产交易服务中心)");
//计算内容长度转换为16进制
jzzh = Integer.toHexString(dxnrascii.length()/2);
//拼接短信内容
dxinfo = "0031000b81"+yddh+"0008a7"+jzzh+dxnrascii;
zfint = (dxinfo.length()-2)/2;
}
}
//判断是否有返回值
String infomessage = "";
//初始化串口
if(serialPort == null){
initPort();
//添加监听
listenSerialPort(serialPort);
}
if(StringUtils.isBlank(infomessage)){
//向设备发送指令,设定Unicode 方式
fsinfo("AT+CMGF=0\r");
}
}
*//**
* 关闭串口
*//*
public void closeSerialPort() {
serialPort.close();
}
*//**
* 向串口发送信息方法字/符串
*//*
public void sendMsg(String information,SerialPort sPort){
PrintWriter pw;
if (information != null && serialPort != null){
try {
pw = new PrintWriter(serialPort.getOutputStream());
pw.write(information);
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
*//**
* 向串口发送信息方法/字节
*//*
public void sendMsgbyte(byte[] information,SerialPort sPort){
System.out.println(information);
DataOutputStream pw;
if (information != null && serialPort != null){
try {
pw = new DataOutputStream(serialPort.getOutputStream());
pw.write(information);
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
*//**
* 字符串转换ASCII码
* @param letter
* @return
*//*
public String letterToH(String letter) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < letter.length(); i++) {
char c = letter.charAt(i);
sb.append(Integer.toHexString(c));
}
sb.deleteCharAt(sb.length() - 2);
return sb.toString();
}
*//**
* 字符串转换unicode
* @param string
* @return
*//*
public String stringToUnicode(String string) {
StringBuffer unicode = new StringBuffer();
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i); // 取出每一个字符
unicode.append(Integer.toHexString(c));// 转换为unicode
}
return unicode.toString();
}
*//**
*
*//*
public void fsinfo(String cmdstr){
if(serialPort == null){
initPort();
//添加监听
listenSerialPort(serialPort);
}
sendMsg(cmdstr, serialPort);
}
// public static void main(String[] args){
// SerialPortUtilityNew portnew = new SerialPortUtilityNew();
// portnew.fsinfo("AT+CMGF=0\r");
// }
}
*/