ee78a24c8d9d17145816724a846e026b1e0bfd9c.svn-base 9.77 KB
/*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");
//	}
	
}
*/