cserialdriver.java

来自「SMSLib一个很有用的程序,有服务平台和收发平台」· Java 代码 · 共 295 行

JAVA
295
字号
//	SMSLib for Java
//	An open-source API Library for sending and receiving SMS via a GSM modem.
//	Copyright (C) 2002-2006, Thanasis Delenikas, Athens/GREECE
//		Web Site: http://www.smslib.org
//
//	SMSLib is distributed under the LGPL license.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

package org.smslib;

import java.io.*;
import java.util.*;

//import gnu.io.*;
import javax.comm.*;

class CSerialDriver  implements SerialPortEventListener
{
	private static final int RECV_TIMEOUT = 30 * 1000;
	private static final int WAIT_DATA_RETRIES = 10;

	private static final int BUFFER_SIZE = 16384;

	private String port;
	private int baud;

	private CommPortIdentifier portId;
	private SerialPort serialPort;
	private InputStream inStream;
	private OutputStream outStream;
	private CNewMsgMonitor newMsgMonitor;
	private boolean stopFlag;

	private CLogger log;

	public CSerialDriver(String port, int baud, CLogger log)
	{
		this.port = port;
		this.baud = baud;
		this.log = log;
		newMsgMonitor = null;
		stopFlag = false;
	}

	public void setPort(String port) { this.port = port; }
	public String getPort() { return port; }
	public int getBaud() { return baud; }
	public void setNewMsgMonitor (CNewMsgMonitor monitor) { this.newMsgMonitor = monitor; }
	public void killMe() { stopFlag = true; }

	public void open() throws Exception
	{
		Enumeration portList;

		log.log(CLogger.LOG_INFO, "CSerialDriver(): Connecting...");

		portId = CommPortIdentifier.getPortIdentifier(getPort());
		serialPort = (SerialPort) portId.open("SMSLib", 1971);
		inStream = serialPort.getInputStream();
		outStream = serialPort.getOutputStream();
		serialPort.notifyOnDataAvailable(true);
		serialPort.notifyOnOutputEmpty(true);
		serialPort.notifyOnBreakInterrupt(true);
		serialPort.notifyOnFramingError(true);
		serialPort.notifyOnOverrunError(true);
		serialPort.notifyOnParityError(true);
		serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
		serialPort.addEventListener(this);
		serialPort.setSerialPortParams(getBaud(), SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
		serialPort.setInputBufferSize(BUFFER_SIZE);
		serialPort.setOutputBufferSize(BUFFER_SIZE);
		serialPort.enableReceiveTimeout(RECV_TIMEOUT);
	}

	public void close()
	{
		log.log(CLogger.LOG_INFO, "CSerialDriver(): Disconnecting...");
		try { serialPort.close(); } catch (Exception e) {}
	}

	public void serialEvent(SerialPortEvent event)
	{
		switch(event.getEventType())
		{
			case SerialPortEvent.BI:
				break;
			case SerialPortEvent.OE:
				log.log(CLogger.LOG_ERROR, "COMM-ERROR: Overrun Error!");
				break;
			case SerialPortEvent.FE:
				log.log(CLogger.LOG_ERROR, "COMM-ERROR: Framing Error!");
				break;
			case SerialPortEvent.PE:
				log.log(CLogger.LOG_ERROR, "COMM-ERROR: Parity Error!");
				break;
			case SerialPortEvent.CD:
				break;
			case SerialPortEvent.CTS:
				break;
			case SerialPortEvent.DSR:
				break;
			case SerialPortEvent.RI:
				break;
			case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
				break;
			case SerialPortEvent.DATA_AVAILABLE:
				if (newMsgMonitor != null) newMsgMonitor.raise(CNewMsgMonitor.DATA);
				break;
		}
	}

	public void clearBufferCheckCMTI() throws Exception
	{
		StringBuffer buffer = new StringBuffer(BUFFER_SIZE);

		try
		{
			while (dataAvailable())
			{
				int c = inStream.read();
				if (c == -1) break;
				buffer.append((char) c);
			}
			if (newMsgMonitor != null && newMsgMonitor.getState() != CNewMsgMonitor.CMTI)
				newMsgMonitor.raise (buffer.toString().indexOf("+CMTI:") >= 0 ? CNewMsgMonitor.CMTI : CNewMsgMonitor.IDLE);
		}
		catch (Exception e) {}
	}

	public void clearBuffer() throws Exception
	{
		
		int retries = WAIT_DATA_RETRIES;

		while (!dataAvailable())
		{
			Thread.sleep(200);
			retries --;
			if (retries == 0) throw new NoResponseException();
		}
		Thread.sleep(200);
		clearBufferCheckCMTI();
	}

	public void send(String s) throws Exception
	{
		log.log(CLogger.LOG_INFO, "TE: " + formatLog(new StringBuffer(s)));
		for (int i = 0; i < s.length(); i ++)
		{
			outStream.write((byte) s.charAt(i));
		}
		outStream.flush();
	}

	public void send(char c) throws Exception
	{
		outStream.write((byte) c);
		outStream.flush();
	}

	public void skipBytes(int numOfBytes) throws Exception
	{
		int count, c;

		count = 0;
		while (count < numOfBytes)
		{
			c = inStream.read();
			if (c != -1) count ++;
		}
	}

	public boolean dataAvailable() throws Exception
	{
		return (!stopFlag && inStream.available() > 0 ? true : false);
	}

	public String getResponse() throws Exception
	{
		final int RETRIES = 3;
		final int WAIT_TO_RETRY = 1000;
		StringBuffer buffer;
		String response;
		int c, retry;

		retry = 0;
		buffer = new StringBuffer(BUFFER_SIZE);

		while (retry < RETRIES)
		{
			try
			{
				while (true)
				{
					if (stopFlag) return "+ERROR:\r\n";
					c = inStream.read();
					if (c == -1)
					{
						buffer.delete(0, buffer.length());
						break;
					}
					buffer.append((char) c);
					response = buffer.toString();
					if ((response.indexOf("\n") > 0) && ((response.indexOf("OK") > 0) || (response.indexOf("ERROR") > 0) || (response.indexOf("SIM PIN") > 0) || (response.indexOf("CMTI") > 0)))
					{
						if (response.matches("\\s*[\\p{ASCII}]*\\s+OK\\s")) break;
						if (response.matches("\\s*[\\p{ASCII}]*\\s+ERROR\\s")) break;
						if (response.matches("\\s*[\\p{ASCII}]*\\s+ERROR: \\d+\\s")) break;
						if (response.matches("\\s*[\\p{ASCII}]*\\s+SIM PIN\\s")) break;
						if (response.matches("\\s*[+]CMTI[:][^\r\n]*[\r\n]"))
						{
							buffer.delete(0, buffer.length());
							if (newMsgMonitor != null) newMsgMonitor.raise(CNewMsgMonitor.CMTI);
							continue;
						}
					}
				}
				retry = RETRIES;
			}
			catch (Exception e)
			{
				if (retry < RETRIES)
				{
					Thread.sleep(WAIT_TO_RETRY);
					retry ++;
				}
				else throw e;
			}
		}
		log.log(CLogger.LOG_INFO, "ME: " + formatLog(buffer));
		clearBufferCheckCMTI();
		return buffer.toString();
	}

	private String formatLog(StringBuffer s)
	{
		String response;
		int i;

		response = "";
		for (i = 0; i < s.length(); i ++)
		{
			switch (s.charAt(i))
			{
				case 13 :
					response += "(cr)";
					break;
				case 10 :
					response += "(lf)";
					break;
				case 9 :
					response += "(tab)";
					break;
				default:
					response += "(" + (int) s.charAt(i) + ")";
					break;
			}
		}
		response += "  Text:[";
		for (i = 0; i < s.length(); i ++)
		{
			switch (s.charAt(i))
			{
				case 13 :
					response += "(cr)";
					break;
				case 10 :
					response += "(lf)";
					break;
				case 9 :
					response += "(tab)";
					break;
				default:
					response += s.charAt(i);
					break;
			}
		}
		response += "]";
		return response;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?