Search the Community
Showing results for tags 'usb-gsm'.
-
Hello Recently, there was a need to develop an SMS gateway. The machine is supposed to send notifications to employees, the number of sms sent is not much 10-50 a month, so it makes no sense to buy a subscription to the www-sms API. The machine is based on a USB-GSM modem: LC SIM800C V3 (aliexp.. price 3-8$) I used UDF: ComUDF.au3 Thank you mLipok for your work! Below is the program code with an example WARNING! The program is written very extensively (not optimized) - it was created with a novice programmer as part of exercises. But it makes it very understandable (lots of comments) There are ONLY functions here: - modem initialization - sending SMS - deleting received sms (no possibility of reading) Maybe someone will be useful for further experiments with AT commands to communicate with the modem. The biggest challenge for me was the dependence of AT commands in different modem operation modes (instruction AT command for chip SIM800 ONLY 380 pages !) LCSIM800CV3.au3 #include "ComUDF.au3" #include <Array.au3> #include <Timers.au3> Global Const $SMS_NOPL = 0 ; send sms not POLISH characters set the SMS mode "GSM" look AT command AT+CSCS Global Const $SMS_PL = 1 ; send sms with POLISH characters set the SMS mode "HEX" look AT command AT+CSCS ; Write setting from PC with modem USB Global $LCSIM800CV3_sComPort = 'COM3' ; a com port with USB modem, e.g. COM1 Global $LCSIM800CV3_sPortSettings = ' baud=9600 data=8' ; speed settings (for default values see _COM_OpenPort) Global $LCSIM800CV3_hComPort Local $LCSIM800CV3_aPortsList = _COM_ListPorts() ; an array with COM ports ;_ArrayDisplay($LCSIM800CV3_aPortsList) ; displays the ports found in OS _Example() ; example function ; #FUNCTION# ==================================================================================================================== ; Name ..........: _Example ; Description ...: Declaring an array with data to send an SMS (various phone number formats) ; Sending SMS in "HEX" mode ; Sending SMS in "GSM" mode ; Syntax ........: _Example() ; Parameters ....: None ; Return values .: None ; Author ........: ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _Example() ;MODIFY number phone vvvvvvv and vvvvvvvv Local $aTest[2][2] = [['123456789','Test pl liter: ęóąśłżźćń'],['+48123456789','Test PL liter: ĘÓĄŚŁŻŹĆŃ']] ConsoleWrite('==START send sms mode "HEX" with diacritical character==' & @CRLF) $Return = _SendSMSFromArray($aTest, $SMS_PL) $Error = @error $Extended = @extended If $Error Then ConsoleWrite('$Error: ' & $Error & @CRLF) ConsoleWrite('$Extended: ' & $Extended & @CRLF) ConsoleWrite('$Return: ' & $Return & @CRLF) Else _ArrayDisplay($Return, '$SMS_PL') EndIf ConsoleWrite('==END send sms mode "HEX" with diacritical character==' & @CRLF) ConsoleWrite('==START send sms mode "GSM" only basic alphabet==' & @CRLF) $Return = _SendSMSFromArray($aTest, $SMS_NOPL) $Error = @error $Extended = @extended If $Error Then ConsoleWrite('$Error: ' & $Error & @CRLF) ConsoleWrite('$Extended: ' & $Extended & @CRLF) ConsoleWrite('$Return: ' & $Return & @CRLF) Else _ArrayDisplay($Return, '$SMS_NOPL') EndIf ConsoleWrite('==END send sms mode "GSM" only basic alphabet==' & @CRLF) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _SendSMSFromArray ; Description ...: Complete SMS sending function (opening the COM port; modem initialization; ; sending a text message; closing the COM port) ; Syntax ........: _SendSMSFromArray($aTableSMS, $PL) ; Parameters ....: $aTableSMS - an 2D array [NumberPhone_1][TextSMS_1] ; [NumberPhone_n][TextSMS_n] ; $PL - $SMS_NOPL (0) NO PL char send normal text "GSM" ; - $SMS_PL (1) PL char send mode "HEX" ; Return values .: success - input array and add col with status ; [NumberPhone_1][TextSMS_1][status_send _1] ; [NumberPhone_n][TextSMS_n][status_send _n] ; fail - set @error and description error ; Author ........: ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _SendSMSFromArray($aTableSMS, $PL) Local $Return, $Error, $Extended, $i $Return = _InitModem($PL) $Error = @error $Extended = @extended If $Error Then _COM_ClosePort($LCSIM800CV3_hComPort) Return SetError(1, 1, '[_SendSMSFromArray] ' & $Return) EndIf _DelAllSMS() _ArrayColInsert($aTableSMS, 2) ; add col to status sending sms For $i=0 to UBound($aTableSMS, $UBOUND_ROWS)-1 $Return = _SendSMS($aTableSMS[$i][0], $aTableSMS[$i][1], $PL) $Error = @error $Extended = @extended If $Error Then $aTableSMS[$i][2] = $Return Else $aTableSMS[$i][2] = 'OK' EndIf Next _COM_ClosePort($LCSIM800CV3_hComPort) Return $aTableSMS EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _InitModem ; Description ...: Open port COM, verify modem reposnse, send command setting SMS mode ; Syntax ........: _InitModem($PL) ; Parameters ....: $PL - 0 no PL character - SMS mode "GSM" ; 1 PL character - SMS mode "HEX" ; Return values .: success - 1 ; failure - set @error and return description error ; Author ........: ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _InitModem($PL) Local $Return, $Error, $Extended $Return = _COM_OpenPort($LCSIM800CV3_sComPort & $LCSIM800CV3_sPortSettings) $Error = @error If $Error Then Return SetError(1, 1, '[_Init] error open port: ' & $LCSIM800CV3_sComPort & $LCSIM800CV3_sPortSettings) EndIf $LCSIM800CV3_hComPort = $Return $Return = __SendToModem('AT') ; ping modem If StringInStr($Return, 'OK') = 0 Then Return SetError(1, 2, '[_Init] communication error with modem - AT command returned: ' & $Return) EndIf $Return = __SendToModem('ATE0') ; disable "echo" 0= send "AT" received: "OK"; 1= send "AT" received: "AT @CR OK" If StringInStr($Return, 'OK') = 0 Then Return SetError(1, 2, '[_Init] communication error with modem - ATE0 command returned: ' & $Return) EndIf $Return = __SendToModem('AT+CMGF=1') ; format wiadomości SMS (0 - PDU, 1 - Text) If StringInStr($Return, 'OK') = 0 Then Return SetError(1, 2, '[_Init] communication error with modem - AT+CMGF=1 command returned: ' & $Return) EndIf If $PL=$SMS_NOPL Then $Return = __SendToModem('AT+CSCS="GSM"') ; set mode "GSM" all character is normal text If StringInStr($Return, 'OK') = 0 Then Return SetError(1, 2, '[_Init] communication error with modem - AT+CSCS=""GSM"" command returned: ' & $Return) EndIf $Return = __SendToModem('AT+CSMP=17,167,0,0') ; additional settings to "GSM" If StringInStr($Return, 'OK') = 0 Then Return SetError(1, 2, '[_Init] communication error with modem - AT+CSMP=17,167,0,0 command returned: ' & $Return) EndIf ElseIf $PL=$SMS_PL Then $Return = __SendToModem('AT+CSCS="HEX"') ; set mode "HEX" all character is hex 4digit If StringInStr($Return, 'OK') = 0 Then Return SetError(1, 2, '[_Init] communication error with modem - AT+CSCS=""HEX"" command returned: ' & $Return) EndIf $Return = __SendToModem('AT+CSMP=17,167,0,8') ; additional settings to "HEX" If StringInStr($Return, 'OK') = 0 Then Return SetError(1, 2, '[_Init] communication error with modem - AT+CSMP=17,167,0,8 command returned: ' & $Return) EndIf Else Return SetError(1, 2, '[_Init] incorrect parameter $PL:' & $PL) EndIf Return 1 EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _DelAllSMS ; Description ...: Deletes all received sms ; Syntax ........: _DelAllSMS() ; Parameters ....: None ; Return values .: success - 1 ; failure - set @error and return description error ; Author ........: ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _DelAllSMS() $Return = __SendToModem('AT+CMGDA="DEL ALL"') ; send command AT delete all sms and receives text "OK" If StringInStr($Return, 'OK') = 0 Then Return SetError(1, 1, '[_DelAllSMS] SMS erase error - AT+CMGDA="DEL ALL" command returned: ' & $Return) EndIf Return 1 EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _SendSMS ; Description ...: Send sms. Restriction of sending to the country POLAND (+48) ; Syntax ........: _SendSMS($sPhoneNumber, $sSMS, $PL) ; Parameters ....: $sPhoneNumber - a string value. ; $sSMS - a string value. ; $PL - 0=no PL character - SMS mode "GSM"; 1=PL character - SMS mode "HEX" ; Return values .: success - 1 ; failure - set @error and return description error ; Author ........: ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _SendSMS($sPhoneNumber, $sSMS, $PL) Local $Return, $Error, $Extended ;verifying and modifying of the telephone number $sPhoneNumber = StringRegExpReplace($sPhoneNumber, "\D", '') If Not StringLen($sPhoneNumber) = 9 Or Not StringLen($sPhoneNumber) = 11 Then ;Number MUST 9 or 11 digit Return SetError(1, 9, '[_SendSMS] wrong phone number: ' & $sPhoneNumber) EndIf ;START ONLY country +48 ;NOTE this section allows you to send text messages only to the area code PL ;CHANGE 48 with your country code or delete this part If StringLen($sPhoneNumber) = 9 Then $sPhoneNumber = '+48' & $sPhoneNumber ; ElseIf StringLen($sPhoneNumber) = 11 Then If StringLeft($sPhoneNumber, 2) <> '48' Then Return SetError(1, 9, '[_SendSMS] wrong phone area code: ' & $sPhoneNumber) Else $sPhoneNumber = '+' & $sPhoneNumber EndIf EndIf ;END ONLY country +48 ;verifying and modifying of the text sms If StringLen($sSMS) > 250 Then Return SetError(1, 10, '[_SendSMS] the content of the SMS exceeds 250 characters: ' & StringLen($sSMS)) EndIf ;sending SMS ;step 1 -send phone number & <CR> $Return = __SendToModem('AT+CMGS="' & $sPhoneNumber & '"') ; send command AT & number and receives char ">" If StringInStr($Return, '>') = 0 Then Return SetError(1, 1, '[_SendSMS] modem communication error - command AT+CMGS="' & $sPhoneNumber & '": ' & $Return) EndIf ;step 2 - send text sms & <ESC> / Ctrl+Z If $PL=$SMS_PL Then ; convert to HEX with PL char $sSMS = Hex(StringToBinary($sSMS, $SB_UTF16BE)) ElseIf $PL=$SMS_NOPL Then ; $SMS_NOPL $sSMS = __ReplacePLChar($sSMS) ; if "GSM" then normal text and replace ą=>a etc. Else Return SetError(1, 1, '[_SendSMS] incorrect parameter $PL:' & $PL) EndIf $Return = __SendToModem($sSMS, Chr(26)) ;send text and Ctrl-Z If StringInStr($Return, 'ERROR') > 0 Then Return SetError(1, 2, '[_SendSMS] modem communication error - command AT+CSCS="GSM" returned: ' & $Return) ElseIf StringInStr($Return, '+CMGS:') > 0 And StringInStr($Return, 'OK') > 0 Then Return 1 Else Return SetError(1, 3, '[_SendSMS] communication error with modem - unknown answer: ' & $Return) EndIf EndFunc Func __SendToModem($sText, $sEnter=Chr(13)) Local $Return, $Error, $hTimerStart If $sEnter <> Chr(13) And $sEnter <> Chr(26) Then Return SetError(1, 10, '[__SendToModem] argument value not allowed $sEnter: ' & $sEnter) EndIf $Return = _COM_SendString($LCSIM800CV3_hComPort, $sText & $sEnter) $Error = @error If $Error Then Return SetError(1, 1, '[__SendToModem][_COM_SendString] it returned an error: ' & $Error) EndIf Sleep(500) ; wait 500ms to allow the receive buffer to fill up $hTimerStart = _Timer_Init() Do Do Sleep(100) $Return = _COM_GetInputcount($LCSIM800CV3_hComPort) $Error = @error If $Error Then Return SetError(1, 1, '[__SendToModem][_COM_GetInputcount] it returned an error: ' & $Error) EndIf $iCount = $Return If _Timer_Diff($hTimerStart) > 61*1000 Then ;WARNING! Must be> 60 sec - instruction from modem: "Max response time send sms: 60s" Return SetError(1, 1, '[__SendToModem][_COM_GetInputcount] no answer from modem for 60 seconds') EndIf Until $iCount > 0 $Return = _COM_ReadString($LCSIM800CV3_hComPort, $iCount) $Error = @error If $Error Then Return SetError(1, 1, '[__SendToModem][_COM_ReadString] it returned an error: ' & $Error) EndIf Until StringInStr($Return, '+CMTI: "')=0 ; we omit modem responses informing about receiving an SMS Return $Return EndFunc ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name ..........: __ReplacePLChar ; Description ...: Replace polish characters to NO polish ; Syntax ........: __ReplacePLChar($sText) ; Parameters ....: $sText - a string value. ; Return values .: String no polish character ; Author ........: ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func __ReplacePLChar($sText) $sText = StringReplace($sText, 'Ą', 'A', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'ą', 'a', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'Ć', 'C', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'ć', 'c', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'Ę', 'E', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'ę', 'e', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'Ł', 'L', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'ł', 'l', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'Ń', 'N', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'ń', 'n', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'Ó', 'O', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'ó', 'o', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'Ś', 'S', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'ś', 's', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'Ź', 'Z', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'ź', 'z', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'Ż', 'Z', 0, $STR_CASESENSE) $sText = StringReplace($sText, 'ż', 'z', 0, $STR_CASESENSE) Return $sText EndFunc