当前位置: 代码网 > it编程>前端脚本>Golang > Golang使用Apache PLC4X连接modbus的示例代码

Golang使用Apache PLC4X连接modbus的示例代码

2024年09月06日 Golang 我要评论
什么是modbusmodbus是一种串行通信协议,是modicon公司于1979年为使用可编程逻辑控制器(plc)通信而发表。modbus是工业领域通信协议的业界标准,是工业电子设备之间常用的连接方式

什么是modbus

modbus是一种串行通信协议,是modicon公司于1979年为使用可编程逻辑控制器(plc)通信而发表。modbus是工业领域通信协议的业界标准,是工业电子设备之间常用的连接方式modbus就是一个总线通信协议,像iic spi这种,但是他不依赖于硬件总线

  • modbus之所以使用广泛,是有他的优点的
  • modbus协议标准开放、公开发表且无版权要求
  • modbus协议支持多种电气接口,包括rs232、rs485、tcp/ip等,还可以在各种介质上传输,如双绞线、光纤、红外、无线等
  • modbus协议消息帧格式简单、紧凑、通俗易懂。用户理解和使用简单,厂商容易开发和集成,方便形成工业控制网络

golang使用apache plc4x连接modbus

apache plc4x 是一个工业物联网通用协议适配器,plc4x 是一组库,用于使用各种协议与工业可编程逻辑控制器 (plc) 进行通信,但这些协议具有共享的 api。

工业的可编程逻辑控制器(plc)大多采用大量不兼容的协议进行通信,因此和外界(it/互联网)的交互变得很困难。plc4x 项目致力于提供一组统一的 api,通过这些 api 实现能与大多数 plc 进行通信的驱动程序(通过各种 plc 的原生通信协议)。

官方地址:https://plc4x.apache.org/users/getting-started/plc4go.html

golang代码连接modbus

package main
import (
	"fmt"
	"github.com/apache/plc4x/plc4go/pkg/plc4go"
	"github.com/apache/plc4x/plc4go/pkg/plc4go/drivers"
	"github.com/apache/plc4x/plc4go/pkg/plc4go/transports"
)
//目前 没看到有opcua驱动
func main()  {
	// create a new instance of the plcdrivermanager
	drivermanager := plc4go.newplcdrivermanager()
	// register the transports
	transports.registertcptransport(drivermanager)
	transports.registerudptransport(drivermanager)
	// register the drivers
	drivers.registerknxdriver(drivermanager)
	drivers.registermodbusdriver(drivermanager)
	// get a connection to a remote plc
	connectionrequestchanel := drivermanager.getconnection("modbus://192.168.23.30?unit-identifier=1")
	// wait for the driver to connect (or not)
	connectionresult := <-connectionrequestchanel
	// check if something went wrong
	if connectionresult.err != nil {
		fmt.printf("error connecting to plc: %s", connectionresult.err.error())
		return
	}
	// if all was ok, get the connection instance
	connection := connectionresult.connection
	// make sure the connection is closed at the end
	defer connection.close()
}

到此这篇关于golang使用apache plc4x连接modbus的示例代码的文章就介绍到这了,更多相关golang连接modbus内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com