EdgeX设备驱动代码分析device-modbus-go

1. config.go

创建连接信息结构体

type ConnectionInfo struct {
   Protocol string
   Address  string
   Port     int
   BaudRate int
   DataBits int
   StopBits int
   Parity   string
   UnitID   uint8
   // Connect & Read timeout(seconds)
   Timeout int
   // Idle timeout(seconds) to close the connection
   IdleTimeout int
}
func createConnectionInfo()    //创建连接信息结构体
func parseIntValue()           //检查string是否存在并转换为int
func createRTUConnectionInfo() //创建RTU连接
func createTCPConnectionInfo() //创建TCP连接

2. constant.go

全局常量定义

3. deviceclient.go

DeviceClient接口,指令信息结构体

func createCommandInfo()                //创建指令信息结构体
func calculateAddressLength()           //计算数据地址长度,valueType/RawType
func TransformDataBytesToResult()       //转换设备发送的二进制数据到valueType
func TransformCommandValueToDataBytes() //利用modbus协议将中数据转换为二进制数据
func calculateByteCount()               //计算数据Byte数
func getBinaryData()                    //获取二进制数据

4. driver.go

type Driver struct {
   Logger              logger.LoggingClient
   AsyncCh             chan<- *sdkModel.AsyncValues
   mutex               sync.Mutex
   addressMap          map[string]chan bool
   workingAddressCount map[string]int
   stopped             bool
}
func (d *Driver) lockAddress()             //lockAddress标记地址不可用,因为实际设备一次处理一个请求
func (d *Driver) unlockAddress()           //在命令完成后解除锁定地址
func (d *Driver) lockableAddress()         //lockableAddress根据协议返回可锁定地址
func (d *Driver) HandleReadCommands()      //处理读命令
func handleReadCommandRequest()            //处理读命令请求
func (d *Driver) HandleWriteCommands()     //处理写命令
func handleWriteCommandRequest()           //处理写命令请求
func (d *Driver) Stop()                    //关闭正在使用的通道
func (d *Driver) waitAllCommandsToFinish() //检查并等待未完成的job
func (d *Driver) AddDevice()
func (d *Driver) UpdateDevice()
func (d *Driver) RemoveDevice()
func NewProtocolDriver()

5. modbusclient.go

与底层modbus驱动交互

//ModbusClient用于连接设备和读写值
type ModbusClient struct {
   // IsModbusTcp是一个指示连接类型的值
   IsModbusTcp bool
   // TCPClientHandler用于保持TCP设备连接
   TCPClientHandler MODBUS.TCPClientHandler
   // TCPClientHandler用于保持RTU设备连接
   RTUClientHandler MODBUS.RTUClientHandler

   client MODBUS.Client
}
func (c *ModbusClient) OpenConnection()  //打开设备连接
func (c *ModbusClient) CloseConnection() //关闭设备连接
func (c *ModbusClient) GetValue()        //读数据
func (c *ModbusClient) SetValue()        //写数据
func NewDeviceClient()                   //新建modbus客户端

6. protocolpropertykey.go

协议属性关键字,常量

const (
   ProtocolTCP = "modbus-tcp"
   ProtocolRTU = "modbus-rtu"

   Address  = "Address"
   Port     = "Port"
   UnitID   = "UnitID"
   BaudRate = "BaudRate"
   DataBits = "DataBits"
   StopBits = "StopBits"
   // Parity: N - None, O - Odd, E - Even
   Parity = "Parity"

   Timeout     = "Timeout"
   IdleTimeout = "IdleTimeout"
)

7. swap.go

//字节交换与字交换
func swap32BitDataBytes(dataBytes []byte, isByteSwap bool, isWordSwap bool){
	if isByteSwap {
		newDataBytes[0] = dataBytes[1]
		newDataBytes[1] = dataBytes[0]
		newDataBytes[2] = dataBytes[3]
		newDataBytes[3] = dataBytes[2]
		copy(dataBytes, newDataBytes)
	}
	if isWordSwap {
		newDataBytes[0] = dataBytes[2]
		newDataBytes[1] = dataBytes[3]
		newDataBytes[2] = dataBytes[0]
		newDataBytes[3] = dataBytes[1]
	}
}

8. utils.go

工具包

func castStartingAddress() //强制转换起始地址
func normalizeRawType()    //规范化原始数据类型
func castSwapAttribute()   //强制转换交换属性                          
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值