Click here to Skip to main content
16,008,075 members
Home / Discussions / C#
   

C#

 
GeneralRe: Running Console application EXE on other machine Pin
siva4552-Feb-09 19:47
siva4552-Feb-09 19:47 
GeneralRe: Running Console application EXE on other machine Pin
Gideon Engelberth3-Feb-09 3:19
Gideon Engelberth3-Feb-09 3:19 
Questiontreeview Pin
samrat.net2-Feb-09 19:00
samrat.net2-Feb-09 19:00 
AnswerRe: treeview Pin
N a v a n e e t h2-Feb-09 19:02
N a v a n e e t h2-Feb-09 19:02 
AnswerRe: treeview Pin
Christian Graus2-Feb-09 19:17
protectorChristian Graus2-Feb-09 19:17 
QuestionChanging the size of datagridview Pin
Lijo Rajan2-Feb-09 18:49
Lijo Rajan2-Feb-09 18:49 
AnswerRe: Changing the size of datagridview Pin
SeMartens2-Feb-09 21:50
SeMartens2-Feb-09 21:50 
QuestionHow to create an array of objects? Pin
Aghosh Babu2-Feb-09 17:54
Aghosh Babu2-Feb-09 17:54 
i am developing an sms gateway application. I have a configuration text file which i have to read from the application and configure the connected modems according to it. After configuring the modems i have to save the configured modem as an object to an array/arraylist. So next time whenver i want the modem to do something i call the object from the array.


using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.ComponentModel;
using System.Data;
using System.Drawing;


namespace SMSTest
{
    class SMSModem 
    {


        public ArrayList PortNameContainer = new ArrayList();
        public ArrayList BaudRateContainer = new ArrayList();
        public ArrayList DataBitContainer = new ArrayList();
        public ArrayList PhoneNumberContainer = new ArrayList();
        public ArrayList SMSCetreNoContainer = new ArrayList();

        SerialPort serialPort1 = new SerialPort();
        private string config_file = "C:/config.txt";
       // public string PhoneNumber;
        private string ServiceCentreNo;
        public int NumberOfLines = 0;


        public bool ModemConfig()
        {
            try
            {
                string line;
                TextReader tr = new StreamReader(config_file);
                System.IO.StreamReader objReader;
                objReader = new System.IO.StreamReader(config_file);


                while ((line = tr.ReadLine()) != null)
                {
                    NumberOfLines++;
                }


                string[] ListLines = new string[NumberOfLines];

                for (int i = 0; i < NumberOfLines; i++)
                {
                    ListLines[i] = objReader.ReadLine();
                    string[] values = ListLines[i].Split(new char[] { '$', '#' });
                    string[] PortSplit = values[0].Split(new char[] { ':', '#' });
                    string PortName = PortSplit[1];

                    //********Reading Baud Rate*********
                    string[] BaudSplit = values[1].Split(new char[] { ':', '#' });
                    int BaudRate = Convert.ToInt32(BaudSplit[1]);

                    //******READING DATA BITS**********
                    string[] BitsSplit = values[2].Split(new char[] { ':', '#' });
                    int DataBits = Convert.ToInt32(BitsSplit[1]);

                    //********READING SIM PHONE NUMBER********
                    string[] NumberSplit = values[3].Split(new char[] { ':', '#' });
                    string PhoneNumber = NumberSplit[1];

                    //******READ SERVICE CENTRE NUMBER*********
                    string[] SerNumberSplit = values[4].Split(new char[] { ':', '#' });
                    ServiceCentreNo = SerNumberSplit[1];

                    //******Setting Modem Configuration********

//I want to save the below configuration as a modem object into an array! Currently i am saving the //individual settings to an array list and when ever i needs the modem to do something, i am //reconfiguring the modem again using the values that i get from the array list! So i cant do parrallel //sending with multiple modems!
                    serialPort1.PortName = PortName;
                    serialPort1.BaudRate = BaudRate;
                    serialPort1.DataBits = DataBits;
                    serialPort1.Handshake = Handshake.RequestToSend;
                    serialPort1.Parity = Parity.None;
                    serialPort1.StopBits = StopBits.One;
                    serialPort1.DtrEnable = true;

                    //**********AT CONFIG***********
                    serialPort1.Open();
                    serialPort1.Write("AT \r\n");
                    Thread.Sleep(5);
                    serialPort1.Write("AT+CMGF=1 \r\n");
                    Thread.Sleep(5);
                    serialPort1.Write("AT+CNMI=1,2,0,0,0 \r\n");
                    Thread.Sleep(5);
                    serialPort1.Write("AT+CMEE=0 \r\n");
                    Thread.Sleep(5);
                    serialPort1.Close();

  //****Adding value to array list
                    PortNameContainer.Add(Convert.ToString(PortName));
                    BaudRateContainer.Add(BaudRate);
                    DataBitContainer.Add(DataBits);
                    PhoneNumberContainer.Add(PhoneNumber);
                    SMSCetreNoContainer.Add(ServiceCentreNo);
                }//end while
            }//end try

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
            return true;
        }
 
        
        
    }
        
}



the below codes shows an exaple for sending out an sms. So whenever i need to send an sms i take the modem phone number, receiver Phone number and sms text using a method SendSMS(string MPN, string RPN, string text). Checks the modem phone number in the arraylist called "PhoneNumberContainer" and get the corresponding values from the other array list and configure the modem for sending. see th code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO.Ports;
using System.Windows.Forms;
using System.Threading;


namespace SMSTest
{
    
    class SMSManager
    {
        private SMSModem mdm = new SMSModem();
        //*****************SEND SMS********************************
        private SerialPort serialPort1 = new SerialPort();
        private int index;
        private string result;
        private void PortInitialise() //initialise port with the reveived modem number
        {
            serialPort1.PortName = Convert.ToString(mdm.PortNameContainer[index]);
            serialPort1.BaudRate = Convert.ToInt32(mdm.BaudRateContainer[index]);
            serialPort1.DataBits = Convert.ToInt32(mdm.DataBitContainer[index]);
            serialPort1.Handshake = Handshake.RequestToSend;
            serialPort1.Parity = Parity.None;
            serialPort1.StopBits = StopBits.One;
            serialPort1.DtrEnable = true;
        }
        
        public string SendSMS(string MPN, string RPN, string text)
        {
            try
            {
            
            mdm.ModemConfig();
            if (mdm.PhoneNumberContainer.Contains(MPN))
            {
                index = mdm.PhoneNumberContainer.IndexOf(MPN);
                //****CALL CONFIG: TO SEND SMS****
                PortInitialise();
                serialPort1.Open();
                string ServiceCentre = Convert.ToString(mdm.SMSCetreNoContainer[index]);
                //*************Write AT to the ports to send sms
                serialPort1.WriteLine("AT+CMGF=1;+CSCA=\"" + ServiceCentre + "\";+CMGS=\"+65" + RPN + "\"\r\n");
                System.Threading.Thread.Sleep(40);
                serialPort1.Write(text);
                char[] arr = new char[1];
                arr[0] = (char)26; //ascii value of Ctrl-Z
                serialPort1.Write(arr, 0, 1);
                System.Threading.Thread.Sleep(3000);
                string data = serialPort1.ReadExisting();
                result = "Message Sent";
                serialPort1.Close();
            }//end if
        }//end of try
        catch(Exception e)
        {
            result = "Message sending failed :" + e.Message;
        }
        return result;
        }
        //********************END OF SEND SMS****************************
                
    }


}


Below is a sample method that is within a button click event:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SMSTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
            

        private void button1_Click(object sender, EventArgs e)
        {
            SMSManager sm = new SMSManager();

            //************SEND SMS***********
            string msg = sm.SendSMS("+6581563840", "82083412", "test");
            MessageBox.Show(msg);
            //*******************************
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}



Now i want to do it in the other way to support parrallel sending. I.e i want to create an array of modem objects, these modem objects will be pre-configured using the values from text file. And every time i want to send out an sms i just call the modem object and send the message directly.

Pls help me... i am stuck and the project had to deliver soon!
AnswerRe: How to create an array of objects? Pin
N a v a n e e t h2-Feb-09 18:07
N a v a n e e t h2-Feb-09 18:07 
GeneralRe: How to create an array of objects? Pin
EliottA2-Feb-09 18:08
EliottA2-Feb-09 18:08 
GeneralRe: How to create an array of objects? Pin
N a v a n e e t h2-Feb-09 18:26
N a v a n e e t h2-Feb-09 18:26 
GeneralRe: How to create an array of objects? Pin
Aghosh Babu2-Feb-09 18:28
Aghosh Babu2-Feb-09 18:28 
GeneralRe: How to create an array of objects in General? Pin
Aghosh Babu2-Feb-09 18:30
Aghosh Babu2-Feb-09 18:30 
GeneralRe: How to create an array of objects in General? Pin
N a v a n e e t h2-Feb-09 18:33
N a v a n e e t h2-Feb-09 18:33 
GeneralRe: How to create an array of objects in General? Pin
Aghosh Babu2-Feb-09 21:33
Aghosh Babu2-Feb-09 21:33 
GeneralRe: How to create an array of objects in General? Pin
J4amieC2-Feb-09 22:36
J4amieC2-Feb-09 22:36 
GeneralRe: How to create an array of objects in General? Pin
Aghosh Babu2-Feb-09 22:57
Aghosh Babu2-Feb-09 22:57 
GeneralRe: How to create an array of objects in General? Pin
wasimsharp3-Feb-09 1:12
wasimsharp3-Feb-09 1:12 
GeneralRe: How to create an array of objects in General? Pin
EliottA3-Feb-09 2:19
EliottA3-Feb-09 2:19 
QuestionUniversal Remote Control for PDA in c# Pin
gauravxxx2-Feb-09 16:22
gauravxxx2-Feb-09 16:22 
AnswerRe: Universal Remote Control for PDA in c# Pin
EliottA2-Feb-09 17:34
EliottA2-Feb-09 17:34 
GeneralRe: Universal Remote Control for PDA in c# Pin
gauravxxx3-Feb-09 2:21
gauravxxx3-Feb-09 2:21 
GeneralRe: Universal Remote Control for PDA in c# Pin
EliottA3-Feb-09 2:26
EliottA3-Feb-09 2:26 
GeneralRe: Universal Remote Control for PDA in c# Pin
gauravxxx3-Feb-09 6:00
gauravxxx3-Feb-09 6:00 
GeneralRe: Universal Remote Control for PDA in c# Pin
EliottA3-Feb-09 6:03
EliottA3-Feb-09 6:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.