Send SMS via Nokia Mobile Using AT Commands

2019-06-13 20:18发布

问题:

I'm trying to send sms with my Nokia cell phone (C1-01) using AT Commands and I can successfully send SMS with this vb.net code.

Button_Send_Click:

Dim SMSPort = New SerialPort
    With SMSPort
        .PortName = "COM2"
        .BaudRate = 9600
        .Parity = Parity.None
        .DataBits = 8
        .StopBits = StopBits.One
        .Handshake = Handshake.None
        .DtrEnable = True
        .RtsEnable = True
        .NewLine = vbCrLf
    End With

    SMSPort.Open()

    SMSPort.Write("AT+CMGF=1" & vbCrLf)
    Threading.Thread.Sleep(200)

    SMSPort.Write("AT+CMGS=" & Chr(34) & TextBox1.Text & Chr(34) & vbCrLf) 'TextBox1.text = Recipient mobile number and chr(34) = "
    Threading.Thread.Sleep(200)

    SMSPort.Write("TEST MESSAGE" & Chr(26)) 'chr(26) = →
    Threading.Thread.Sleep(200)

    MsgBox(SMSPort.ReadExisting())

    SMSPort.Close()

It's working fine for the 1st time, after fire this code I received SMS on my cell phone TEST MESSAGE(Brilliant) BUT when I press send button in second time I received SMS on my cell phone which contains: AT+CMGF=1 AT+CMGS=+92XXYYYYYY TEST MESSAGE

Why in second time it's including "AT Commands i.e AT+CMGF etc.." in SMS? How can I remove unwanted text from this? I have also try SMSPort.DiscardInBuffer() and SMSPort.DiscardOutBuffer() Properties after opening and before closing my serial port (SMSPort) but my issue is not resolving. I have googled alot but all is vain, Please help me to resolve this issue.

Platform: Microsoft Visual Basic 2010 with .NET 2.0

回答1:

First of all, you must seriously redo your AT command handling to

  • Read and parse the every single response line given back from the modem until you get a final result code. This applies for every single command line invocation, no exceptions whatsoever. See this answer for more details.
  • Never ever call Threading.Thread.Sleep in any code that handles AT commands. See this answer for more details about the risk of aborting the next command.
  • For AT+CMGS specifically you also MUST wait for the "\n\r> " response before sending data, see this answer (again) for more details.

Before fixing these fundamental issues you cannot expect any successful behaviour.



回答2:

OK, I came across the same issue today, and I solved it by adding "\r\n" to the end of the message, before sending CTRL+Z (char 26). You may try it with just "\n"



回答3:

Try this

Dim SMSPort = New SerialPort
    With SMSPort
        .PortName = "COM2"
        .BaudRate = 9600
        .Parity = Parity.None
        .DataBits = 8
        .StopBits = StopBits.One
        .Handshake = Handshake.None
        .DtrEnable = True
        .RtsEnable = True
        .NewLine = vbCrLf
End With

SMSPort.Open()

SMSPort.WriteLine("AT" & Chr(13))
Threading.Thread.Sleep(200)

SMSPort.WriteLine("AT+CMGF=1" & Chr(13))
Threading.Thread.Sleep(200)

SMSPort.WriteLine("AT+CMGS=" & Chr(34) & TextBox1.Text & Chr(34)) 
Threading.Thread.Sleep(200)

SMSPort.WriteLine("TEST MESSAGE" & Chr(26))
Threading.Thread.Sleep(200)

MsgBox(SMSPort.ReadExisting())

SMSPort.Close()

Add SMSPort.WriteLine("AT" & Chr(13)) and I replaced Write with WriteLine and removed vbCrlf. Also added Chr(13) to SMSPort.WriteLine("AT+CMGF=1")