enzo
i know its been a little more than a couple of weeks, however i have pulled some of my code that will do a loop around a GSM modem and check for messages, grab the phone number it was sent from, the message and then perform a task based on the message. the code is optoscript and assumes a few things.
1 - com handle for the gsm modem is 'gsm_com_port’
2 - ‘sms_string_var’ is a string variable that will hold some temp strings
3 - ‘sms_return_str’ is the return string from the modem
4 - ‘sms_rx_number’ is the ph number that sent the message
5 - the modem you are using accepts standard AT commands for sms stuff
6 - your modem is configured for the correct parameters.
7 - ‘sms_control’ is the name of the chart you are using (incase we get a timeout, this chart stops and set an error variable true)
/******************************************************************************************************/
/******************************************************************************************************/
/* this will check to see if any sms's have arrived and wether their content needs us to do anything */
/******************************************************************************************************/
/******************************************************************************************************/
SetVariableFalse(sms_received);
SetVariableFalse(sms_msg_to_send);
sms_count = 0;
/*************************************************************************************/
/* This part makes sure that there is communication between the controller and modem*/
/*************************************************************************************/
/* Build the 'at' string and send it */
ClearCommunicationReceiveBuffer(gsm_com_port);
sms_string_var = "at";
sms_string_var += chr(13);
sms_com_port_error = TransmitString(sms_string_var,gsm_com_port);
/* Wait until there is an OK from the modem */
repeat
ReceiveString(sms_return_str, gsm_com_port);
IncrementVariable(sms_count);
until ((FindSubstringInString("OK", 0, sms_return_str) >= 0) or (sms_count > 10));
DelayMsec(100); //this chart is a gun so we need to slow things down a bit for internal processing
/* if count got to 10 then there is something wrong with the communications so set the error flag and bail */
if (sms_count > 10) then
SetVariableTrue(sms_modem_error);
StopChart(sms_control);
endif
/********************************************************************************************/
/* now read list all the messages in the modem and delete the one we read and list them all*/
/* again. I know its cumbersome, but it guarantees success */
/********************************************************************************************/
/* list all the messages in the phone */
sms_string_var = "at+CMGL="; //read sms at command
sms_string_var += chr(34); //stick in the "
sms_string_var += "ALL"; //stick in the ALL
sms_string_var += chr(34); //stick in the "
sms_string_var += chr(13); //now for the <CR>
sms_com_port_error = TransmitString(sms_string_var,gsm_com_port); //send it down the cable
DelaySec(1); //this chart is a gun so we need to slow things down a bit for internal processing
/* get the string in from the buffer until we find +CMGL or OK or the loop expires */
repeat
IncrementVariable(sms_count); //so we don't get tied up in the loop forever
DelayMsec(100);
ReceiveString(sms_return_str, gsm_com_port);
until ((FindSubstringInString("+CMGL:", 0, sms_return_str) >= 0) or (sms_count > 10) or (FindSubstringInString("OK", 0, sms_return_str) >= 0));
/* if count got to 10 then there is something wrong with the communications so set the error flag and bail */
if (sms_count > 10) then
SetVariableTrue(sms_modem_error);
StopChart(sms_control);
endif
DelayMsec(100); //this chart is a gun so we need to slow things down a bit for internal processing
/* if we got an ok from the modem straight up, then there are no more msg's so don't do any more */
if (FindSubstringInString("OK", 0, sms_return_str) < -1) then //-57 means that the string was not found, so if we haven't got to an
//OK string then we haven't finished checking the list
sms_return_str_len = GetStringLength(sms_return_str); //find out how long the string is
/* find the index number of the msg so we can delete it after we have processed it */
sms_return_str_srt = FindSubstringInString(" ", 0, sms_return_str); //find out where the 1st space is
sms_return_str_end = FindSubstringInString(",", sms_return_str_srt, sms_return_str); //find out where the 1st comma is
sms_msg_index_len = (sms_return_str_end - 1) - (sms_return_str_srt); //this gives us the length of the string we need to read
GetSubstring(sms_return_str, sms_return_str_srt+1, sms_msg_index_len, sms_msg_index); //stick the index number into the var
DelayMsec(100); //this chart is a gun so we need to slow things down a bit for internal processing
/* now get the phone number the msg was sent from so we can reply if we have to */
sms_return_str_srt = FindSubstringInString(",", 10, sms_return_str); //find out where the 2nd comma is
sms_return_str_end = FindSubstringInString(",", sms_return_str_srt+1, sms_return_str); //find out where the 2nd comma is
sms_return_str_len = (sms_return_str_end - 2) - (sms_return_str_srt +1); //this gives us the length of the string we need to read
GetSubstring(sms_return_str, sms_return_str_srt+2, sms_return_str_len, sms_rx_number); //get the phone number
DelayMsec(100); //this chart is a gun so we need to slow things down a bit for internal processing
/* now that we have the number, check to see if its a number we will allow through our 'firewall' (heh, heh...) */
//if ((FindSubstringInString("409217740", 0, sms_rx_number) >= 0) or (FindSubstringInString("400448668", 0, sms_rx_number) >= 0)) then
/* now get the sms msg */
sms_return_str = ""; //clear the return string
ReceiveString(sms_return_str, gsm_com_port); //read the string from the buffer
sms_count=0; //reset the count value
repeat
DelayMsec(100);
IncrementVariable(sms_count);
until ((GetStringLength(sms_return_str) > 1) or (sms_count > 10));
DelayMsec(100); //this chart is a gun so we need to slow things down a bit for internal processing
StringToUpperCase(sms_return_str); //convert string to uppercase to avoid confusion
/* now lets see if the msg has what we are looking for */
//these next statements are where we check the string we are filtering for
////////////////////////////////////////////////////////////////////////////////
// f we find a *4320 in the string then
// we will then execute a command based on what we find
if (FindSubstringInString("*4320", 0, sms_return_str) >= 0) then
//here is where you can do findsubstringinstring and execute the command or chart required
endif
//
//
///////////////////////////////////////////////////////////////////////////////////////
//endif
/* right lets clear the buffer so we can delete the msg. I know this is a cumbersome way to do things but it ensures success */
repeat
ReceiveString(sms_return_str, gsm_com_port);
//email_error_main_msg[sms_count] = sms_return_str;
IncrementVariable(sms_count);
DelayMsec(10);
until ((FindSubstringInString("OK", 0, sms_return_str) >= 0) or (sms_count > 20));
ClearCommunicationReceiveBuffer(gsm_com_port);
DelayMsec(100); //this chart is a gun so we need to slow things down a bit for internal processing
/* now lets delete the msg that we just read */
sms_count = 0; //reset the loop counter
sms_string_var = "at+CMGD="; //read sms at command
sms_string_var += sms_msg_index;
sms_string_var += chr(13);
sms_com_port_error = TransmitString(sms_string_var,gsm_com_port);
DelaySec(2); //this chart is a gun so we need to slow things down a bit for internal processing
repeat
ReceiveString(sms_return_str, gsm_com_port);
//email_error_main_msg[sms_count] = sms_return_str;
IncrementVariable(sms_count);
DelayMsec(100);
until ((FindSubstringInString("OK", 0, sms_return_str) >= 0) or (sms_count > 10));
/* if the return string is "OK" then we have got to the end of the list so get outta here */
else
SetVariableTrue(sms_receive_complete); //when we have finished getting all the messages, then set this var true
endif
the block is executed when the RI of the modem goes greater than 3VDC, therefore RI (Pin 9 of the DB9 connector) needs to be hooked up to an AI -10 to +10VDC input.
this code has been in place for several years and has worked flawlessly (AFAIK). it could do with an overhaul, however i have not needed to therefore never have…
i hope this helps you in your quest to control via sms.
Nick