Anyone have experience with communicating with a Telit module or any other GSM\GPRS module? What technique people use to communicate using AT commands.
When i send a command such as AT, i need to wait for a response(eg OK) before i send another command, so i know it executed correctly.
The issue i am having is the timing, when do i know when to send the next command?
eg
//SendCommand reads the serial buffer for data and waits a certain period before returing…
if (sResult == "AT#SD=1,0,55006,\"190.25.129.1\"||CONNECT|")
{
sResult = SendCommand(data, true, 3000);
sResult = SendCommand("+++", false, 5000);
if (sResult == "|NO CARRIER|")
{
sResult = SendCommand("AT#SH=1", false, 4000);//Close the Socket without deactivating the context
return true;
}
else
{
sResult = SendCommand("AT#SH=1", false, 4000);//Close the Socket without deactivating the context
return false;
}
}
else
{
return false;
}
I’ve done drivers for a fair segment of the SIMCOM line and ESP8266. One of the key challenges is managing unsolicited output (which all of these generate). That is, while you are waiting for a response, all kinds of other stuff can come back that you don’t want to just ignore, and which can mess up the response you are waiting for.
For that reason, I have used the following approach:
[ul]All send actions are synchronous and performed on the calling thread, and then call an ‘Expect’ function which blocks looking for a timeout or an expected response
A background thread listens for received characters (which can either be part of a command stream or TCP stream)
The background thread extracts well-known unsolicited notifications (incoming call, received text, closed socket, etc) and routes them to event handlers
The background thread extracts TCP stream characters and routes them to sockets
The background thread pushes all remaining terminated lines to a command-response queue. If one of those items matches something from an Expect, then it is returned to that caller.[/ul]
Sending AT commands sounds straightforward, but when you throwin the command echos, command responses, unsolicited notifications, and streamed bufferes that all of these boards generate, it gets a bit hairy.
Part of your init can disable all unsolicited result codes. There are a number of AT commands to do this. Search for unsolicited in the AT command manual to get more details. I’ve done this with the CellularModem driver and it has made things more stable.
SIM5320E and A versions. I also have some legacy SIM900B modules in a few old projects. The SIM5320 is 3G capable.
The latest CellularModem driver works quite well in this respect. It is Gadgeteer based but you can get the source and easily modify it to work with pure NETMF with some minor changes.
I had to put in a change though for sending of SMS in sequence. The driver will fail if it has not received back the confirmation so I block with a time on the call to send sms if there is still a pending message send. It now works to send multiple message in sequence without waiting between calls to the send function.
//
// Make sure the last message was sent first
//
if(! smsMessageSent)
{
long msTimer = UtilsClass.msTime() + 5000; // 5 seconds max wait
while(! smsMessageSent && UtilsClass.msTime() < msTimer)
{
Thread.Sleep(100);
}
}
this.WriteLine("AT+CMGS=\"" + number + "\"\r");
Thread.Sleep(100);
this.WriteLine(message);
Thread.Sleep(100);
smsMessageSent = false; // Block and other call to send until it actually sends
this.WriteLine((char)26 + "\r");
UtilsClass is just a bunch of useful calls and one is to get the current millisecond time from the system.
Then in the receive thread (DoWork) I have this extra check: