Search code examples
vb.netsmsmodem

Detect response from Modem?


I'm working with a Teltonika G10 GSM modem and wrote up a basic program to send out SMS. I put a 1.5 second timer between each AT command to allow the modem to simulate the wait for the "OK" from the modem. This works for now but I'd rather use a branching statement wait for an actual response such as "OK" or "ERROR" rather than using a timer.

        SerialPort1.Write("AT+CMGD=1,4" & vbCrLf)
        Thread.Sleep(1250)
        SerialPort1.Write("AT+CMGF=1" & vbCrLf)
        Thread.Sleep(1250)
        SerialPort1.Write("AT+CMGS=" & Chr(34) & "3475558223" & Chr(34) & vbCrLf)
        Thread.Sleep(1250)
        SerialPort1.Write(":|" & Chr(26))

Solution

  • I was new to programming with AT commands and had spent a good deal of time with Putty to get an understanding of it. Hans Passant suggeested to use .ReadLine() and it's worked great.

    Below is a sample of code that's worked great. It basically submits a command to the modem and will only continue if the modem responds with an "OK".

     If modem.IsOpen() Then
            modem.Write("AT+CMGD=" & Chr(34) & "ALL" & Chr(34) & vbCrLf)   'deletes last received message
            'Sets Modem to Text
            While (modem.ReadLine().ToString <> "OK")
                modem.Write("AT+CMGF=1" & vbCrLf)
            End While
            While (modem.ReadLine().ToString <> "OK")
                modem.Write("AT+CSMP=17,167,0,0" & vbCrLf)
            End While
    
            While (modem.ReadLine().ToString <> "OK")
                modem.Write("AT+CNMI=1,1,0,0,0" & vbCrLf)
            End While
        End If