关于程序员:用户手册接入设备之使用-PLC4x-连接-西门子S7-PLC

30次阅读

共计 2761 个字符,预计需要花费 7 分钟才能阅读完成。

Shifu还能够应用 PLC4X 的模式实现对 西门子 S7 系列 PLC 兼容。本文将介绍如何应用deviceshifu-plc4x-http 接入一台 西门子 S7-1200 1214C PLC并且与之交互。

连贯

第 1 步

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

提醒

如果您的 PLC 设施不为 192.168.0.1 能够将 edgedevice-plc4x.yaml 文件中的 address 改成您的设施的 IP

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

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

deviceshifu-plc4x-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: deviceshifu-plc4x-deployment
  name: deviceshifu-plc4x-deployment
  namespace: deviceshifu
spec:
  replicas: 1
  selector:
    matchLabels:
      app: deviceshifu-plc4x-deployment
  template:
    metadata:
      labels:
        app: deviceshifu-plc4x-deployment
    spec:
      containers:
      - image: edgehub/deviceshifu-http-plc4x:v0.1.1
        name: deviceshifu-http
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: deviceshifu-config
          mountPath: "/etc/edgedevice/config"
          readOnly: true
        env:
        - name: EDGEDEVICE_NAME
          value: "edgedevice-plc4x"
        - name: EDGEDEVICE_NAMESPACE
          value: "devices"
      volumes:
      - name: deviceshifu-config
        configMap:
          name: plc4x-configmap
      serviceAccountName: edgedevice-sa

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

deviceshifu-plcs4x-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: plc4x-configmap
  namespace: deviceshifu
data:
  driverProperties: |
    driverSku: testPlc4x
    driverImage: 
  instructions: |
    instructions:
  telemetries: |
    telemetrySettings:

deviceshifu-plc4x-service.yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    app: deviceshifu-plc4x-deployment
  name: deviceshifu-plc4x
  namespace: deviceshifu
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: deviceshifu-plc4x-deployment
  type: LoadBalancer

edgedevice-plc4x.yaml

apiVersion: shifu.edgenesis.io/v1alpha1
kind: EdgeDevice
metadata:
  name: edgedevice-plc4x
  namespace: devices
spec:
  sku: "testPlc4x" 
  connection: Ethernet
  address: 192.168.0.1 #change this accordingly
  protocol: PLC4X
  protocolSettings:
    PLC4XSetting:
      protocol: s7

第 3 步

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

kubectl apply -f ../plc4x_configuration_directory

操作

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

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

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

deviceshifu-plc4x-http内置了两个命令 readwrite,咱们能够通过 shifu 应用这两个命令来对设施进行读写操作。

提醒

本文中的命令 %Q0.0:BOOL 为 PLC4X 的命令其中 Q 为内存区域名称,0.0示意起始 bit 的地址及其偏移量。BOOL为读取 (写入) 的数据类型。

如果您想理解更多对于 PLC4X 的命令,请返回 PLC4X 官网。

read

read示意应用对应命令读取设施的值:

比方,命令 curl "deviceshifu-plc4x/read?%Q0.0:BOOL" 会返回内存区域 Q0 的地址内容。

curl "deviceshifu-plc4x/read?%Q0.0:BOOL"; echo
{"field_%Q0.0:BOOL":"BOOL(1bit):false"}

此时 Shifu 返回 Q 区的从左往右第一个指示灯的状态。如果该指示灯为亮的则返回 true,否则返回 false。因为以后为燃烧状态,返回值为 false。

write

write示意通过命令批改对应地位的值:

比方,命令 curl "deviceshifu-plc4x/read?%Q0.0:BOOL=true" 会将 Q0 的第一个 bit 批改为 true。

curl "deviceshifu-plc4x/read?%Q0.0:BOOL=true"; echo

更多

如果您须要同时读取或者写入多个命令,您能够应用 & 进行连贯应用。

比方,命令 curl "deviceshifu-plc4x/read?%Q0.0:BOOL&%Q0.1:BOOL" 会同时将 Q0 的第一个 bit 和 第二个 bit 的值返回。

命令 curl "deviceshifu-plc4x/write?%Q0.0:BOOL=true&%Q0.1:BOOL=true" 会同时将 Q0 的第一个 bit 和 第二个 bit 批改为 true。

本文由边无际受权公布

正文完
 0