金笛工业手机论坛  

返回   金笛工业手机论坛 > 开发者乐园 > 短信开发资料

短信开发资料 短信开发技巧,代码分析

回复
 
LinkBack 主题工具 显示模式
旧 2009-11-26   #1 (permalink)
超级版主
 
s0z0y 的头像
 
注册日期: 2009-06-30
帖子: 173
s0z0y 正向着好的方向发展
发送 ICQ 消息给 s0z0y
默认 linux下用java通过串口连接手机或短信猫发送中文短信的方法

PHP 代码:
下载jar包,解压,把其中so文件复制到/usr/lib

把javax
.comm.properties复制到jre目录下

把comm
.jar放到classpath目录下

串口连接:

CommPortIdentifier portID
;
String owner = new String("modem");
int keeptime 5000;
Enumeration portList;
portList CommPortIdentifier.getPortIdentifiers();

// String driverName = "com.sun.comm.Win32Driver";
String driverName "com.sun.comm.LinuxDriver";
CommDriver driver null;

try {
// System.loadLibrary("win32com");

driver = (CommDriver) Class.forName(driverName).newInstance();
driver.initialize();
System.out.println("Win32Driver Initialized");
} catch (
Exception e) {
e.printStackTrace();
}
// 如果有多个端口
while (portList.hasMoreElements()) {
portID = (CommPortIdentifierportList.nextElement();
System.out.println("COM:" portID.getName());
// if (portID.getName().equals("COM1"))
if (portID.getName().equals("/dev/ttyS0"))
try {
sPort = (SerialPortportID.open(ownerkeeptime);
break;
}catch (
PortInUseException e) {
e.printStackTrace();
System.exit(1);
}
}

try{

if (
sPort != null)
{
in sPort.getInputStream();
out sPort.getOutputStream();

try {

sPort.addEventListener(this);
sPort.notifyOnDataAvailable(true);
} catch (
TooManyListenersException e) {
e.printStackTrace();
}
}

}catch(
Exception e)
{
e.printStackTrace();
}

其中注释掉的是windows下的连接方法

全部代码
import javax
.comm.*;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import java.io.*;


public class 
ATSms implements SerialPortEventListener {
private 
SerialPort sPort null;

private 
InputStream in null;
private 
OutputStream out null;

private 
byte b[] = new byte[1024];

private 
String cmd "";
private 
int len 0;
private 
String recvMsg "";


public 
ATSms()
{
init();
}

public 
synchronized void serialEvent(SerialPortEvent serialPortEvent) {

int c 0;
// 如果有串口事件发生
if (serialPortEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE)
{
try {
System.out.print("Recv :" );
while ((
in.read()) != -1) {
System.out.print((char)c);
recvMsg += ((char)c);
}
}
// try
catch (IOException e) {
e.printStackTrace();
}
}
}

private static 
String getUnicodeString(String s)
{
String unicodeStr "";
for(
int i=0;i<S.LENGTH();I++)
{
String c Integer.toHexString(s.charAt(i) & 0xffff);
while(
c.length()<4)
{
"0" c;
}
unicodeStr += c;
}
return 
unicodeStr;
}
private static 
String getSMobile(String mobile)
{
StringBuffer sMobile = new StringBuffer("68");
for( 
int i=0;i<MOBILE.LENGTH();I=I+2)
{
sMobile.append((i+1<MOBILE.LENGTH())?MOBILE.CHARAT(I+1):"F");
</
MOBILE.LENGTH())?MOBILE.CHARAT(I+1):"F");
sMobile.append(mobile.charAt(i));
}
return 
sMobile.toString();
}

private 
void init()
{
CommPortIdentifier portID;
String owner = new String("modem");
int keeptime 5000;
Enumeration portList;
portList CommPortIdentifier.getPortIdentifiers();

// String driverName = "com.sun.comm.Win32Driver";
String driverName "com.sun.comm.LinuxDriver";
CommDriver driver null;

try {
// System.loadLibrary("win32com");
// System.out.println("Win32Com Library Loaded");

driver = (CommDriver) Class.forName(driverName).newInstance();
driver.initialize();
System.out.println("Win32Driver Initialized");
} catch (
Exception e) {
e.printStackTrace();
}
// 如果有多个端口
while (portList.hasMoreElements()) {
portID = (CommPortIdentifierportList.nextElement();
System.out.println("COM:" portID.getName());
// if (portID.getName().equals("COM1"))
if (portID.getName().equals("/dev/ttyS0"))
try {
// System.out.println("try to open the /dev/ttyS0");
sPort = (SerialPortportID.open(ownerkeeptime);
// System.out.println("try to open the /dev/ttyS0 OK");
break;
}catch (
PortInUseException e) {
e.printStackTrace();
System.exit(1);
}
}
try{

if (
sPort != null)
{
in sPort.getInputStream();
out sPort.getOutputStream();

try {

sPort.addEventListener(this);
sPort.notifyOnDataAvailable(true);
} catch (
TooManyListenersException e) {
e.printStackTrace();
}
}


/**
System.out.print("set PDU mode :");
out.write("at+cmgf=0\r".getBytes());
out.flush();
// len = in.read(b);
for(int i=0;i<3;i++)
{
recvMsg = readThread.readComMsg();
if(!"".equals(recvMsg))break;
Thread.sleep(1000);
System.out.println("Sleep 1001.");
}

if(recvMsg.indexOf("OK")>=0)
{
System.out.println("OK");
}else
{
System.out.println("ERROR");
}
*/

}catch(Exception e)
{
e.printStackTrace();
}
}
public 
void close()
{
try {
Thread.sleep(4000);

in.close();
out.close();
} catch (
Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
sPort.close();
}

public 
int sendSms(String recv,String content)
{
int sendok 1;
for(
int i=0;i<CONTENT.LENGTH();I=I+70)
{
sendok sendShortSms(recv, ((i+70<=content.length())?content.substring(i,i+70):content.substring(i)) );
}
return 
sendok;
}
private 
int sendShortSms(String recv,String content)
{
int sendok = -1;
try{
System.out.println("send sms to:" recv "\ncontent:" content);
content getUnicodeString(content);

// content = (j+70>uc.length()/2)?uc.substring(j):uc.substring(j,j+140);
// System.out.println(j + ":" + content);
cmd "at+cmgs=" Integer.toString(15+content.length()/2) + "\r";
out.write(cmd.getBytes());
out.flush();
System.out.println("Send:" cmd);

for(
int i=0;i<5;i++)
{
if(
recvMsg.indexOf(">")>=0)
{
recvMsg recvMsg.substring(recvMsg.indexOf(">")+1);
break;
}else
{
try
{
Thread.sleep(1000);
}catch(
Exception e)
{
}
}
}

cmd Integer.toHexString(content.length()/0xffff);
if(
cmd.length()<2)cmd="0"+cmd;
cmd "0011000D91" getSMobile(recv) + "000800" cmd content;
cmd cmd.toUpperCase();
out.write(cmd.getBytes());
out.write(26);
out.flush();
System.out.println("Send:" cmd);

for(
int i=0;i<5;i++)
{
if(
recvMsg.indexOf("OK")>=0)
{
recvMsg recvMsg.substring(recvMsg.indexOf("OK")+2);
sendok 1;
break;
}else
{
try
{
Thread.sleep(1000);
}catch(
Exception e)
{
}
}
}

}catch(
Exception e)
{
sendok = -1;
e.printStackTrace();
}
return 
sendok;
}

public static 
void main(String args[])
{
String c = ("16日" );
ATSms at = new ATSms();
at.sendSms("13999999999",c);
at.close();
}


__________________
通讯利器,金笛锻造
www.sendsms.cn

让 世 界 倾 听 我 们 的 笛 声
s0z0y 当前离线   回复时引用此帖
旧 2010-01-30   #2 (permalink)
初级会员
 
注册日期: 2010-01-30
帖子: 3
zhangyou1010 正向着好的方向发展
默认

好资料,有没有发送彩信的啊?
zhangyou1010 当前离线   回复时引用此帖
旧 2010-02-05   #3 (permalink)
高级会员
 
注册日期: 2009-07-14
帖子: 102
水哭了 正向着好的方向发展
默认

彩信方面我们有数据库的接口。如有疑问。请联系 QQ:1152066310
水哭了 当前离线   回复时引用此帖
回复

书签


当前查看此主题的会员: 2 (0 位会员和 2 位游客)
 

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用



所有时间均为北京时间。现在的时间是 01:41


Powered by vBulletin® 版本 3.8.3
版权所有 ©2000 - 2024,Jelsoft Enterprises Ltd.