using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace smsxx
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private string msg=""; //簡訊內容
private string[] nums={"138********","137*******"};//要發送到的手機號
private int[] sendTimes; //記錄重試次數
private AxSMSocx.AxSMS axSMS1; //他們提供的那個sms.ocx控制項
private System.Timers.Timer timer1;
private TextWriter tw =null; //日誌寫入器
/// <summary>
/// 必需的設計器變數。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
... //都是預設產生的程式碼
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
... //都是預設產生的程式碼
}
#region Windows 表單設計器產生的程式碼
/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{... //都是預設產生的程式碼
}
#endregion
/// <summary>
/// 應用程式的主進入點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void axSMS1_OnComm(object sender, System.EventArgs e)
{
int ret=Int32.Parse(axSMS1.CommEvent.ToString());
switch(ret)
{
case 4444: addLog("[錯誤] 串口開啟");
break;
case 6666: addLog("[錯誤] 發送逾時");
break;
case 7777: addLog("[錯誤] 發送失敗");
break;
case 8880: addLog("[成功] "+axSMS1.LastSentSMS);
break;
case 8884: string lastsms=axSMS1.LastSentSMS;
addLog("[失敗] "+lastsms); //這裡發送的第一個手機的情況是不會返回的,估計是他們控制項的bug。
int i=lastsms.LastIndexOf("|")+1;
if(i>0)
{
i=Int32.Parse(lastsms.Substring(i));
if(sendTimes[ i]<4)
{
axSMS1.SendSMSWithoutRet(nums,msg,i);
sendTimes[ i]+=1;
}
}
break;
case 8888: addLog("[成功] "+axSMS1.NewSMS);
break;
case 2000: addLog("[失敗] 沒有入網");
axSMS1.Refresh();
break;
case 2003: addLog("[失敗] 拒絕登入");
axSMS1.Refresh();
break;
case 5555: addLog(axSMS1.InputInfo);
break;
case 4000: addLog(axSMS1.Read_SMS(4,1).ToString());
break;
}
}
private void addLog(string log)
{
tw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+" "+log);
}
private void Form1_Load(object sender, System.EventArgs e)
{
tw=new StreamWriter("smslogs/"+DateTime.Now.ToString("yyyy-MM-dd")+".log",true);
axSMS1.EndComm();
axSMS1.CommPort=1;
if(!axSMS1.IsOpen)
{
axSMS1.InitComm();
}
if(axSMS1.IsOpen)
{
axSMS1.SMSnotSaveInSIM();
}
else
{
addLog("[失敗]裝置第一次初始化");
}
timer1.Enabled=true;
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timer1.Enabled=false;
if(!axSMS1.IsOpen)
{
axSMS1.InitComm();
}
if(axSMS1.IsOpen)
{
StreamReader sr=new StreamReader("smsconf",System.Text.Encoding.GetEncoding("GB18030"));
msg=sr.ReadToEnd();
sr.Close();
sendTimes=new int[nums.Length];
for(int i=0;i<nums.Length;i++)
{
sendTimes=1;
}
for(int i=0;i<nums.Length;i++)
{
while(axSMS1.SendingBusyState())
{
Application.DoEvents();
}
if(axSMS1.SendSMSWithoutRet(nums,msg,i))
{
addLog("[成功] " +nums+"->"+msg);
}
else
{
addLog("[失敗] "+nums+"->"+msg);
}
}
for(int i=0;i<1000;i++)
{
Application.DoEvents();
System.Threading.Thread.Sleep(50);
}
axSMS1.EndComm();
}
else
{
addLog("[失敗] 第二次初始化");
}
tw.Close();
Close();
}
}
}