コード例 #1
0
        public Form1(param p_inst) //(int id,DateTime dt,String orgz,int index=0)
        {
            InitializeComponent();
            inst   = p_inst;
            custid = inst.cust_id;
            conn   = inst.conn; // db connection
            //selectedDate = inst.dt;
            org     = inst.org;
            indx    = inst.indx;
            tc      = inst.tc; // used to select next index
            QueryOK = inst.QueryOk;
            if (QueryOK)
            {
                btQuery.BackColor = Color.Green;
            }
            else
            {
                btQuery.BackColor = Color.Red;
            }


            String SqlGenId = String.Format(@"Select Gen_Id,Location From Generators Where Cust_Id='{0}'", custid);
            String dbconn   = ConfigurationManager.AppSettings["ConnectionString"];

            conn = new SqlConnection(dbconn);

            #region initz
            SqlDataReader drg = null;
            try
            {
                // conn.Open();
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }
                using (SqlCommand cmdg = new SqlCommand(SqlGenId, conn))
                {
                    //cmdg.CommandTimeout = 60;   // 60 ms time out
                    drg = cmdg.ExecuteReader(); //CommandBehavior.CloseConnection);
                    listGenerators.Clear();
                    listLocation.Clear();
                    while (drg.Read())
                    {
                        String genid = drg.GetString(0).Trim();
                        comboBox1.Items.Add(genid);
                        listGenerators.Add(genid);
                        listLocation.Add(drg.GetString(1));
                    }
                    drg.Close();
                    if (comboBox1.Items.Count > 1)
                    {
                        comboBox1.Items.Add("All");
                        listLocation.Add(org + " [All]");
                    }

                    comboBox1.SelectedIndex = indx;
                    inst.gen_id             = listGenerators[comboBox1.SelectedIndex].ToString(); //assign Gen_id
                    lblLoc.Text             = "Location: " + listLocation[indx];
                    lblOrg.Text             = "Organization: " + org;

                    comboBox1.Show();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error .. No Database", "Data Base", MessageBoxButtons.OK);
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
                return;
            }
            #endregion

            #region Modem Init
            Modem    = ConfigurationManager.AppSettings["GPRSRxMODEM"];
            BaudRate = Convert.ToInt16(ConfigurationManager.AppSettings["GPRSBaudRate"]);
            try
            {
                //ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2","SELECT * FROM Win32_SerialPort");
                //ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_POTSModem ");
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    //String mod = queryObj["Name"].ToString();
                    //MessageBox.Show("Modem: " + mod + "\n");
                    if (queryObj["Status"].ToString() == "OK" && queryObj["Name"].ToString().Contains(Modem))   // i.e "ST-Ericsson ... "
                    {
                        // string name = queryObj.GetPropertyValue("Name").ToString();
                        //MessageBox.Show(System.Convert.ToString(queryObj["Description"]));
                        com = queryObj["AttachedTo"].ToString();
                        //break;

                        //int index = com.LastIndexOf('M');
                        //if (index > 0 && index <= 5)                     // Check for valid COMx port
                        //    port = Convert.ToInt32(com.Substring(index + 1, com.Length - index - 1));
                    }
                }
                if (com == null)  // using external STM900 Modem
                {
                    String     SqlConfig  = "SELECT * FROM ConfigSystem";
                    SqlCommand cmd        = new SqlCommand(SqlConfig, conn);
                    String     ComRxModem = null;
                    bool       Configured = false;
                    // com = Convert.ToString(ConfigurationManager.AppSettings["RxMODEM"]);

                    if (conn.State != ConnectionState.Open)
                    {
                        conn.Open();
                    }
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        while (rd.Read())
                        {
                            ComRxModem = Convert.ToString(rd[0]);
                            // ComPollModem = Convert.ToString(rd[1]);
                            // PollTimer = Convert.ToInt32(rd[2]);
                            Configured = Convert.ToBoolean(rd[3]);
                            // DateTime  = Convert.ToString(rd[4]);
                        }
                        rd.Close();
                    }
                    if (Configured)
                    {
                        com = ComRxModem;   // assign com from db
                    }
                }
                if (com != null)
                {
                    //gprs = new GsmCommMain(port, BaudRate, 1000);              //
                    gprs = new UsbModem();

                    try
                    {
                        this.usbport = gprs.OpenPort(com, BaudRate, 8, 300, 300);
                        if (this.usbport.IsOpen)
                        {
                            // String ok =  gprs.ConfigModem();    // set modem defaults
                        }
                        else
                        {
                            MessageBox.Show("Error: Unable to init Modem:");
                        }
                    }
                    catch (Exception ex)
                    {
                        // ModemSts = false;

                        //DeviceHelper helper = new DeviceHelper();
                        if (DeviceHelper.TryResetPortByName(com))    // modem?
                        {
                            this.usbport = gprs.OpenPort(com, BaudRate, 8, 300, 300);
                        }
                        else
                        {
                            MessageBox.Show("Error: Unable to Init/Reset Modem:" + ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Comm port is Closed");
            }
            #endregion
            if (gprs != null)  //if (ModemSts)
            {
                btnGsmModule.Text      = "ON";
                btnGsmModule.BackColor = Color.Green;

                String network = gprs.GetCurrentOperator(this.usbport);
                if (network.Length > 0)
                {
                    btnNetworkStatus.Text      = network;
                    btnNetworkStatus.BackColor = Color.Green;
                }
            }
            if (gprs == null) //if (!ModemSts)
            {
                // modem not connected
                MessageBox.Show("Modem Failure", "Modem", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                try
                {
                    this.usbport.DataReceived += new SerialDataReceivedEventHandler(gprs_dataReceived); // intrrupt handling only
                    SmsMsgInstance             = new SmsMessageIndication(ProcessSmsData);              // Delegate to process & update SQL db
                    gprs.MsgRxdEnableInterrupt(this.usbport);                                           // Now enable Rx interrupt
                    // String ok = gprs.ConfigModem();    // set modem defaults

                    // gprs.PhoneConnected += new EventHandler(gprs_ModemConnected);
                    //gprs.MessageReceived += new MessageReceivedEventHandler(gprs_SmsMessageReceived);
                    // gprs.ReceiveComplete += new  ProgressEventHandler(gprs_ReceiveComplete);
                    // gprs.PhoneDisconnected += new EventHandler(gprs_PhoneDisconnected);
                    // gprs.ModemStatus = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("ERROR = Mdem Config: " + ex.Message);
                }
                //Check that the InvalidFilesDirectory exists
                if (!Directory.Exists(ConfigurationManager.AppSettings["DiscardedSMS"]))
                {
                    Directory.CreateDirectory(ConfigurationManager.AppSettings["DiscardedSMS"]);
                }
            }
            // String msg = "+CMT: "+923322683401","","15/11/07,16:22:46+20" D,15/11/07,16:22:43,num,1,2278,67,67,0,0,0,2192,0,0,1494,2147483647,235,480072,0,10000,498,100000,200"
        }  // form constr
コード例 #2
0
        public Form1(param p_inst) //(int id,DateTime dt,String orgz,int index=0)
        {
            InitializeComponent();
            inst   = p_inst;
            custid = inst.cust_id;
            conn   = inst.conn; // db connection
            //selectedDate = inst.dt;
            org  = inst.org;
            indx = inst.indx;
            tc   = inst.tc; // used to select next index
            btQuery.BackColor = Color.Red;
            String SqlGenId = String.Format(@"Select Gen_Id,Location From Generators Where Cust_Id='{0}'", custid);
            String dbconn   = ConfigurationManager.AppSettings["ConnectionString"];

            conn = new SqlConnection(dbconn);


            SqlDataReader drg = null;

            try
            {
                // conn.Open();
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }
                using (SqlCommand cmdg = new SqlCommand(SqlGenId, conn))
                {
                    //cmdg.CommandTimeout = 60;   // 60 ms time out
                    drg = cmdg.ExecuteReader(); //CommandBehavior.CloseConnection);
                    listGenerators.Clear();
                    listLocation.Clear();
                    while (drg.Read())
                    {
                        String genid = drg.GetString(0);
                        comboBox1.Items.Add(genid);
                        listGenerators.Add(genid);
                        listLocation.Add(drg.GetString(1));
                    }
                    drg.Close();
                    if (comboBox1.Items.Count > 1)
                    {
                        comboBox1.Items.Add("All");
                        listLocation.Add(org + " [All]");
                    }

                    comboBox1.SelectedIndex = indx;
                    inst.gen_id             = listGenerators[comboBox1.SelectedIndex].ToString(); //assign Gen_id
                    lblLoc.Text             = "Location: " + listLocation[indx];
                    lblOrg.Text             = "Organization: " + org;

                    comboBox1.Show();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error .. No Database", "Data Base", MessageBoxButtons.OK);
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
                return;
            }



            // Modem = ConfigurationManager.AppSettings["GPRSMODEM"];
            BaudRate = Convert.ToInt16(ConfigurationManager.AppSettings["GPRSBaudRate"]);
            try
            {
                /*  ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_SerialPort");
                 * foreach (ManagementObject queryObj in searcher.Get())
                 * {
                 *    String mod = queryObj["Name"].ToString();
                 *    if (queryObj["Name"].ToString().Contains(Modem))   // i.e "ST-Ericsson ... "
                 *    {
                 *        com = queryObj["DeviceID"].ToString();
                 *        //int index = com.LastIndexOf('M');
                 *        //if (index > 0 && index <= 5)                     // Check for valid COMx port
                 *        //    port = Convert.ToInt32(com.Substring(index + 1, com.Length - index - 1));
                 *    }
                 * }  */
                // com = "COM3";
                com = Convert.ToString(ConfigurationManager.AppSettings["RxMODEM"]);
                if (com != null)
                {
                    //gprs = new GsmCommMain(port, BaudRate, 1000);              //
                    gprs = new UsbModem();


                    try
                    {
                        this.usbport = gprs.OpenPort(com, BaudRate, 8, 300, 300);
                        if (this.usbport.IsOpen)
                        {
                            // String ok =  gprs.ConfigModem();    // set modem defaults
                        }
                        else
                        {
                            MessageBox.Show("Error: Unable to init Modem:");
                        }
                    }
                    catch (Exception ex)
                    {
                        // ModemSts = false;
                        MessageBox.Show("Error: Unable to init Modem:" + ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Comm port is Closed");
            }

            if (gprs != null)  //if (ModemSts)
            {
                btnGsmModule.Text      = "ON";
                btnGsmModule.BackColor = Color.Green;

                String network = gprs.GetCurrentOperator(this.usbport);
                if (network.Length > 0)
                {
                    btnNetworkStatus.Text      = network;
                    btnNetworkStatus.BackColor = Color.Green;
                }
            }
            if (gprs == null) //if (!ModemSts)
            {
                // modem not connected
                MessageBox.Show("Modem Failure", "Modem", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                try
                {
                    this.usbport.DataReceived += new SerialDataReceivedEventHandler(gprs_dataReceived); // intrrupt handling only
                    SmsMsgInstance             = new SmsMessageIndication(ProcessSmsData);              // Delegate to process & update SQL db
                    gprs.MsgRxdEnableInterrupt(this.usbport);                                           // Now enable Rx interrupt
                    // String ok = gprs.ConfigModem();    // set modem defaults

                    // gprs.PhoneConnected += new EventHandler(gprs_ModemConnected);
                    //gprs.MessageReceived += new MessageReceivedEventHandler(gprs_SmsMessageReceived);
                    // gprs.ReceiveComplete += new  ProgressEventHandler(gprs_ReceiveComplete);
                    // gprs.PhoneDisconnected += new EventHandler(gprs_PhoneDisconnected);
                    // gprs.ModemStatus = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("ERROR = Mdem Config: " + ex.Message);
                }
                //Check that the InvalidFilesDirectory exists
                if (!Directory.Exists(ConfigurationManager.AppSettings["DiscardedSMS"]))
                {
                    Directory.CreateDirectory(ConfigurationManager.AppSettings["DiscardedSMS"]);
                }
            }
        }  // form constr