3

I want to have a serial communication using Siemens S7-1200 with CM 1241 (RS-232) and communicate with my Arduino. Here is the setup of the communication. I have 2 temperature sensor and one Led connected to my Arduino, and on the PLC side I have S7-1200 from Siemens and CM-1241. Arduino and my PLC are connected just by using Tx and Rx pin no handshake is done.

I am sending the temperature data from both the sensor to the PLC. on the PLC side i decide when to turn on the Led connected to my arduino depending on the different temperature values. I have assigning a ID to both the sensors before sending out the data. This is how the transmitted data from Arduino looks like $AOPT_TEMP1_20_TEMP2_21 .

So far its good, I am receiving serial data on my PLC using RCV_PTP (received data are placed on the buffer) and sending data using SEND_PTP. I have also implement a filter on the PLC which will only accepts the serial data starting with '$AOPT_'. Now, I want to receive the temperature value from two temperature sensor TEMP1 & TEMP2 and then control the Led. For instance if (TEMP1>TEMP2 ) then turn ON the Led else turn OFF.

I am able to receive the data on the PLC from Arduino but now I don't know how to proceed with comparing the received information. How do i extract the only required data from the received buffer? Any suggestions will be highly appreciated.

Thanks in advance....

ZF007
  • 3,708
  • 8
  • 29
  • 48

1 Answers1

0

It is simple to parse strings (from the serial buffer) in SCL: You can use commands: **

LEN
CONCAT
LEFT or RIGHT
MID
INSERT
DELETE
REPLACE
FIND
EQ_STRNG and NE_STRNG
GE_STRNG and LE_STRNG
GT_STRNG and LT_STRNG
INT_TO_STRING and
STRING_TO_INT
DINT_TO_STRING and
STRING_TO_DINT
REAL_TO_STRING and
STRING_TO_REAL

** Found on this SCL cheat sheet:http://plc4good.org.ua/files/03_downloads/SCL_table/SCL-cheat-sheet.pdf

I would start with..

  • Creating a function block in SCL.
  • Add an input attribute as a string
  • Add Two output attributes (Temp1,Temp2) as Reals or Ints
  • Several Static variables for temporary strings and text->real conversion.

Parse your code similar to the following(since I don't have my TIA portal this may need modifying): for your string "$AOPT_TEMP1_20_TEMP2_21" assuming the beginning is always "$AOPT_TEMP1_" (12 Chars)

temp1_temp:=DELETE(IN1:=inputmsg,IN2:='$AOPT_TEMP1_',L:=12,P:=0);

//result should be "20_TEMP2_21"
//if you have a result above or below a 2 digit number we can't just get 
//the next two chars in the string.  so we use the FIND.

temp1_endpos:=FIND(IN1:=temp1_temp,IN2:='_');
temp1_str:=LEFT(IN1:temp1_temp,L:=temp1_endpos);
Temp1:=string_to_real(temp1_str); 

//work off of the position of the temp1_endpos and the string stored in
//temp1_temp

temp2_str:=RIGHT(IN1:=temp1_temp,LEN(temp1_temp)-temp1_endpos-6);

//working from the right side of the string 
// 20_TEMP2_21
//   ^-------pos 2   temp2_ is another 6 so we subract another 6
//         ^---pos 6
// len was (in this case) 11, we work from the right because we don't 
    // know how many digits each temp may be.

Temp2:=string_to_real(temp2_str);  

Keep in mind this is all off the top of my head and using the manual for quick references: https://cache.industry.siemens.com/dl/files/465/36932465/att_106119/v1/s71200_system_manual_en-US_en-US.pdf

Some things may be need adjusting. If you don't/ can't use SCL these blocks exist in the Ladder as well. If you can you can just wire this function block up executing only after you receive your buffer.

Entrabiter
  • 752
  • 7
  • 13