Dmitry Telenkov 2 days ago
parent
commit
155dfcd556
4 changed files with 45 additions and 0 deletions
  1. 5 0
      go/modbus/go.mod
  2. 4 0
      go/modbus/go.sum
  3. 35 0
      go/modbus/internal/client.go
  4. 1 0
      go/modbus/main.go

+ 5 - 0
go/modbus/go.mod

@@ -1,3 +1,8 @@
 module modbus
 module modbus
 
 
 go 1.24.1
 go 1.24.1
+
+require (
+	github.com/goburrow/serial v0.1.0 // indirect
+	github.com/simonvetter/modbus v1.6.4 // indirect
+)

+ 4 - 0
go/modbus/go.sum

@@ -0,0 +1,4 @@
+github.com/goburrow/serial v0.1.0 h1:v2T1SQa/dlUqQiYIT8+Cu7YolfqAi3K96UmhwYyuSrA=
+github.com/goburrow/serial v0.1.0/go.mod h1:sAiqG0nRVswsm1C97xsttiYCzSLBmUZ/VSlVLZJ8haA=
+github.com/simonvetter/modbus v1.6.4 h1:E03lBz/JftDza/+Ue+vxwkNZ/WW1xiqyFCUQ4NhqHn0=
+github.com/simonvetter/modbus v1.6.4/go.mod h1:hh90ZaTaPLcK2REj6/fpTbiV0J6S7GWmd8q+GVRObPw=

+ 35 - 0
go/modbus/internal/client.go

@@ -2,8 +2,43 @@ package client
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"time"
+
+	"github.com/simonvetter/modbus"
 )
 )
 
 
+func InitClient(port string, speed int) {
+	var client *modbus.ModbusClient
+	var err error
+
+	client, err = modbus.NewClient(&modbus.ClientConfiguration{
+		URL:      "rtu:///dev/ttyUSB0",
+		Speed:    115200,
+		DataBits: 8,
+		Parity:   modbus.PARITY_NONE,
+		StopBits: 1,
+		Timeout:  300 * time.Millisecond,
+	})
+
+	client.SetUnitId(1)
+
+	// modbus.
+
+	// fmt.Println(client.)
+
+	if err != nil {
+		fmt.Errorf("Something went wrong!")
+	}
+
+	err = client.Open()
+	defer client.Close()
+
+	var reg16 uint16
+	reg16, err = client.ReadRegister(50, modbus.HOLDING_REGISTER)
+
+	fmt.Printf("value: %v", reg16)
+}
+
 func Hello() {
 func Hello() {
 	fmt.Println("Hello from glient.go")
 	fmt.Println("Hello from glient.go")
 }
 }

+ 1 - 0
go/modbus/main.go

@@ -8,4 +8,5 @@ import (
 func main() {
 func main() {
 	fmt.Println("Hello from main!")
 	fmt.Println("Hello from main!")
 	client.Hello()
 	client.Hello()
+	client.InitClient("rtu:///dev/ttyUSB0", 115200)
 }
 }