2015-09-06 270 views
1

我想用西門子S7-1200與CM 1241(RS-232)進行串行通訊並與我的Arduino通訊。 這是通信的設置。我有兩個溫度傳感器和一個連接到我的Arduino的LED,在PLC一側有西門子和CM-1241的S7-1200。 Arduino和我的PLC只通過Tx和Rx引腳連接,沒有握手。西門子PLC與Arduino之間的串行通訊

我將溫度數據從傳感器發送到PLC。在PLC端,我決定何時打開連接到我的arduino的LED,具體取決於不同的溫度值。在發送數據之前,我已經爲這兩個傳感器分配了一個ID。這就是來自Arduino的傳輸數據如何看起來像$ AOPT_TEMP1_20_TEMP2_21。

到目前爲止,我在使用RCV_PTP(接收到的數據放在緩衝區中)接收我的PLC上的串行數據並使用SEND_PTP發送數據。我還在PLC上實現了一個過濾器,它只接受以'$ AOPT_'開頭的串行數據。現在,我想從兩個溫度傳感器TEMP1接收溫度值,然後控制LED。例如,如果(TEMP1> TEMP2)然後打開,則其他LED關閉。

我能夠從Arduino接收PLC上的數據,但現在我不知道如何繼續比較接收到的信息。我如何從接收緩衝區中提取唯一所需的數據?任何建議將不勝感激。

在此先感謝....

+0

只是爲了確認,你問PLC側的文本處理? –

+0

是的,在PLC一側 –

回答

0

它是簡單的SCL解析字符串(從串行緩衝器): 您可以使用命令: **

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 

** 找到在這個SCL小抄:http://plc4good.org.ua/files/03_downloads/SCL_table/SCL-cheat-sheet.pdf

我就開始..

  • 在SCL中創建功能塊。
  • 添加一個輸入屬性作爲字符串
  • 添加兩個輸出屬性(TEMP1,TEMP2),爲實數或整數爲臨時字符串
  • 幾個靜態變量和文本的>真實轉換。

解析類似於以下(因爲我沒有我的TIA Portal這可能需要修改)代碼: 你的字符串「$ AOPT_TEMP1_20_TEMP2_21」 假設開始總是「$ AOPT_TEMP1_」(12個字符)

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); 

請記住,這是所有我的頭頂部,使用手冊,快速參考: https://cache.industry.siemens.com/dl/files/465/36932465/att_106119/v1/s71200_system_manual_en-US_en-US.pdf

有些事情可能需要調整。如果您不/不能使用SCL,則這些塊也存在於梯形圖中。如果你可以,只有在你收到緩衝區後,才能連接這個功能塊。