===== Receiving SMS ===== The **receiveSMS** function block in the base **is not intended for** use in **FBD**, as it would query messages in each program cycle. However, it can be used in FBD with appropriate modifications This function block has no input variables, but contains 5 outputs: * **channel:** variable of type string, contains the designation of the alarm channel on which the SMS was received (in case of multiple modems) * **sender:** variable of type string, contains telephone number in international format (e.g. +420777666555) * **message:** variable of type string, contains the text of the SMS message itself * **receivetime:** variable of type DT, contains the time when the message was received The last output is an unlabeled output, which is of type integer and contains a numeric status with the result of the operation. More is described below in the text (for the output result). ---- Since the block is intended only for use in the ST, it is necessary to create your own function block written in the ST for its use. This is only possible in **Full Mode** solution. The procedure for switching to full mode can be found [[en:sw:01-mervis:creating-new-project-hidden#prepnuti_do_plneho_modu|here in the introductory article]]. To receive an SMS, create a function block by right-clicking on the project (e.g. in executable projects). {{ :en:sw:01-mervis:sms_ide13.png?direct |}} ---- A dialog box will appear, select/paste here: * **Name:** ReceiveSMS_fbd * **Type:** FunctionBlock * **Language:** st * **Namespace:** v0_0 and confirm by clicking OK. {{ :en:sw:01-mervis:sms_ide14.png?nolink |}} ---- A window with the code structure example of the function block will open. Delete all the contents of the block and replace them with the following code: NAMESPACE v0_0 FUNCTION_BLOCK ReceiveSMS_fbd VAR trigger : R_TRIG; readPulse : bool := 0; END_VAR VAR_INPUT readSMS : bool := 0; END_VAR VAR_OUTPUT result : int := 31; sms_channel : string := ""; sender : string := ""; message : string := ""; receive_time : dt := dt#0001-01-01-00:00:00; END_VAR trigger(CLK:=readSMS,Q=>readPulse); IF readPulse THEN result := MESSAGING.RECEIVESMS( CHANNEL => sms_channel, SENDER => sender, MESSAGE => message, RECEIVETIME => receive_time ); END_IF; END_FUNCTION_BLOCK END_NAMESPACE The result should look like this: {{ :en:sw:01-mervis:sms_ide15.png?direct |}} ---- You can now close the function block window, then save and compile the solution. Then open **main.program.fbd** and right-click on an empty space and select from the context menu: **Add library box -> Add library box**. {{ :en:sw:01-mervis:sms_ide16.png?direct |}} ---- A dialog window for searching for functions and function blocks opens. In the **Name** field, enter a name of the block: //ReceiveSMS_fbd//, highlight it and click OK. {{ :en:sw:01-mervis:sms_ide17.png?direct |}} ---- This inserts the block into the program space: {{ :en:sw:01-mervis:sms_ide18.png?direct |}} ---- Now you need to create several test variables. First, create an input variable **trigger** of type bool and enter **False** as its initial value. Connect the variable to the **readSMS** input of the block: {{ :en:sw:01-mervis:sms_ide19.png?direct |}} ---- Next, create the output variables **channel**, **sender**, and **message**, all of the type string. Lastly, create a variable **receivetime** of type DT. Since all variables are output, it is not necessary to set their initial values. Finally, connect them to the block. The result should look something like this: {{ :en:sw:01-mervis:sms_ide20.png?direct |}} ---- Upload the solution and start debugging. Each time the value of the variable **trigger** is changed from **False** to **True**, one message is read from the SIM card memory. {{ :en:sw:01-mervis:sms_ide21.png?direct |}} The **result** output will be **0** if the message was read correctly and **31** if no message is available. Other values indicate an error, see the help (F1 key) for the **receivesms** function block.