SMSLib 备忘录

http://www.cnblogs.com/bluespot/archive/2008/08/02/1258735.html

    这是网上装载相当广泛的一篇关于SM Push的文章,里面提到了SMSLib的包,这是一个基于串口控制GPS Modem或者手机包,网上有相当多的介绍,摸索了一天,基本上学会使用了,备忘如下:

 

  • 需要的第三方包,comm.jar,slf4j(slf4j-api-1.5.10.jar,slf4j-jdk14-1.5.10.jar,log4j-over-slf4j-1.5.10.jar),第一个是基于win32平台的串口控制包,后面是日志的,似乎可以搭配不同的log包使用没有深入学习。commons-net-2.0.jar据说需要,可能是用smpp使用到吧,我用串口的话似乎可以不用。
  • 串口可以使com,usb甚至红外,蓝牙,我在5800I上用USB,实际上是com18,smslib里面有一段测试哪个串口连接了手机的类,可以试试
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Formatter;
import org.smslib.helper.CommPortIdentifier;
import org.smslib.helper.SerialPort;

public class CommTest
{
	private static final String _NO_DEVICE_FOUND = "  no device found";

	private final static Formatter _formatter = new Formatter(System.out);

	static CommPortIdentifier portId;

	static Enumeration<CommPortIdentifier> portList;

	static int bauds[] = { 9600, 14400, 19200, 28800, 33600, 38400, 56000, 57600, 115200 };

	/**
	 * Wrapper around {@link CommPortIdentifier#getPortIdentifiers()} to be
	 * avoid unchecked warnings.
	 */
	private static Enumeration<CommPortIdentifier> getCleanPortIdentifiers()
	{
		return CommPortIdentifier.getPortIdentifiers();
	}

	public static void main(String[] args)
	{
		System.out.println("\nSearching for devices...");
		portList = getCleanPortIdentifiers();
		while (portList.hasMoreElements())
		{
			portId = portList.nextElement();
			if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
			{
				_formatter.format("%nFound port: %-5s%n", portId.getName());
				for (int i = 0; i < bauds.length; i++)
				{
					SerialPort serialPort = null;
					_formatter.format("       Trying at %6d...", bauds[i]);
					try
					{
						InputStream inStream;
						OutputStream outStream;
						int c;
						String response;
						serialPort = portId.open("SMSLibCommTester", 1971);
						serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
						serialPort.setSerialPortParams(bauds[i], SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
						inStream = serialPort.getInputStream();
						outStream = serialPort.getOutputStream();
						serialPort.enableReceiveTimeout(1000);
						c = inStream.read();
						while (c != -1)
							c = inStream.read();
						outStream.write('A');
						outStream.write('T');
						outStream.write('\r');
						Thread.sleep(1000);
						response = "";
						c = inStream.read();
						while (c != -1)
						{
							response += (char) c;
							c = inStream.read();
						}
						if (response.indexOf("OK") >= 0)
						{
							try
							{
								System.out.print("  Getting Info...");
								outStream.write('A');
								outStream.write('T');
								outStream.write('+');
								outStream.write('C');
								outStream.write('G');
								outStream.write('M');
								outStream.write('M');
								outStream.write('\r');
								response = "";
								c = inStream.read();
								while (c != -1)
								{
									response += (char) c;
									c = inStream.read();
								}
								System.out.println(" Found: " + response.replaceAll("\\s+OK\\s+", "").replaceAll("\n", "").replaceAll("\r", ""));
							}
							catch (Exception e)
							{
								System.out.println(_NO_DEVICE_FOUND);
							}
						}
						else
						{
							System.out.println(_NO_DEVICE_FOUND);
						}
					}
					catch (Exception e)
					{
						System.out.print(_NO_DEVICE_FOUND);
						Throwable cause = e;
						while (cause.getCause() != null)
						{
							cause = cause.getCause();
						}
						System.out.println(" (" + cause.getMessage() + ")");
					}
					finally
					{
						if (serialPort != null)
						{
							serialPort.close();
						}
					}
				}
			}
		}
		System.out.println("\nTest complete.");
	}
}
  •  串口主要通过AT命令控制手机发送短信,关于AT命令和PDU的结构请参照下面的博客,作者搜集了相当多的资料

http://hi.baidu.com/dtzw/blog/category/Gsm

  • 关于如何向手机指定端口发短信,我搜索了很多资料,可惜没有找到明确的指导资料,有几点要确认的:一般的Short Message Moden(这个关键词的中文居然被和谐了,无聊)和手机短信无法触发注册到特定端口的Midlet,端口数据其实没有写在PDU的结构数据中,而是写在写入短信体的UD中,关于UDHI(User Data Head Indicator)的结构网上没有找到资料,全部都是直接写入短信数据,要了解UDHI还是要自己查SMSLib的代码,它确实可以向指定端口发送短信,不过网上相当多的代码是旧版本的。

 

你可能感兴趣的:(数据结构,C++,c,log4j,C#)