Serial communication Using c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nmsreddi
    Contributor
    • Jul 2006
    • 366

    Serial communication Using c#

    Hi friends

    I am working on c#.net . i am developing a windows application using c# 2003

    aim of my application is sending and receiving sms using GSM modem.

    for this i am using AxMscomm control which is there in VB6.0(telephone device)

    by using this i am able to receive the data and able to send message .but the

    problem is when i send the sms and after sending if i want to receive the data again

    it throwing an exception Exception from HRESULT:0x800A1 F45

    I am using two different windows for receiving (reading messages from sim) and

    other for sending sms.

    i am unable to access the AxMscomm open and close (ie portopen and close)

    property from other windows forms .when i tried to close the port from other

    windows form(form other than the AxMscomm control is placed) it shows the

    portopen property as false but internally the connection is not closed.

    how to handle the properties of this control from overall my application

    My partil code goes like this....
    *************** *************** ************cod e************** **********
    SerialPort is the name of my AxMscomm control

    SerialPort.Comm Port=1;
    if(SerialPort.P ortOpen)
    SerialPort.Port Open=false;

    SerialPort.RThr eshold=1;
    SerialPort.Sett ings="9600,n,8, 1";
    SerialPort.DTRE nable=true;
    SerialPort.RTSE nable=true;
    SerialPort.Hand shaking=MSCommL ib.HandshakeCon stants.comNone;
    SerialPort.Inpu tMode=MSCommLib .InputModeConst ants.comInputMo deText;
    SerialPort.Inpu tLen=0;
    SerialPort.Null Discard=false;


    opening the connection to rs232.......... ...

    SerialPort.Port Open=true;

    timer1.Start();

    ////i am using timer to read messages ffrom my sim contineously

    and stop reading when i tried to send the sms. and autmatically start reading

    with out manual interaction.

    reading messages from sim in timer tick

    SerialPort.OnCo mm +=new EventHandler(Se rialPort_OnComm );

    try
    {
    timer1.Stop();


    if(SerialPort.I nBufferCount>0)
    {
    Thread.Sleep(10 00);

    ProcessSerialpo rtdata((string) SerialPort.Inpu t);

    timer1.Start();// commented
    }
    else
    timer1.Start(); //commented
    }
    catch(Exception ex)
    {
    if(timer1.Enabl ed==true)
    timer1.Stop();
    else
    timer1.Start();
    }


    private void ProcessSerialpo rtdata(string input)
    { richTextBox1.Te xt="";
    richTextBox1.Ap pendText(input+ "\n");
    r


    if(richTextBox1 .TextLength>60)
    {
    i=0;
    j++;

    }
    else if((s.IndexOf(" ERROR")!=-1))
    {
    j=1;
    // timer1.Start();
    SerialPort.InBu fferCount=0;


    }


    Sending sms............ .....


    timer1.Stop();
    string cmdsend="AT+CMG S="+"xxxxxxxxxx "+Convert.ToCha r(13);

    SerialPort.Outp ut=cmdsend;
    Thread .Sleep (100);
    string cmd=SerialPort. Input.ToString ();
    cmdsend="";

    if(cmd.IndexOf (">")>1)
    {
    //cmd="";
    cmdsend=" sms to b send"+Convert.T oChar (26);
    SerialPort.Outp ut =cmdsend;
    Thread.Sleep (10000);
    cmd=SerialPort. Input.ToString ();
    }



    Thread.Sleep(10 00);

    timer1.Start();


    this is the code i am using in my application

    i am using timer control to my control process of reading and sending

    separately using single rs232 port

    Hope you will get the best

    Thanks and regards

    Nmsreddi
Working...