Golang使用Apache PLC4X連接modbus的示例代碼
什么是Modbus
Modbus是一種串行通信協(xié)議,是Modicon公司于1979年為使用可編程邏輯控制器(PLC)通信而發(fā)表。Modbus是工業(yè)領域通信協(xié)議的業(yè)界標準,是工業(yè)電子設備之間常用的連接方式Modbus就是一個總線通信協(xié)議,像IIC SPI這種,但是他不依賴于硬件總線
- Modbus之所以使用廣泛,是有他的優(yōu)點的
- Modbus協(xié)議標準開放、公開發(fā)表且無版權要求
- Modbus協(xié)議支持多種電氣接口,包括RS232、RS485、TCP/IP等,還可以在各種介質上傳輸,如雙絞線、光纖、紅外、無線等
- Modbus協(xié)議消息幀格式簡單、緊湊、通俗易懂。用戶理解和使用簡單,廠商容易開發(fā)和集成,方便形成工業(yè)控制網絡
Golang使用Apache PLC4X連接modbus
Apache PLC4X 是一個工業(yè)物聯(lián)網通用協(xié)議適配器,PLC4X 是一組庫,用于使用各種協(xié)議與工業(yè)可編程邏輯控制器 (PLC) 進行通信,但這些協(xié)議具有共享的 API。
工業(yè)的可編程邏輯控制器(PLC)大多采用大量不兼容的協(xié)議進行通信,因此和外界(IT/互聯(lián)網)的交互變得很困難。PLC4X 項目致力于提供一組統(tǒng)一的 API,通過這些 API 實現(xiàn)能與大多數(shù) PLC 進行通信的驅動程序(通過各種 PLC 的原生通信協(xié)議)。
官方地址: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內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Go?基本數(shù)據(jù)類型與字符串相互轉換方法小結
這篇文章主要介紹了Go基本數(shù)據(jù)類型與字符串相互轉換,將string類型轉換成基本類型時,必須確保string類型是有效的,文中補充介紹了Go基本數(shù)據(jù)類型和其字符串表示之間轉換,結合實例代碼給大家講解的非常詳細,需要的朋友可以參考下2024-01-01