不同的短信猫开发差别比较大了~using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Runtime.InteropServices; using System.Text;public class Mysms { public Mysms() {
} [DllImport("E:\\Mysms.dll")] public extern static bool InitModem(int com, int beter); //初始化设备 [DllImport("E:\\Mysms.dll")] public extern static bool SendSms(int com, int beter, string context, string code, bool english, bool mianti, bool report); //发送信息 [DllImport("E:\\Mysms.dll")] public extern static bool ReadSms(int com, int beter, int smsId, StringBuilder context, StringBuilder code, StringBuilder gettime, bool del); //接收信息 [DllImport("E:\\Mysms.dll")] public extern static bool Dail(int com, int beter, string code, string wav); //振铃 [DllImport("E:\\Mysms.dll")] public extern static bool HangUp(int com, int beter); //挂机 }有了这个类以后,在任何页面都可以轻松的调用方法发送短信了!下面是我在前台调用的示例在vs2005中建一个test.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> 对方号码: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /> 内容:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="发送短信" /> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="呼叫对方" /> <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="接收短信" /> <br /> 对方号码:<asp:Label ID="Label1" runat="server" Text="对方号码"></asp:Label> <br /> 接收时间:<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br /> <asp:TextBox ID="TextBox1" runat="server" Height="127px" TextMode="MultiLine" Width="293px"></asp:TextBox></div> </form> </body> </html>test.aspx.cs内容为:using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Runtime.InteropServices; using System.Text; public partial class test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.InteropServices;
using System.Text;public class Mysms
{
public Mysms()
{
}
[DllImport("E:\\Mysms.dll")]
public extern static bool InitModem(int com, int beter); //初始化设备
[DllImport("E:\\Mysms.dll")]
public extern static bool SendSms(int com, int beter, string context, string code, bool english, bool mianti, bool report); //发送信息
[DllImport("E:\\Mysms.dll")]
public extern static bool ReadSms(int com, int beter, int smsId, StringBuilder context, StringBuilder code, StringBuilder gettime, bool del); //接收信息
[DllImport("E:\\Mysms.dll")]
public extern static bool Dail(int com, int beter, string code, string wav); //振铃
[DllImport("E:\\Mysms.dll")]
public extern static bool HangUp(int com, int beter); //挂机
}有了这个类以后,在任何页面都可以轻松的调用方法发送短信了!下面是我在前台调用的示例在vs2005中建一个test.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
对方号码:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
内容:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="发送短信" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="呼叫对方" />
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="接收短信" />
<br />
对方号码:<asp:Label ID="Label1" runat="server" Text="对方号码"></asp:Label>
<br />
接收时间:<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
<asp:TextBox ID="TextBox1" runat="server" Height="127px" TextMode="MultiLine" Width="293px"></asp:TextBox></div>
</form>
</body>
</html>test.aspx.cs内容为:using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.InteropServices;
using System.Text;
public partial class test : System.Web.UI.Page
{ protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string code = this.TextBox2.Text.Trim();
string text = this.TextBox1.Text.Trim();
if (Mysms.InitModem(1, 115200))
{ if (Mysms.SendSms(1, 115200, text, code, false, false, false))
{
Response.Write("<script language=javascript>alert('ok了');</script>");
}
else
{
Response.Write("<script language=javascript>alert('出错了');</script>");
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string code = this.TextBox2.Text.Trim();
if (Mysms.InitModem(1, 115200))
{ if (Mysms.Dail(1, 115200, code,"E:\\demo.wav"))
{
Mysms.HangUp(1, 115200);
Response.Write("<script language=javascript>alert('ok了');</script>");
}
}
}
protected void Button3_Click(object sender, EventArgs e)
{
StringBuilder context = new StringBuilder(500);
StringBuilder code = new StringBuilder(15);
StringBuilder gettime = new StringBuilder(20);
bool result;
//System.Text.StringBuilder context=" ",code=" ",gettime=" ";
//for (int i = 1; i <= 5; i++)
//{
result = Mysms.InitModem(1,115200);
if (result)
{
try
{
result = Mysms.ReadSms(1, 115200, 1, context, code, gettime, true); if (result)
{
this.Label1.Text = code.ToString();
this.Label2.Text = gettime.ToString();
this.TextBox1.Text = context.ToString();
// Response.Write("<script language=javascript>alert('查了');</script>");
}
else
{
Response.Write("<script language=javascript>alert('没了');</script>");
}
}
catch(Exception ex)
{
Response.Write("<script language=javascript>alert('"+ex.Message+"');</script>"); }
}
//}
}
}
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 ((c = 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)
{
c = "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");
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 = (CommPortIdentifier) portList.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 = (SerialPort) portID.open(owner, keeptime);
// 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()/2 & 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();
}}