62,248
社区成员




Sms_Connection(Com_Port As Integer,Com_BaudRate As Integer, Mobile_Type As String) As Integer
Sms_Connection函数说明如下:
功能描述:用于初始化终端与串口的连接
Com_Port:串口号(0为红外接口,1,2,3,...为串口)
Com_BaudRate:波特率
Mobile_Type:返回终端型号
Sms_Connection:返回值(0:连接终端失败;1:连接终端成功)
Sms_Send(Sms_TelNum As String, Sms_Text As String) As Integer
Sms_Send函数说明如下:
功能描述:发送短信
Sms_TelNum:发送给的终端号码
Sms_Text:发送的短信内容
Sms_Send:返回值(0:发送短信失败;1:发送短信成功)
Sms_Disconnection() As Integer
Sms_Disconnection函数说明如下:
功能描述:断开终端与串口的连接
using System;
using System.Runtime.InteropServices;
namespace WebSys
{
/// <summary>
/// Dll的转载和调用
/// </summary>
public class DllInvoke
{
[DllImport("kernel32.dll")]
private extern static IntPtr LoadLibrary(String path);
[DllImport("kernel32.dll")]
private extern static IntPtr GetProcAddress(IntPtr lib, String funcName);
[DllImport("kernel32.dll")]
private extern static bool FreeLibrary(IntPtr lib);
private static IntPtr hLib;
public DllInvoke()
{
}
/// <summary>
/// 加载Dll,并将要执行的函数转换为委托
/// </summary>
/// <param name="DLLPath">Dll的路径</param>
/// <param name="APIName">转换的函数</param>
/// <param name="t">委托类型</param>
/// <returns>将函数转换为委托</returns>
public Delegate Invoke(String DLLPath, String APIName, Type t)
{
//加载Dll
hLib = LoadLibrary(DLLPath);
//将要执行的函数转换为委托
IntPtr api = GetProcAddress(hLib, APIName);
return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);
}
/// <summary>
/// 卸载 Dll
/// </summary>
public void UnLoadDll()
{
bool ret = FreeLibrary(hLib);
hLib = IntPtr.Zero;
}
}
}
namespace WebSys
{
/// <summary>
/// Dll中函数的调用
/// </summary>
public delegate uint Sms_Connection(String CopyRight, uint Com_Port, uint Com_BaudRate, ref String Mobile_Type, ref String CopyRightToCOM);
public delegate uint Sms_Disconnection();
public delegate uint Sms_Send(string Sms_TelNum, string Sms_Text);
public class DecryptCommon
{
DllInvoke dll = new DllInvoke();
public DecryptCommon()
{
}
/// <summary>
/// 连接终端,返回连接状态:连接成功返回串口号,连接失败返回空
/// </summary>
/// <param name="strPort">串口号</param>
/// <returns>返回连接状态</returns>
public string Set_Sms_Connection()
{
//连接终端,串口号从1到9判断
string strRtn = "";
String strCopyRight = "//上海迅赛信息技术有限公司,网址www.xunsai.com//";
uint Com_BaudRate = 9600;
String strType = "";
String strCopyRightToCOM = "";
try
{
Sms_Connection Connection = (Sms_Connection)dll.Invoke(System.Web.HttpContext.Current.Server.MapPath(@"~/Bin/sms.dll"),"Sms_Connection", typeof(Sms_Connection));
for (uint Com_Port = 1; Com_Port < 10; Com_Port++)
{
if (Connection(strCopyRight, Com_Port, Com_BaudRate, ref strType, ref strCopyRightToCOM) == 1)
{
strRtn = Com_Port.ToString();
break;
}
}
}
catch(Exception ex)
{
strRtn = ex.Message;
}
finally
{
dll.UnLoadDll();
}
return strRtn;
}
public void Set_Sms_Disconnection()
{
//断开终端
try
{
Sms_Disconnection Disconnection = (Sms_Disconnection)dll.Invoke(System.Web.HttpContext.Current.Server.MapPath(@"~/Bin/sms.dll"),"Sms_Disconnection", typeof(Sms_Disconnection));
Disconnection();
}
catch
{
throw (new Exception());
}
finally
{
dll.UnLoadDll();
}
}
/// <summary>
/// 发送短信,返回发送状态
/// </summary>
/// <param name="Sms_TelNum">发送电话号码</param>
/// <param name="Sms_Text">发送内容</param>
/// <returns>返回状态:1为成功;0为失败.</returns>
public string Set_Sms_Send(string Sms_TelNum, string Sms_Text)
{
//发送短信
string strRtn = "0";
try
{
Sms_Send Send = (Sms_Send)dll.Invoke(System.Web.HttpContext.Current.Server.MapPath(@"~/Bin/sms.dll"),"Sms_Send", typeof(Sms_Send));
strRtn = Send(Sms_TelNum, Sms_Text).ToString();
}
catch
{
throw (new Exception());
}
finally
{
dll.UnLoadDll();
}
return strRtn;
}
}
}