【EdgeX(16)】 :边缘计算,工业4.0,golang开发,使用Apache PLC4X项目对PLC设备进行modbus协议对接,实现读取和发送代码学习中
golang 感觉上要是使用其他语言也可以实现和plc 进行数据对接的。使用golang 进行开发的好处是可以方便、快速的进行数据上报,接口联调。做边缘计算,做云计算。打造工业化4.0服务。
前言
相关EdgeX Foundry 全部分类:
https://blog.csdn.net/freewebsys/category_9437788.html
本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/127585739
未经博主允许不得转载。
博主CSDN地址是:https://blog.csdn.net/freewebsys
博主掘金地址是:https://juejin.cn/user/585379920479288
博主知乎地址是:https://www.zhihu.com/people/freewebsystem
1,关于Apache PLC4X
目前手上没有PLC设备,只是进行学习。
PLC行业发展趋势
PLC软件的算法需要针对工业4.0的生产要求做出进一步优化,在结合人工智能和大数据的基础上,PLC行业应该进一步优化软件,提供更符合行业实际应用的工具,提升工业算法解决问题的能力并降低算法复杂程度。另外,PLC作为一种辅助工业制造的工具,底层程序应该进一步封装为简洁易用的工具包,对上提供简单接口供技术人员调用,实现复杂生产工艺的操作简洁化。在用户交互方面,PLC工具需要结合具体场景对技术人员的操作习惯进一步理解,并依据实际情况制定切合实际的用户交互界面,进一步增强系统的交互性能。
项目地址:
https://plc4x.apache.org/
https://github.com/apache/plc4x/
有golang 的代码:
https://plc4x.apache.org/users/getting-started/plc4go.html
https://github.com/apache/plc4x/tree/develop/plc4go
视频学习资料:
ApacheConAsia2021 IOT0808:Christofe - 使用Apache PLC4X的高安全性IIoT通信-英文
多角度讲解MODBUS通信在控制项目上的应用
多角度讲解MODBUS通信在控制项目上的应用
About Apache PLC4X
Apache PLC4X is an effort to create a set of libraries for communicating with industrial grade programmable logic controllers (PLCs) in a uniform way. We are planning on shipping libraries for usage in:
Java
Go
C (not ready for usage)
Python (not ready for usage)
C# (.Net) (not ready for usage)
支持多个语音,但是做IOT还是研究golang 版本的吧。方便部署开发。
可以支持的协议:
https://plc4x.apache.org/users/protocols/index.html
2,使用golang代码进行读写操作plc设备
读取plc设备的demo例子,官方的example里面的:
package main
import (
"fmt"
"github.com/apache/plc4x/plc4go/pkg/api"
"github.com/apache/plc4x/plc4go/pkg/api/drivers"
"github.com/apache/plc4x/plc4go/pkg/api/model"
)
func main() {
driverManager := plc4go.NewPlcDriverManager()
drivers.RegisterModbusTcpDriver(driverManager)
// Get a connection to a remote PLC
crc := driverManager.GetConnection("modbus-tcp://192.168.23.30")
// Wait for the driver to connect (or not)
connectionResult := <-crc
if connectionResult.GetErr() != nil {
fmt.Printf("error connecting to PLC: %s", connectionResult.GetErr().Error())
return
}
connection := connectionResult.GetConnection()
// Make sure the connection is closed at the end
defer connection.BlockingClose()
// Prepare a read-request
readRequest, err := connection.ReadRequestBuilder().
AddTagAddress("tag", "holding-register:26:REAL").
Build()
if err != nil {
fmt.Printf("error preparing read-request: %s", connectionResult.GetErr().Error())
return
}
// Execute a read-request
rrc := readRequest.Execute()
// Wait for the response to finish
rrr := <-rrc
if rrr.GetErr() != nil {
fmt.Printf("error executing read-request: %s", rrr.GetErr().Error())
return
}
// Do something with the response
if rrr.GetResponse().GetResponseCode("tag") != model.PlcResponseCode_OK {
fmt.Printf("error an non-ok return code: %s", rrr.GetResponse().GetResponseCode("tag").GetName())
return
}
value := rrr.GetResponse().GetValue("tag")
fmt.Printf("Got result %f", value.GetFloat32())
}
写入信息代码:
package main
import (
"fmt"
"github.com/apache/plc4x/plc4go/pkg/api"
"github.com/apache/plc4x/plc4go/pkg/api/drivers"
"github.com/apache/plc4x/plc4go/pkg/api/model"
)
func main() {
driverManager := plc4go.NewPlcDriverManager()
drivers.RegisterModbusTcpDriver(driverManager)
// Get a connection to a remote PLC
crc := driverManager.GetConnection("modbus-tcp://192.168.23.30")
// Wait for the driver to connect (or not)
connectionResult := <-crc
if connectionResult.GetErr() != nil {
fmt.Printf("error connecting to PLC: %s", connectionResult.GetErr().Error())
return
}
connection := connectionResult.GetConnection()
// Make sure the connection is closed at the end
defer connection.BlockingClose()
// Prepare a write-request
writeRequest, err := connection.WriteRequestBuilder().
AddTagAddress("tag", "holding-register:26:REAL", 2.7182818284).
Build()
if err != nil {
fmt.Printf("error preparing read-request: %s", connectionResult.GetErr().Error())
return
}
// Execute a read-request
wrc := writeRequest.Execute()
// Wait for the response to finish
wrr := <-wrc
if wrr.GetErr() != nil {
fmt.Printf("error executing write-request: %s", wrr.GetErr().Error())
return
}
if wrr.GetResponse().GetResponseCode("tag") != model.PlcResponseCode_OK {
fmt.Printf("error an non-ok return code: %s", wrr.GetResponse().GetResponseCode("tag").GetName())
return
}
fmt.Print("Result: SUCCESS\n")
}
支持的设备plc 设备协议:
ads bacnetip cbus eip knxnetip modbus s7 等驱动的实现,不太清楚具体含义。
可能是协议啥的吧。
代码中的使用的是 modbus2 的驱动去调用的。
感觉上应该和modbus 获取的数据类似。
依赖的其他golang的开源项目:
github.com/IBM/netaddr
github.com/ajankovic/xdiff
github.com/fatih/color
github.com/gdamore/tcell/v2
github.com/gopacket/gopacket
github.com/icza/bitio
github.com/jacobsa/go-serial
github.com/k0kubun/go-ansi
github.com/libp2p/go-reuseport
github.com/pkg/errors
github.com/rivo/tview
github.com/rs/zerolog
github.com/schollz/progressbar/v3
github.com/snksoft/crc
github.com/spf13/cobra
github.com/spf13/viper
github.com/stretchr/testify
github.com/subchen/go-xmldom
github.com/viney-shih/go-lock
golang.org/x/exp
golang.org/x/tools
gopkg.in/yaml.v3
go-serial
This is a package that allows you to read from and write to serial ports in Go.
GoPacket
This library provides packet decoding capabilities for Go.
google 开发packet 项目 fork到了
github.com/gopacket/gopacket 继续更新维护。
还有命令行的GUI项目:
https://github.com/rivo/tview
https://github.com/ajankovic/xdiff
X-Diff is an algorithm that proposes change detection between XML documents by treating them as unordered trees.
命令行使用:
xdiff -left original.xml -right edited.xml
https://github.com/schollz/progressbar
使用代码实现进度条。
https://github.com/spf13/cobra
一个基础的golang命令行项目:
给k8s , hugo 等项目引用。
3,总结
golang 感觉上要是使用其他语言也可以实现和plc 进行数据对接的。
使用golang 进行开发的好处是可以方便、快速的进行数据上报,接口联调。
做边缘计算,做云计算。打造工业化4.0服务。
本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/108971807
更多推荐
所有评论(0)