Skip to content
This repository has been archived by the owner on Mar 4, 2023. It is now read-only.

The port is not being closed #21

Open
Zerg17 opened this issue Nov 21, 2022 · 1 comment
Open

The port is not being closed #21

Zerg17 opened this issue Nov 21, 2022 · 1 comment

Comments

@Zerg17
Copy link

Zerg17 commented Nov 21, 2022

Code:

package main

import (
	"fmt"

	"github.com/goburrow/serial"
	modbus "github.com/things-go/go-modbus"
)

func main() {
	p := modbus.NewRTUClientProvider(modbus.WithEnableLogger(),
	modbus.WithSerialConfig(serial.Config{
		Address:  "\\\\.\\com31",
		BaudRate: 38400,
		DataBits: 8,
		StopBits: 1,
		Parity:   "N",
		Timeout:  modbus.SerialDefaultTimeout,
	}))

	client := modbus.NewClient(p)
	err := client.Connect()
	if err != nil {
		fmt.Println("connect failed, ", err)
		return
	}

	_, err = client.ReadInputRegisters(0x16, 69, 13)
	if err != nil {
		fmt.Println(err.Error())
	}

	fmt.Println("Close err:", client.Close())

	_, err = client.ReadInputRegisters(0x16, 69, 13)
	if err != nil {
		fmt.Println(err.Error())
	}
}

Out:

modbusRTUMaster => 2022/11/21 12:42:20 [D]: sending [16 04 00 45 00 0d 23 3d]
modbusRTUMaster => 2022/11/21 12:42:20 [D]: received [16 04 1a 00 00 00 00 00 01 00 00 80 00 00 03 83 c9 7c e8 20 22 11 15 00 00 80 01 00 05 ea 2d]
Close err: <nil>
modbusRTUMaster => 2022/11/21 12:42:20 [D]: sending [16 04 00 45 00 0d 23 3d]
modbusRTUMaster => 2022/11/21 12:42:20 [D]: received [16 04 1a 00 00 00 00 00 01 00 00 80 00 00 03 83 c9 7c e8 20 22 11 15 00 00 80 01 00 05 ea 2d]

After the client.Close() the port remains open and continues to work

@thinkgos
Copy link
Member

auto reconnect when have a action after closed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants