modbus

package
v0.0.0-...-bc3b7f3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

基础Modbus数据模型

基础Modbus数据模型之上增加Modbus数据协议解析的方法

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandType

type CommandType uint8
const (
	ReadCoilStatus          CommandType = 1 //00001
	ReadInputStatus         CommandType = 2 //10001
	ReadHoldingRegisters    CommandType = 3 //40001
	ReadInputRegisters      CommandType = 4 //30001
	ForceSingleCoil         CommandType = 5
	PresetSingleRegister    CommandType = 6
	ForceMultipleCoils      CommandType = 15
	PresetMultipleRegisters CommandType = 16
)

type DirectionType

type DirectionType uint8
const (
	Send    DirectionType = 0
	Receive DirectionType = 1
)

type FrameListener

type FrameListener struct {
	// contains filtered or unexported fields
}

发送接收监听结构基类,实现IModbusTask中两个监听接口函数。应用时将本类作为基类继承即可

func (*FrameListener) GetReceiverEvent

func (_this *FrameListener) GetReceiverEvent() *events.EventParam[[]byte]

func (*FrameListener) GetSenderEvent

func (_this *FrameListener) GetSenderEvent() *events.EventParam[[]byte]

type IModbusAction

type IModbusAction interface {
	//提供发送数据的端口与方案
	SetWriter(writer *func([]byte) error)
	//提供读取数据的端口与方案
	SetReader(reader *func() ([]byte, error))
	//读一次数据
	ReadOnce(addr, command uint8, regNo uint16, regLength uint8) error
	//写一次数据
	WriteOnce(addr, command uint8, regNo uint16, buffer []uint16) error
	//获取发送帧
	GetSendFrame() *ModbusProtocol
	//获取接收帧
	GetReceiveFrame() *ModbusProtocol
}

基于不同的数据流方式,为Modbus提供发送与接收时的特殊处理方案

func CreateModbusAction

func CreateModbusAction() IModbusAction

type IModbusTask

type IModbusTask interface {
	//返回值:发送帧数据结构,返回帧数据结构
	ReadOnce(adress, command uint8, startReg uint16, regLen uint8, datas []uint16) (*ModbusModel, *ModbusModel, error)
	//获取接受监听事件,以注册
	GetReceiverEvent() *events.EventParam[[]byte]
	//获取发送监听事件,以注册
	GetSenderEvent() *events.EventParam[[]byte]
}

为具体协议制作的接口,提供给三个具体独写协议使用

func CreateModbusRTU

func CreateModbusRTU(comName string, baudRate, dataBits, stopBits, parity, timeOutMilsecond int, autoReconnect bool) (IModbusTask, error)

func CreateModbusRTUOverTCP

func CreateModbusRTUOverTCP(ip string, port, timeOutMilsecond int, autoReconnect bool) (IModbusTask, error)

func CreateModbusTCP

func CreateModbusTCP(ip string, port, timeOutMilsecond int, autoReconnect bool) (IModbusTask, error)

type ModbusAction

type ModbusAction struct {
	// contains filtered or unexported fields
}

基于Modbus数据协议解析的方法,组建读写对象。继承IModbusAction

func (*ModbusAction) GetReceiveFrame

func (_this *ModbusAction) GetReceiveFrame() *ModbusProtocol

func (*ModbusAction) GetSendFrame

func (_this *ModbusAction) GetSendFrame() *ModbusProtocol

func (*ModbusAction) ReadOnce

func (_this *ModbusAction) ReadOnce(addr, command uint8, regNo uint16, regLength uint8) error

func (*ModbusAction) SetReader

func (_this *ModbusAction) SetReader(reader *func() ([]byte, error))

func (*ModbusAction) SetWriter

func (_this *ModbusAction) SetWriter(writer *func([]byte) error)

func (*ModbusAction) WriteOnce

func (_this *ModbusAction) WriteOnce(addr, command uint8, regNo uint16, buffer []uint16) error

type ModbusModel

type ModbusModel struct {
	//Direction    DirectionType `json:"direction"`    //: "send",Y
	Command   CommandType `json:"command"`   //: "01",Y
	Addr      byte        `json:"addr"`      //: "03",Y
	RegNo     uint16      `json:"regNo"`     //: "00 00",
	RegLength uint8       `json:"regLength"` //: "00 0a",
	BufferLen uint8       `json:"bufferLen"` //: "0a",
	Body      []uint16    `json:"body"`      //: "",

}
func (_this *ModbusTCPHeader) ToString() string {
	return ByteArrayToString(_this.ToArray())
	//return fmt.Sprintf("%2X-%2X-%4X-%4X", _this.TxNo, _this.RxNo, _this.ProtocolType, _this.Length)
}

App struct

type ModbusProtocol

type ModbusProtocol struct {
	ModbusModel
	// contains filtered or unexported fields
}

func (*ModbusProtocol) AnalyzeBytes

func (_this *ModbusProtocol) AnalyzeBytes(data []byte, direction DirectionType) error

func (*ModbusProtocol) AnalyzeStruct

func (_this *ModbusProtocol) AnalyzeStruct() ([]byte, error)

func (*ModbusProtocol) GetTime

func (_this *ModbusProtocol) GetTime() time.Time

type ModbusRTU

type ModbusRTU struct {
	FrameListener
	// contains filtered or unexported fields
}

func (*ModbusRTU) ReadOnce

func (_this *ModbusRTU) ReadOnce(adress, command uint8, startReg uint16, regLen uint8, datas []uint16) (*ModbusModel, *ModbusModel, error)

type ModbusRTUOverTCP

type ModbusRTUOverTCP struct {
	FrameListener
	// contains filtered or unexported fields
}

func (*ModbusRTUOverTCP) ReadOnce

func (_this *ModbusRTUOverTCP) ReadOnce(adress, command uint8, startReg uint16, regLen uint8, datas []uint16) (*ModbusModel, *ModbusModel, error)

type ModbusTCP

type ModbusTCP struct {
	FrameListener
	SenderHeader  ModbusTCPHeader
	ReceiveHeader ModbusTCPHeader
	// contains filtered or unexported fields
}

func (*ModbusTCP) ReadOnce

func (_this *ModbusTCP) ReadOnce(adress, command uint8, startReg uint16, regLen uint8, datas []uint16) (*ModbusModel, *ModbusModel, error)

type ModbusTCPHeader

type ModbusTCPHeader struct {
	TxNo         uint16 `json:"txNo"`
	RxNo         uint16 `json:"rxNo"`
	ProtocolType uint16 `json:"ProtocolType"` //ModbusTCP固定为0000
	Length       uint16 `json:"length"`
}

func (*ModbusTCPHeader) FromArray

func (_this *ModbusTCPHeader) FromArray(buffer []byte) error

func (*ModbusTCPHeader) ToArray

func (_this *ModbusTCPHeader) ToArray() []byte

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL