短信猫记录读取程序

短信猫记录读取程序_第1张图片

  有两年多没有写程序了,感觉很陌生了,这是一个短信猫记录的读取程序,下载一个动态链接库(sms.dll)就行了,运行很稳定。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Data.SqlClient;
using System.Text;

namespace WinTQY
{
    public partial class MainForm : Form
    {
        public struct StructTQY {
            public string C01;
            public DateTime C02;
            public string C03;
            public decimal C04;
            public decimal C05;
            public decimal C06;
            public decimal C07;
            public decimal C08;
            public decimal C09;
            public decimal C10;
            public decimal C11;
            public decimal C12;
            public decimal C13;
            public string C14;
            public string C15;
            public string C16;
            public string C17;
            public string C18;
        }

        public MainForm()
        {
            InitializeComponent();
        }

        /// 
        /// 应用程序的主入口点。
        /// 
        ///
        [STAThread]

        [DllImport("sms.dll", EntryPoint = "Sms_Connection")]
        public static extern uint Sms_Connection(string CopyRight, uint Com_Port, uint Com_BaudRate, out string Mobile_Type, out string CopyRightToCOM);

        [DllImport("sms.dll", EntryPoint = "Sms_Disconnection")]
        public static extern uint Sms_Disconnection();

        [DllImport("sms.dll", EntryPoint = "Sms_Send")]
        public static extern uint Sms_Send(string Sms_TelNum, string Sms_Text);

        [DllImport("sms.dll", EntryPoint = "Sms_Receive")]
        public static extern uint Sms_Receive(string Sms_Type, out string Sms_Text);

        [DllImport("sms.dll", EntryPoint = "Sms_Delete")]
        public static extern uint Sms_Delete(string Sms_Index);

        [DllImport("sms.dll", EntryPoint = "Sms_AutoFlag")]
        public static extern uint Sms_AutoFlag();

        [DllImport("sms.dll", EntryPoint = "Sms_NewFlag")]
        public static extern uint Sms_NewFlag();

         private void button_DisConnect_DXM_Click(object sender, EventArgs e)
        {
            Sms_Disconnection();
            toolStripStatusLabel2.Text = "已经断开与短信猫的连接!";

            label2.Text = " ";
            label2.ForeColor = System.Drawing.Color.FromArgb(0, 0, 0);

            button_AUTOReceive.Enabled = false;
            button_Exit.Enabled = true;
            textBox_Port.Enabled = true;
            button_Connect_DXM.Enabled = true;

        }

        private void button_Connect_DXM_Click(object sender, EventArgs e)
        {
            button_Connect_DXM.Enabled = false;
            //this.Enabled = false;
            button_DisConnect_DXM.Enabled = false;
            button_AUTOReceive.Enabled = false;
            button_Exit.Enabled = false;
            textBox_Port.Enabled = false;
            //连接短信猫
            String TypeStr = "";
            String CopyRightToCOM = "";
            String CopyRightStr = "//上海迅赛信息技术有限公司,网址www.xunsai.com//";
            //连接短信猫,串口号:0为红外接口,1,2,3,...为串口
            if (Sms_Connection(CopyRightStr, uint.Parse(textBox_Port.Text), 9600, out TypeStr, out CopyRightToCOM) == 1) {
                //连接成功后开始处理短信             
                DealWithDX();
                toolStripStatusLabel2.Text = "完成短信的处理!";
                button_DisConnect_DXM.Enabled = true;
                button_AUTOReceive.Enabled = true;
                button_Exit.Enabled = true;
                label2.Text = "●";
                label2.ForeColor = System.Drawing.Color.FromArgb(0, 255, 0);
            }
            else
            {
                toolStripStatusLabel2.Text = "连接失败!";
            }
        }

        private void button_Exit_Click(object sender, EventArgs e)
        {
            //Sms_Disconnection();
            Application.Exit();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Sms_NewFlag() == 1)
            {
                toolStripStatusLabel2.Text = "收到新短信,请查收!";
                //读取新短信,0:未读短信,1:已读短信,2:待发短信,3:已发短信,4:全部短信
                DealWithDX();
            }
        }

        private void button_AUTOReceive_Click(object sender, EventArgs e)
        {
            if (Sms_AutoFlag() == 1)
            {
                toolStripStatusLabel2.Text = "未收到新短信";
                timer1.Enabled = true;
                button_AUTOReceive.Enabled = false;

            }
            else
            {
                toolStripStatusLabel2.Text = "该短信猫不支持自动接收短信功能";
            }
        }

        //处理短信的函数
        public void DealWithDX()
        {
            textBox1.Text = "";
            //得到计算机的名称
            string JSJName = System.Net.Dns.GetHostName();
            //得到计算机的IP地址
            string JSJIP = "";
            System.Net.IPAddress[] JSJIPaddr = System.Net.Dns.GetHostByName(JSJName).AddressList;
            for (int i = 0; i < JSJIPaddr.Length; i++) { JSJIP += JSJIPaddr[i].ToString() + "\n"; }
            //定义接收的字符串
            string ReceiveSmsStr = "";
            //初始化,读取全部短信记录,并与数据库中的记录进行对比,没有则添加到数据库中,完成后清空短信猫中的全部短信记录
            if (Sms_Receive("4", out ReceiveSmsStr) == 1 )
            {
                //解析每条短信
                if (ReceiveSmsStr != null)
                {
                    string[] SMSDatas = ReceiveSmsStr.Split(new char[1] { '|' });
                    //如果记录数大于1则进行比对操作
                    if (SMSDatas.Length >= 1)
                    {
                        foreach (var SMSData in SMSDatas)
                        {
                            //解析每一条短信
                            if (string.IsNullOrEmpty(SMSData) == false)
                            {
                                string[] SMSRecs = SMSData.Split(new char[1] { '#' });
                                string DX_XH = SMSRecs[0].ToString();//短信序号
                                string DX_SJH = SMSRecs[2].ToString();//手机号码
                                string DX_NR = SMSRecs[3].ToString();//短信内容
                                string DX_SJ = SMSRecs[4].ToString();//记录时间
                                textBox1.Text = textBox1.Text + "\r\n短信序号:" + DX_XH + "\r\n手机号码:" + DX_SJH + "\r\n短信内容:" + DX_NR + "\r\n接收时间:" + DX_SJ + "\n\r";
                                //通过数据库比对查看是否已经存储,不存在则需要写入数据库
                                using (SqlConnection Conn = new SqlConnection(SLinkToDB))
                                {
                                    Conn.Open();
                                    SqlCommand SqlCMD = new SqlCommand();
                                    SqlCMD.Connection = Conn;
                                    SqlCMD.CommandText = "select count(*) from YQCYTQY where c01=CONVERT(varchar(100),'" + DX_SJ + "',13)";
                                    int Rows = (int)SqlCMD.ExecuteScalar();
                                    if (Rows != 1)
                                    {
                                        //需要将该记录插入到数据库的YQCYTQY表中
                                        string SInsert = "insert into YQCYTQY values(@c01,@c02,@c03,@c04,@c05,@c06,getdate())";
                                        SqlCommand SQLInsert = new SqlCommand(SInsert, Conn);
                                        SqlParameter C01 = new SqlParameter("@c01", SqlDbType.DateTime);
                                        C01.Value = DateTime.Parse(DX_SJ);
                                        SQLInsert.Parameters.Add(C01);
                                        SqlParameter C02 = new SqlParameter("@c02", SqlDbType.VarChar);
                                        C02.Value = DX_SJH;
                                        SQLInsert.Parameters.Add(C02);
                                        SqlParameter C03 = new SqlParameter("@c03", SqlDbType.VarChar);
                                        C03.Value = DX_NR;
                                        SQLInsert.Parameters.Add(C03);
                                        SqlParameter C04 = new SqlParameter("@c04", SqlDbType.VarChar);
                                        C04.Value = JSJIP;
                                        SQLInsert.Parameters.Add(C04);
                                        SqlParameter C05 = new SqlParameter("@c05", SqlDbType.VarChar);
                                        C05.Value = JSJName;
                                        SQLInsert.Parameters.Add(C05);
                                        SqlParameter C06 = new SqlParameter("@c06", SqlDbType.VarChar);
                                        C06.Value = "";
                                        SQLInsert.Parameters.Add(C06);
                                        SQLInsert.ExecuteNonQuery();

                                        if (DX_NR.IndexOf("BOOM") >= 1 || DX_NR.IndexOf("OK") >= 1)
                                        {
                                            //将短信内容分解后插入到数据库的TQYINFO表中
                                            textBox1.Text = textBox1.Text + "\r\n准备存入数据库......";

                                            StructTQY SaveTQY = new StructTQY();
                                            SaveTQY.C03 = "";
                                            SaveTQY.C04 = 0.0m;
                                            SaveTQY.C05 = 0.0m;
                                            SaveTQY.C06 = 0.0m;
                                            SaveTQY.C07 = 0.0m;
                                            SaveTQY.C08 = 0.0m;
                                            SaveTQY.C09 = 0.0m;
                                            SaveTQY.C10 = 0.0m;
                                            SaveTQY.C11 = 0.0m;
                                            SaveTQY.C12 = 0.0m;
                                            SaveTQY.C13 = 0.0m;
                                            SaveTQY = GetTQTInfo(DX_NR);
                                            SaveTQY.C01 = DX_SJH;
                                            SaveTQY.C02 = Convert.ToDateTime(DX_SJ);

                                            textBox1.Text = textBox1.Text + "\r\n分解字符串......" ;
                                            string STQYINFO = "insert into TQYINFO values(@C01,@C02,@C03,@C04,@C05,@C06,@C07,@C08,@C09,@C10,@C11,@C12,@C13,@C14,@C15,@C16,@C17,@C18)";
                                            SqlCommand SQLTQYINFO = new SqlCommand(STQYINFO, Conn);

                                            SqlParameter TQYINFO_C01 = new SqlParameter("@C01", SqlDbType.VarChar);
                                            TQYINFO_C01.Value = SaveTQY.C01;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C01);

                                            SqlParameter TQYINFO_C02 = new SqlParameter("@C02", SqlDbType.DateTime);
                                            TQYINFO_C02.Value = SaveTQY.C02;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C02);

                                            SqlParameter TQYINFO_C03 = new SqlParameter("@C03", SqlDbType.VarChar);
                                            TQYINFO_C03.Value = SaveTQY.C03;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C03);

                                            SqlParameter TQYINFO_C04 = new SqlParameter("@C04", SqlDbType.Decimal);
                                            TQYINFO_C04.Value = SaveTQY.C04;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C04);

                                            SqlParameter TQYINFO_C05 = new SqlParameter("@C05", SqlDbType.Decimal);
                                            TQYINFO_C05.Value = SaveTQY.C05;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C05);

                                            SqlParameter TQYINFO_C06 = new SqlParameter("@C06", SqlDbType.Decimal);
                                            TQYINFO_C06.Value = SaveTQY.C06;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C06);

                                            SqlParameter TQYINFO_C07 = new SqlParameter("@C07", SqlDbType.Decimal);
                                            TQYINFO_C07.Value = SaveTQY.C07;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C07);

                                            SqlParameter TQYINFO_C08 = new SqlParameter("@C08", SqlDbType.Decimal);
                                            TQYINFO_C08.Value = SaveTQY.C08;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C08);

                                            SqlParameter TQYINFO_C09 = new SqlParameter("@C09", SqlDbType.Decimal);
                                            TQYINFO_C09.Value = SaveTQY.C09;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C09);

                                            SqlParameter TQYINFO_C10 = new SqlParameter("@C10", SqlDbType.Decimal);
                                            TQYINFO_C10.Value = SaveTQY.C10;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C10);

                                            SqlParameter TQYINFO_C11 = new SqlParameter("@C11", SqlDbType.Decimal);
                                            TQYINFO_C11.Value = SaveTQY.C11;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C11);

                                            SqlParameter TQYINFO_C12 = new SqlParameter("@C12", SqlDbType.Decimal);
                                            TQYINFO_C12.Value = SaveTQY.C12;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C12);

                                            SqlParameter TQYINFO_C13 = new SqlParameter("@C13", SqlDbType.Decimal);
                                            TQYINFO_C13.Value = SaveTQY.C13;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C13);

                                            SqlParameter TQYINFO_C14 = new SqlParameter("@C14", SqlDbType.VarChar);
                                            TQYINFO_C14.Value = SaveTQY.C14;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C14);

                                            SqlParameter TQYINFO_C15 = new SqlParameter("@C15", SqlDbType.VarChar);
                                            TQYINFO_C15.Value = SaveTQY.C15;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C15);

                                            SqlParameter TQYINFO_C16 = new SqlParameter("@C16", SqlDbType.VarChar);
                                            TQYINFO_C16.Value = SaveTQY.C16;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C16);

                                            SqlParameter TQYINFO_C17 = new SqlParameter("@C17", SqlDbType.VarChar);
                                            TQYINFO_C17.Value = SaveTQY.C17;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C17);

                                            SqlParameter TQYINFO_C18 = new SqlParameter("@C18", SqlDbType.VarChar);
                                            TQYINFO_C18.Value = SaveTQY.C18;
                                            SQLTQYINFO.Parameters.Add(TQYINFO_C18);

                                            SQLTQYINFO.ExecuteNonQuery();
                                            textBox1.Text = textBox1.Text + "\r\n完成!";

                                        }

                                       
                                    }
                                    //删除短信
                                    Sms_Delete(DX_XH);
                                }
                            }
                        }
                    }
                }
            }
        }

        public string GetMidString(String SSrc, string S1, string S2) {
            string SReturn;
            int I1, I2;

           
            I1 = SSrc.IndexOf(S1);
            I2 = SSrc.IndexOf(S2);

            if (S2 == "")
            {
                SReturn = SSrc.Substring(I1 + S1.Length, SSrc.Length - (I1 + S1.Length)).Trim();
            }
            else {
                SReturn = SSrc.Substring(I1 + S1.Length, Math.Abs(I2 - (I1 + S1.Length))).Trim();
            }

            return SReturn;
       
        }

        public StructTQY GetTQTInfo(string SInfo) {
            StructTQY MineTQY=new StructTQY();
            MineTQY.C03 = "";
            MineTQY.C04 = 0.0m;
            MineTQY.C05 = 0.0m;
            MineTQY.C06 = 0.0m;
            MineTQY.C07 = 0.0m;
            MineTQY.C08 = 0.0m;
            MineTQY.C09 = 0.0m;
            MineTQY.C10 = 0.0m;
            MineTQY.C11 = 0.0m;
            MineTQY.C12 = 0.0m;
            MineTQY.C13 = 0.0m;

            if (SInfo.IndexOf("(!BOOM!)")>=1) {
                //通球信息
                MineTQY.C03 =GetMidString(SInfo, "(!BOOM!)", "KM"); //通球距离

                //MessageBox.Show("距离:" + MineTQY.C03.ToString());
                MineTQY.C04=0;//蓄电池电压
                MineTQY.C05=0;//A通道磁场强度
                MineTQY.C06=0;//B通道磁场强度
                MineTQY.C07 = Convert.ToDecimal(GetMidString(SInfo, "AN-VPP:", "AS-VPP:"));//A通道N极磁场强度

                MineTQY.C08 = Convert.ToDecimal(GetMidString(SInfo, "AS-VPP:", "BN-VPP:"));//A通道S极磁场强度

                MineTQY.C09 = Convert.ToDecimal(GetMidString(SInfo, "BN-VPP:", "BS-VPP:"));//B通道N极磁场强度

                MineTQY.C10 = Convert.ToDecimal(GetMidString(SInfo, "BS-VPP:", "TQ-SD:"));//B通道S极磁场强度

                MineTQY.C11 = Convert.ToDecimal(GetMidString(SInfo, "TQ-SD:", "AB-F:"));//通球速度

                MineTQY.C12 = Convert.ToDecimal(GetMidString(SInfo, "AB-F:", "AB-M:"));//通球相位

                MineTQY.C13 = Convert.ToDecimal(GetMidString(SInfo, "AB-M:", ""));//磁场幅差
                MineTQY.C13 = Convert.ToDecimal(MineTQY.C13 /10000);
                MineTQY.C13 = Math.Round(MineTQY.C13, 2);

                MineTQY.C07 = Convert.ToDecimal((MineTQY.C07 - 2000)/20);
                MineTQY.C08 = Convert.ToDecimal((MineTQY.C08 - 2000)/20);
                MineTQY.C09 = Convert.ToDecimal((MineTQY.C09 - 2000)/20);
                MineTQY.C10 = Convert.ToDecimal((MineTQY.C10 - 2000)/20);
                MineTQY.C11 =Convert.ToDecimal( 82609/(MineTQY.C11*1000));
                MineTQY.C12 = Convert.ToDecimal(MineTQY.C11 * 46 / 10000);

                MineTQY.C07 = Math.Round(MineTQY.C07,2);
                MineTQY.C08 = Math.Round(MineTQY.C08, 2);
                MineTQY.C09 = Math.Round(MineTQY.C09, 2);
                MineTQY.C10 = Math.Round(MineTQY.C10, 2);
                MineTQY.C11 = Math.Round(MineTQY.C11, 2);
                MineTQY.C12 = Math.Round(MineTQY.C12, 2);

                MineTQY.C14 = "通球";
                MineTQY.C15 = "管线标记"; 
            }

            if (SInfo.IndexOf("OK")>=1) {
                //报平安信息
                MineTQY.C03 = GetMidString(SInfo, "OK!)", "KM"); //通球距离
                MineTQY.C04 = Convert.ToDecimal(GetMidString(SInfo, "BATT-V:", "(GS-A):"));//蓄电池电压
                MineTQY.C05 = Convert.ToDecimal(GetMidString(SInfo, "(GS-A):", "(GS-B):"));//A通道磁场强度
                MineTQY.C06 = Convert.ToDecimal(GetMidString(SInfo, "(GS-B):", ""));//B通道磁场强度
                //MineTQY.C05 = Convert.ToDecimal((MineTQY.C05 - 2000) / 20);
                //MineTQY.C06 = Convert.ToDecimal((MineTQY.C06 - 2000) / 20);

                MineTQY.C04 = Math.Round(MineTQY.C04, 2);
                MineTQY.C05 = Math.Round(MineTQY.C05, 2);
                MineTQY.C06 = Math.Round(MineTQY.C06, 2);

                MineTQY.C14 = "报平安";
                MineTQY.C15 = "管线标记"; 
            }

            MineTQY.C16 = "DESTDot";
            MineTQY.C17 = "";
            MineTQY.C18 = ""; 
       
            return MineTQY;
        }


    }
}

 

你可能感兴趣的:(C#,C#,短信猫)