You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool-biz/nmodbus/handler/data.go

97 lines
2.1 KiB
Go

1 year ago
package handler
import "sync"
// DataMgr 数据管理器
1 year ago
type DataMgr struct {
Address uint8 // 主机地址
coils []byte // 线圈DO数字输出
discreteInputs []byte // 离散输入DI数字输入
holdingRegisters []uint16 // 保持寄存器AO模拟输出
inputRegisters []uint16 // 输入寄存器AI模拟输入
mu sync.RWMutex
}
func NewDataManager(address uint8) *DataMgr {
return &DataMgr{
Address: address,
coils: make([]byte, 65536),
discreteInputs: make([]byte, 65536),
holdingRegisters: make([]uint16, 65536),
inputRegisters: make([]uint16, 65536),
mu: sync.RWMutex{},
}
}
func (m *DataMgr) ReadCoils(start, end int) []byte {
m.mu.RLock()
defer m.mu.RUnlock()
return m.coils[start:end]
}
func (m *DataMgr) ReadDiscreteInputs(start, end int) []byte {
m.mu.RLock()
defer m.mu.RUnlock()
return m.discreteInputs[start:end]
}
func (m *DataMgr) ReadHoldingRegisters(start, end int) []uint16 {
m.mu.RLock()
defer m.mu.RUnlock()
return m.holdingRegisters[start:end]
}
func (m *DataMgr) ReadInputRegisters(start, end int) []uint16 {
m.mu.RLock()
defer m.mu.RUnlock()
return m.inputRegisters[start:end]
}
// WriteSingleCoil 写单个线圈数据
func (m *DataMgr) WriteSingleCoil(idx int, val byte) {
m.mu.Lock()
defer m.mu.Unlock()
m.coils[idx] = val
}
// WriteSingleHoldingRegister 写入单个保持寄存器
func (m *DataMgr) WriteSingleHoldingRegister(idx int, val uint16) {
m.mu.Lock()
defer m.mu.Unlock()
m.holdingRegisters[idx] = val
}
// WriteCoils 写多个线圈数据
// idxList 表示预写入的线圈地址列表
// values 表示预写入的线圈数据,与地址列表一一对应
func (m *DataMgr) WriteCoils(idxList []int, values []byte) {
m.mu.Lock()
defer m.mu.Unlock()
for i, idx := range idxList {
m.coils[idx] = values[i]
}
}
func (m *DataMgr) WriteHoldingRegisters(values []uint16, start int, end ...int) int {
m.mu.Lock()
defer m.mu.Unlock()
var e int
if len(end) == 0 {
e = len(m.holdingRegisters)
} else {
e = end[0]
}
return copy(m.holdingRegisters[start:e], values)
1 year ago
}