共计 3263 个字符,预计需要花费 9 分钟才能阅读完成。
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/v1 | |
kind: Deployment | |
metadata: | |
labels: | |
app: deviceshifu-plc-deployment | |
name: deviceshifu-plc-deployment | |
namespace: deviceshifu | |
spec: | |
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: v1 | |
kind: ConfigMap | |
metadata: | |
name: plc-configmap-0.0.1 | |
namespace: deviceshifu | |
data: | |
# 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: v1 | |
kind: Service | |
metadata: | |
labels: | |
app: deviceshifu-plc-deployment | |
name: deviceshifu-plc | |
namespace: deviceshifu | |
spec: | |
ports: | |
- port: 80 | |
protocol: TCP | |
targetPort: 8080 | |
selector: | |
app: deviceshifu-plc-deployment | |
type: LoadBalancer |
edgedevice-plc.yaml
apiVersion: shifu.edgenesis.io/v1alpha1 | |
kind: EdgeDevice | |
metadata: | |
name: edgedevice-plc | |
namespace: devices | |
spec: | |
sku: "PLC" | |
connection: Ethernet | |
address: 0.0.0.0:11111 | |
protocol: HTTP | |
status: | |
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 的指令,别离是sendsinglebit
、getcontent
、getcpuordercode
,咱们能够通过 Shifu
来对设施执行这些命令。
sendsinglebit
sendsinglebit
示意批改一个 bit,它须要下列参数:
rootaddress
: 内存区域名称,比方M
代表Merker
,Q
代表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
代表Merker
,Q
代表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