首页 文章

西门子PLC与Arduino之间的串行通信

提问于
浏览
1

我希望使用西门子S7-1200与CM 1241(RS-232)进行串行通信,并与我的Arduino进行通信 . 这是通信的设置 . 我有2个温度传感器和一个Led连接到我的Arduino,在PLC端我有西门子的S7-1200和CM-1241 . Arduino和我的PLC只是通过使用Tx和Rx引脚连接,没有完成握手 .

我将温度数据从传感器发送到PLC . 在PLC端,我根据不同的温度值决定何时打开连接到我的arduino的Led . 我在发送数据之前为两个传感器分配了一个ID . 这是来自Arduino的传输数据看起来像$ AOPT_TEMP1_20_TEMP2_21 .

到目前为止它很好,我使用RCV_PTP(接收数据放在缓冲区上)并使用SEND_PTP发送数据在PLC上接收串行数据 . 我还在PLC上实现了一个过滤器,它只接受以'$ AOPT_'开头的串行数据 . 现在,我想从两个温度传感器TEMP1和TEMP2接收温度值,然后控制LED . 例如,如果(TEMP1> TEMP2)然后打开,则Led else关闭 .

我能够从Arduino接收PLC上的数据,但现在我不知道如何继续比较收到的信息 . 如何从接收的缓冲区中提取唯一所需的数据?任何建议将受到高度赞赏 .

提前致谢....

1 回答

  • 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)添加为Reals或Ints

    • 临时字符串和文本 - >实际转换的几个静态变量 .

    解析你的代码类似于以下(因为我没有我的TIA门户,这可能需要修改):对于你的字符串“$ 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

    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 .

相关问题