Link to home
Start Free TrialLog in
Avatar of ipdicky
ipdicky

asked on

How to use VB.Net 2005 to send SMS via serial connect to GSM modem

Here is my code
========================================
Private WithEvents port As SerialPort

Public Sub New()
    port = New SerialPort("COM1")

    port.Parity = Parity.None
    port.StopBits = StopBits.One
    port.DataBits = 8
    port.Handshake = Handshake.None
    Try
        ' Open the serial port
        port.Open()
    Catch ex As Exception
        RaiseEvent SMSError(SMSStatus.PortOpenError)
    End Try
End Sub

Public Sub Send(ByVal toTel as String, ByVal msg As String)
    SendString("AT+CMGS=""" + toTel + """" + vbCrLf)
    SendString(msg + Chr(26) + Chr(90))
End Sub

Private Sub SendString(ByVal str As String)
    Debug.WriteLine(str)
    Debug.WriteLine(port.IsOpen.ToString())
    Try
        port.Write(str)
    Catch ex As System.Exception
        RaiseEvent SMSError(SMSStatus.SMSSendError)
    End Try
End Sub

Private Sub port_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived
    Dim recString As String = port.ReadLine()
    Debug.WriteLine("rec: " + recString)
End Sub
========================================

I have two questioins:
1) I try to use a null modem cable and hyper terminal to test the output of my program, I get the output has a different from hyper terminal to hyper terminal config (type the AT command from one hyper terminal and receive from another hyper terminal).  At hyper terminal to hyper terminal config, the Ctrl-Z is show a arrow only, but the output of my program is a arrow followed by a "Z" char.  How to send a correct Ctrl-Z out?

2) The DataReceived event n trigger even I try to type something at hyper terminal that at the other end of null modem cable

Thanks in advance!!!
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ipdicky
ipdicky

ASKER

Thank you very much!!! I try it and the reseult is positive.