locked
SEnding SMS code in c++. RRS feed

  • Question

  • Hi,

       Im trying to develop a code which sends sms to gsm phones using c++.

       Actually i found the same code which is designed in .net

       but can anyone please help me doing the same in c++.


    This is the code in .net

    If anyone do have any solution, please post immediately. please



    please post any related topics which may be useful .

    thanking u



    option Explicit On

     

    Imports System

    Imports System.Threading

    Imports System.IO.Ports

    Imports System.Windows.Forms

     

    <ComClass(SMSCOMMS.ClassId, SMSCOMMS.InterfaceId, SMSCOMMS.EventsId)>_

    Public Class SMSCOMMS

     

        

    Public Const ClassId As String = " 30dd879c-ee2f-11db-8314-0800200c9a66"   

    Public Const InterfaceId As String = " df56bca8-207c-11dd-bd0b-0800200c9a66"   

    Public Const EventsId As String = " c46ffcd5-207c-11dd-bd0b-0800200c9a66"


        Private WithEvents SMSPort As SerialPort

        Private SMSThread As Thread

        Private ReadThread As Thread

        Shared _Continue As Boolean = False

        Shared _ContSMS As Boolean = False

        Private _Wait As Boolean = False

        Shared _ReadPort As Boolean = False

        Public Event Sending(ByVal Done As Boolean)

        Public Event DataReceived(ByVal Message As String)


        Public Sub SetPort(ByRef COMMPORT As String)

            SMSPort = New SerialPort

            With SMSPort

                .PortName = COMMPORT

                .BaudRate = 9600

                .Parity = Parity.None

                .DataBits = 8

                .StopBits = StopBits.One

                .Handshake = Handshake.RequestToSend

                .DtrEnable = True

                .RtsEnable = True

                .NewLine = vbCrLf

            End With

     

            ReadThread = New Thread(AddressOf ReadPort)

        End Sub

     

     

        Public Function SendSMS(ByVal CellNumber As String, ByVal SMSMessage As String) As Boolean

            Dim MyMessage As String = Nothing

            If SMSMessage.Length <= 160 Then

                MyMessage = SMSMessage

            Else

                MyMessage = Mid(SMSMessage, 1, 160)

            End If

            If IsOpen = True Then

                SMSPort.Write("AT+CMGF=1" & Chr(13))

                SMSPort.Write([String].Format("AT+CMGS=""{0}""" & Chr(13), CellNumber))

                _ContSMS = False

                While _ContSMS = False

                    Application.DoEvents()

                End While

                SMSPort.Write([String].Format("{0}" & Chr(26) & Chr(13), MyMessage))

                _Continue = False

                RaiseEvent Sending(False)

            End If

        End Function

     

        Private Sub ReadPort()

            Dim SerialIn As String = Nothing

            Dim RXBuffer(SMSPort.ReadBufferSize) As Byte

            Dim SMSMessage As String = Nothing

            Dim Strpos As Integer = 0

            Dim TmpStr As String = Nothing

            While SMSPort.IsOpen = True

                If (SMSPort.BytesToRead <> 0) And (SMSPort.IsOpen = True) Then

                    While SMSPort.BytesToRead <> 0

                        SMSPort.Read(RXBuffer, 0, SMSPort.ReadBufferSize)

                        SerialIn = SerialIn & System.Text.Encoding.ASCII.GetString(RXBuffer)

                        If SerialIn.Contains(">") = True Then

                            _ContSMS = True

                        End If

                        If SerialIn.Contains("+CMGS:") = True Then

                            _Continue = True

                            RaiseEvent Sending(True)

                            _Wait = False

                            SerialIn = String.Empty

                            ReDim RXBuffer(SMSPort.ReadBufferSize)

                        End If

     

                    End While

                    RaiseEvent DataReceived(SerialIn)

                    SerialIn = String.Empty

                    ReDim RXBuffer(SMSPort.ReadBufferSize)

                End If

            End While

        End Sub

     

        Public ReadOnly Property IsOpen() As Boolean

            Get

                If SMSPort.IsOpen = True Then

                    IsOpen = True

                Else

                    IsOpen = False

                End If

            End Get

        End Property


        Public Sub Open()

            If IsOpen = False Then

                SMSPort.Open()

                ReadThread.Start()

            End If

        End Sub

     

        Public Sub Close()

            If IsOpen = True Then

                SMSPort.Close()

            End If

        End Sub


     

    End Class

    Monday, May 12, 2008 11:49 PM

Answers

All replies

  • Hi,

     

    Based on my knowledge, GSM modem is generally connected to PC through serial port, and we send SMS through AT commands. Thus, we can send SMS through sending AT commands to the serial port which the GSM modem is connected. Therefore, the core part of the task is to communicate with serial port.

     

    Your above code uses System.IO.Ports.SerialPort to communicate with serial port. You can use System.IO.Ports.SerialPort in C++/CLI too. To convert VB.Net to C++/CLI, you needn’t to modify the keywords. For example, replace “import” with “using namespace”. You can refer to C++/CLI language specifications at

    http://download.microsoft.com/download/9/9/c/99c65bcd-ac66-482e-8dc1-0e14cd1670cd/C++-CLI%20Standard.pdf

    Wednesday, May 14, 2008 10:26 AM
  • Here is sample code, you can take as reference.

     

    Code Snippet

    #include "stdafx.h"

    using namespace System;

    using namespace System::Threading;

    using namespace System::ComponentModel;

    using namespace System::IO::Ports;  

     

    public ref class SMSCOMMS

    {

    private: SerialPort ^SMSPort;

                 Thread ^SMSThread;

                 Thread ^ReadThread;

    public: static bool _Continue = false;

               static bool _ContSMS = false;

    private :bool _Wait ;

                bool IsOpen;

    public :

               static bool _ReadPort = false;

    public:

                delegate void SendingEventHandler(bool Done);

                event SendingEventHandler^ Sending;

                delegate void DataReceivedEventHandler(String ^Message);

                event DataReceivedEventHandler^ DataReceived;   

     

    public: SMSCOMMS(String ^COMMPORT)

                            {

                                        IsOpen=false;

                                        SMSPort = gcnew SerialPort();

                                        SMSPort->PortName = COMMPORT;

                                        SMSPort->BaudRate = 9600;

                                        SMSPort->Parity = Parity::None;

                                        SMSPort->DataBits = 8;

                                        SMSPort->StopBits = StopBits::One;

                                        SMSPort->Handshake = Handshake::RequestToSend;

                                        SMSPort->DtrEnable = true;

                                        SMSPort->RtsEnable = true;

                                        SMSPort->NewLine = System::Environment::NewLine;

                                        ReadThread = gcnew Thread(

                                                    gcnew System::Threading::ThreadStart(this,&SMSCOMMS::ReadPort));

                            }   

     

    public: bool SendSMS(String ^CellNumber, String ^SMSMessage)

                            {

                                        String ^MyMessage = nullptr;

                                        //Check if Message Length <= 160

                                        if (SMSMessage->Length <= 160)

                                                    MyMessage = SMSMessage;

                                        else

                                                    MyMessage = SMSMessage->Substring(0, 160);

                                        if (IsOpen == true)

                                        {

                                                    SMSPort->WriteLine("AT+CMGS=" + CellNumber + "r");

                                                    SMSPort->WriteLine(

                                                                MyMessage + System::Environment::NewLine + (char)(26));

                                        }

                                        return false;

                            }   

     

     

     

     

    Wednesday, May 14, 2008 10:29 AM
  • Code Snippet

    private: void ReadPort()

                             {

                                         String ^SerialIn = "";

                                         array<Byte> ^RXBuffer = gcnew array<Byte>(SMSPort->ReadBufferSize + 1);

                                         String ^SMSMessage = nullptr;

                                         int Strpos = 0;

                                         String ^TmpStr = nullptr;

                                         while (SMSPort->IsOpen == true)

                                         {

                                                     if ((SMSPort->BytesToRead != 0) & (SMSPort->IsOpen == true))

                                                     {

                                                                 while (SMSPort->BytesToRead != 0)

                                                                 {

                                                                             SMSPort->Read(RXBuffer, 0, SMSPort->ReadBufferSize);

                                                                             SerialIn =

                                                                                         SerialIn + System::Text::Encoding::ASCII->GetString(

                                                                                         RXBuffer);

                                                                             if (SerialIn->Contains(">") == true)

                                                                             {

                                                                                         _ContSMS = true;

                                                                             }

                                                                             if (SerialIn->Contains("+CMGS:") == true)

                                                                             {

                                                                                         _Continue = true;

                                                                                         _Wait = false;

                                                                                         SerialIn = String::Empty;

     

                                                                                      RXBuffer = gcnew array<Byte>(SMSPort->ReadBufferSize + 1);

                                                                             }

                                                                 }

               

                                                                 SerialIn = String::Empty;

                                                                 RXBuffer = gcnew array<Byte>(SMSPort->ReadBufferSize + 1);

                                                     }

                                         }

                             }   

     

     

     

    public: void Open()

                            {

                                        if (IsOpen == false)

                                        {

                                                    SMSPort->Open();

                                                    IsOpen=true;

                                                    ReadThread->Start();

                                        }

                            }   

     

    public: void Close()

                            {

                                        if (IsOpen == true)

                                        {

                                                    SMSPort->Close();

                                                    IsOpen=false;

                                        }

                            }   

     

    };

     

     

     

    Wednesday, May 14, 2008 10:31 AM
  • If you’d like to use native C++, you can use device communication functions to communicate with serial port; you can take following documents and sample code as reference

    http://msdn.microsoft.com/en-us/library/aa363194(VS.85).aspx

    http://www.codeproject.com/KB/system/serial_com.aspx

    http://www.codeproject.com/KB/system/serial.aspx

    http://msdn2.microsoft.com/en-us/library/aa363424(VS.85).aspx

     

    Hope this helps!

     

    Wednesday, May 14, 2008 10:36 AM
  • Thanx Yan-Fei Wei
    Ur code helped me alot.
    But regarding the com guids, i still dont understand how do i implement.

    According to my knowledge, the comclass in vc++ must be written in the xml manifest document.
    Can u help me how exactly i can implement these com guids.

    <ComClass(SMSCOMMS.ClassId, SMSCOMMS.InterfaceId, SMSCOMMS.EventsId)>_



    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <
    assembly xmlns="urnTongue Tiedchemas-microsoft-com:asm.v1" manifestVersion="1.0"
    >
       <
    assemblyIdentity name="Manifests.TheDLL" processorArchitecture="X86" type="win32" version="1.2.3.4"></assemblyIdentity
    >
       <
    dependency
    >
          <
    dependentAssembly
    >
             <
    assemblyIdentity type="win32" name="Microsoft.VC90.DebugCRT" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity
    >
          </
    dependentAssembly
    >
       </
    dependency
    >
       <
    dependency
    >
          <
    dependentAssembly
    >
             <
    assemblyIdentity type="win32" name="Microsoft.VC90.ATL" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity
    >
          </
    dependentAssembly
    >
       </
    dependency
    >
       <
    file name="Manifests.TheDLL.dll" hashalg="SHA1"
    >
          <
    comClass clsid="{FFA34D8E-7DF5-4D99-8CD0-5784FAC1944B}" tlbid="{5F6C3293-DAA3-4721-9A19-15735B069DB6}" description="TheCoClass Class"></comClass
    >
          <
    typelib tlbid="{5F6C3293-DAA3-4721-9A19-15735B069DB6}" version="1.0" helpdir="" flags="HASDISKIMAGE"></typelib
    >
       </
    file
    >
    </
    assembly>


    please have a look http://blogs.msdn.com/yvesdolc/archive/2008/05/09/native-manifests-let-s-do-com-and-forget-the-registry.aspx
    Friday, May 16, 2008 9:27 PM
  • Hi pinky_codey,

    May I know what does your interface look like because i am using your code but the problem is that you do not have the button to click when you sent the message. Hope to get the answer soon.

    Thanks

    Regards
    LiL_Is
    Wednesday, August 5, 2009 6:06 AM