Shifu 实现了对 西门子S7系列 PLC的兼容。用户能够应用 Shifu,通过 HTTP申请S7 PLC 的内存进行批改。本文将介绍如何接入一台 西门子S7-1200 1214C PLC 并且与之交互。

参见演示视频 (Bilibili)以取得操作流程演示。

连贯

若还未装置 Shifu ,请点击 这里 进行装置。

第1步

在接入 Shifu 之前,PLC该当曾经通过以太网与运行 Shifu 的上位机实现物理连贯,并且领有一个IP地址,这里咱们应用192.168.0.1

提醒

如果您的PLC设施不为192.168.0.1能够将deviceshifu-plc-deployment.yaml文件中的PLC_ADDRESS改成您的设施的IP)

第2步

创立一个文件夹,在示例中咱们将其命名为plc_configuration_directory。将下述的四个配置文件都保留在该文件夹下 。

首先咱们须要一个配置文件来获取IP地址与设施类型:

deviceshifu-plc-deployment.yaml

apiVersion: apps/v1kind: Deploymentmetadata:  labels:    app: deviceshifu-plc-deployment  name: deviceshifu-plc-deployment  namespace: deviceshifuspec:  replicas: 1  selector:    matchLabels:      app: deviceshifu-plc-deployment  template:    metadata:      labels:        app: deviceshifu-plc-deployment    spec:      containers:        - image: edgehub/deviceshifu-http-http:v0.0.1          name: deviceshifu-http          ports:            - containerPort: 8080          volumeMounts:            - name: deviceshifu-config              mountPath: "/etc/edgedevice/config"              readOnly: true          env:            - name: EDGEDEVICE_NAME              value: "edgedevice-plc"            - name: EDGEDEVICE_NAMESPACE              value: "devices"        - image: edgehub/plc-device:v0.0.1          name: plc          env:            - name: PLC_ADDRESS              value: "192.168.0.1"            - name: PLC_RACK              value: "0"                    - name: PLC_SLOT              value: "1"            - name: PLC_CONTAINER_PORT              value: "11111"      volumes:        - name: deviceshifu-config          configMap:            name: plc-configmap-0.0.1      serviceAccountName: edgedevice-sa

同时,还须要一些通用的配置文件:

deviceshifu-plc-configmap.yaml

apiVersion: v1kind: ConfigMapmetadata:  name: plc-configmap-0.0.1  namespace: deviceshifudata:#    device name and image address  driverProperties: |    driverSku: PLC    driverImage: plc-device:v0.0.1    driverExecution: " "#    available instructions  instructions: |    sendsinglebit:    sendcontent:    getcontent:    getcpuordercode:#    telemetry retrieval methods#    in this example, a device_health telemetry is collected by calling hello instruction every 1 second  telemetries: |    device_health:      properties:        instruction: getcpuordercode        initialDelayMs: 1000        intervalMs: 1000

deviceshifu-plc-service.yaml

apiVersion: v1kind: Servicemetadata:  labels:    app: deviceshifu-plc-deployment  name: deviceshifu-plc  namespace: deviceshifuspec:  ports:    - port: 80      protocol: TCP      targetPort: 8080  selector:    app: deviceshifu-plc-deployment  type: LoadBalancer

edgedevice-plc.yaml

    apiVersion: shifu.edgenesis.io/v1alpha1kind: EdgeDevicemetadata:  name: edgedevice-plc  namespace: devicesspec:  sku: "PLC"  connection: Ethernet  address: 0.0.0.0:11111  protocol: HTTPstatus:  edgedevicephase: "Pending"

第3步

Shifu 增加PLC设施,创立和启动 deviceShifu:

kubectl apply -f ../plc_configuration_directory

操作

对于PLC,Shifu 能够通过HTTP申请来读取和写入其内存。

在执行操作之前,咱们须要启动一个nginx容器,以用于HTTP申请的收发,启动的相干的命令如下:

kubectl run nginx --image=nginx:1.21 -n deviceshifu kubectl exec -it nginx -n deviceshifu -- bash

上面列举了3个PLC的指令,别离是sendsinglebitgetcontentgetcpuordercode,咱们能够通过 Shifu 来对设施执行这些命令。

sendsinglebit

sendsinglebit 示意批改一个bit,它须要下列参数:

  • rootaddress: 内存区域名称,比方M代表MerkerQ代表Digital Output
  • address: 内存区域中的地址。
  • start: 开始地位。
  • digit: 从开始地位起第几个bit。
  • value: 须要批改成为的数值。

比方,命令curl "deviceshifu-plc/sendsinglebit?rootaddress=Q&address=0&start=0&digit=1&value=1" 会将 Q0.1 的第二个 bit 批改为1。

curl "deviceshifu-plc/sendsinglebit?rootaddress=Q&address=0&start=0&digit=1&value=1"; echo

察看PLC咱们会发现其Q区的从左往右第二个指示灯变亮。

getcontent

getcontent 示意失去特定内存区域中地址的值,它须要下列参数:

  • rootaddress: 内存区域名称,比方M代表MerkerQ代表Digital Output
  • address: 内存区域中的地址。
  • start: 开始地位。

比方,命令 curl "deviceshifu-plc/getcontent?rootaddress=Q&address=0&start=0" 会返回内存区域 Q0.0 的地址内容。

curl "deviceshifu-plc/getcontent?rootaddress=Q&address=0&start=0"

getcpuordercode

getcpuordercode 示意失去PLC的动态信息。

curl "deviceshifu-plc/getcpuordercode"; echo