Files
Desktop_InfraredTransmission/RCU-10/RCU/FrmC43Function.vb

600 lines
20 KiB
VB.net
Raw Normal View History

2025-12-11 10:59:57 +08:00
Imports System.IO
Imports System.Threading
Public Class FrmC43Function
Implements IModuleForm
#Region "窗体"
''' <summary>
''' 窗体加载函数
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub FrmC43Function_Load(sender As Object, e As EventArgs) Handles Me.Load
C43ConfigData() 'C43配置
End Sub
''' <summary>
''' 显示窗体接口
''' </summary>
''' <param name="parentControl"></param>
Public Sub ShowForm(parentControl As Control) Implements IModuleForm.ShowForm
FormBorderStyle = FormBorderStyle.None '无边框
TopLevel = False
Dock = DockStyle.Fill '填满大小
Parent = parentControl '父容器
Show()
End Sub
''' <summary>
''' 发送函数
''' </summary>
''' <param name="sendPacket"></param>
Public Sub SendData(sendPacket() As Byte)
FrmRCU.SendData(sendPacket) '方法1
End Sub
#End Region
#Region "C43功能配置"
''' <summary>
''' 枚举表格列名信息
''' </summary>
Enum EnListed
SerialNumber '序号
IsSelected '是否选中
ButtonName '按键号
ButtonType '按键类型
DeviceType '设备类型
DeviceNumber '设备编号
CarryWayL '执行方式L
CarryWayH '执行方式H
SceneControlWay '场景执行方式
DelayToPerform '延时执行
Remark '备注
End Enum
''' <summary>
''' C43配置数据
''' </summary>
Private Sub C43ConfigData()
GrdC43cfg.DisplayRowNumber = True
GrdC43cfg.ExtendLastCol = True
GrdC43cfg.Cols = 11
GrdC43cfg.Rows = 2
With GrdC43cfg.Cell(0, EnListed.IsSelected)
.Text = "是否选中"
.BackColor = Color.LightGray
.ForeColor = Color.Blue
.Font = New Font("Arial", 8, FontStyle.Bold)
End With
With GrdC43cfg.Cell(0, EnListed.ButtonName)
.Text = "按键号"
.BackColor = Color.LightGray
.ForeColor = Color.Blue
.Font = New Font("Arial", 8, FontStyle.Bold)
End With
With GrdC43cfg.Cell(0, EnListed.ButtonType)
.Text = "按键类型"
.BackColor = Color.LightGray
.ForeColor = Color.Blue
.Font = New Font("Arial", 8, FontStyle.Bold)
End With
With GrdC43cfg.Cell(0, EnListed.DeviceType)
.Text = "设备类型"
.BackColor = Color.LightGray
.ForeColor = Color.Blue
.Font = New Font("Arial", 8, FontStyle.Bold)
End With
With GrdC43cfg.Cell(0, EnListed.DeviceNumber)
.Text = "设备编号"
.BackColor = Color.LightGray
.ForeColor = Color.Blue
.Font = New Font("Arial", 8, FontStyle.Bold)
End With
With GrdC43cfg.Cell(0, EnListed.CarryWayL)
.Text = "执行方式"
.BackColor = Color.LightGray
.ForeColor = Color.Blue
.Font = New Font("Arial", 8, FontStyle.Bold)
End With
With GrdC43cfg.Cell(0, EnListed.CarryWayH)
.Text = "执行内容"
.BackColor = Color.LightGray
.ForeColor = Color.Blue
.Font = New Font("Arial", 8, FontStyle.Bold)
End With
With GrdC43cfg.Cell(0, EnListed.SceneControlWay)
.Text = "场景控制方式"
.BackColor = Color.LightGray
.ForeColor = Color.Blue
.Font = New Font("Arial", 8, FontStyle.Bold)
End With
With GrdC43cfg.Cell(0, EnListed.DelayToPerform)
.Text = "延时执行"
.BackColor = Color.LightGray
.ForeColor = Color.Blue
.Font = New Font("Arial", 8, FontStyle.Bold)
End With
With GrdC43cfg.Cell(0, EnListed.Remark)
.Text = "备注"
.BackColor = Color.LightGray
.ForeColor = Color.Blue
.Font = New Font("Arial", 8, FontStyle.Bold)
End With
'是否选中
GrdC43cfg.Column(EnListed.IsSelected).CellType = FlexCell.CellTypeEnum.CheckBox
GrdC43cfg.Cell(1, EnListed.IsSelected).Text = $"1"
'按键号
GrdC43cfg.Column(EnListed.ButtonName).Alignment = FlexCell.AlignmentEnum.CenterCenter
GrdC43cfg.Column(EnListed.ButtonName).Mask = FlexCell.MaskEnum.Numeric
GrdC43cfg.Column(EnListed.ButtonName).DecimalLength = 0
'按键类型
GrdC43cfg.Column(EnListed.ButtonType).Alignment = FlexCell.AlignmentEnum.CenterCenter
GrdC43cfg.Column(EnListed.ButtonType).CellType = FlexCell.CellTypeEnum.ComboBox
With GrdC43cfg.ComboBox(EnListed.ButtonType).Items
.Clear()
.Add("点按") '默认点按
.Add("松开")
.Add("长按")
End With
'设备类型
GrdC43cfg.Column(EnListed.DeviceType).Alignment = FlexCell.AlignmentEnum.CenterCenter
GrdC43cfg.Column(EnListed.DeviceType).CellType = FlexCell.CellTypeEnum.ComboBox
With GrdC43cfg.ComboBox(EnListed.DeviceType).Items
.Clear()
.Add("场景")
.Add("本地继电器")
.Add("本地服务信息")
.Add("干节点窗帘")
.Add("空调")
.Add("LED扩展")
.Add("背景音乐")
.Add("监控")
End With
'设备编号
GrdC43cfg.Column(EnListed.DeviceNumber).Alignment = FlexCell.AlignmentEnum.CenterCenter
GrdC43cfg.Column(EnListed.DeviceNumber).Mask = FlexCell.MaskEnum.Numeric
GrdC43cfg.Column(EnListed.DeviceNumber).DecimalLength = 0
'执行方式
GrdC43cfg.Column(EnListed.CarryWayL).Alignment = FlexCell.AlignmentEnum.CenterCenter
'执行内容
GrdC43cfg.Column(EnListed.CarryWayH).Alignment = FlexCell.AlignmentEnum.CenterCenter
'场景控制方式
GrdC43cfg.Column(EnListed.SceneControlWay).Alignment = FlexCell.AlignmentEnum.CenterCenter
GrdC43cfg.Column(EnListed.SceneControlWay).CellType = FlexCell.CellTypeEnum.ComboBox
With GrdC43cfg.ComboBox(EnListed.SceneControlWay).Items
.Clear()
.Add("场景单控")
.Add("场景可关闭")
End With
'延时执行
GrdC43cfg.Column(EnListed.DelayToPerform).Alignment = FlexCell.AlignmentEnum.CenterCenter
GrdC43cfg.Column(EnListed.DelayToPerform).Mask = FlexCell.MaskEnum.Numeric
GrdC43cfg.Column(EnListed.DelayToPerform).DecimalLength = 0
'备注
GrdC43cfg.Column(EnListed.Remark).Alignment = FlexCell.AlignmentEnum.LeftCenter
End Sub
''' <summary>
''' C43搜索命令—按地址搜索
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
_sendTick = 0 '标志位
'获取Set发送内容Data部分
_port485 = $"&H{Hex(NudC43Port.Value)}"
_timeouts = $"&H{Hex(NudC43TimeOut.Value)}"
'组包C43搜索命令
Dim C43Address As Byte = $"&H{Hex(NudAddress.Value)}"
Dim C43Data(4) As Byte
C43Data(0) = C43Address
C43Data(1) = &HA3
C43Data(2) = &H1
C43Data(3) = &H0
C43Data(4) = C43Address + &HA3 + &H1 + &H0 '和校验
'填充数据包
C43Data = FillPacket(C43Data)
'发送数据包
SendData(C43Data)
End Sub
''' <summary>
''' C43发布
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub BtnReleaseUpgrade_Click(sender As Object, e As EventArgs) Handles BtnReleaseUpgrade.Click
_sendTick = 0 '标志位
'获取Set发送内容Data部分
_port485 = $"&H{Hex(NudC43Port.Value)}"
_timeouts = $"&H{Hex(NudC43TimeOut.Value)}"
Dim C43Address As Byte = $"&H{Hex(NudAddress.Value)}" '获取发送数据部分
Dim C43dataList() As Byte = C43cfgDataTakeOut() '表格数据
Dim C43Head() As Byte = C43FirstBlockHeadData() '8个字节的版本号&全局变量
Dim C43crc16() As Byte = CheckSendData(C43Head, C43dataList) 'C43发布命令的CRC16校验
Dim blocks As Byte = GetBlocks(C43dataList) '求数据的块数
'和校验
Dim dataSumI As UInteger = C43Address + &HA6 + &H1 + &H1 + C43crc16(0) + C43crc16(1) + blocks
Dim data7Sum As Byte = dataSumI And &HFF
'组包C43发布命令
Dim C43Data(7) As Byte
C43Data(0) = C43Address
C43Data(1) = &HA6
C43Data(2) = &H1
C43Data(3) = &H1
C43Data(4) = C43crc16(0)
C43Data(5) = C43crc16(1)
C43Data(6) = blocks
C43Data(7) = data7Sum
'填充数据包
C43Data = FillPacket(C43Data)
'发送数据
SendData(C43Data)
End Sub
''' <summary>
''' C43第一包数据包头
''' </summary>
''' <returns></returns>
Private Function C43FirstBlockHeadData() As Byte()
Dim C43Head(7) As Byte
C43Head(0) = &H0 '配置版本号
C43Head(1) = &H0 '配置版本号
C43Head(2) = &H0 '配置版本号
C43Head(3) = &H0 '全局变量_背光时间
C43Head(4) = &H0 '备用
C43Head(5) = &H0 '备用
C43Head(6) = &H0 '备用
C43Head(7) = &H0 '备用
Return C43Head
End Function
''' <summary>
''' C43下发数据
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub BtnIssuesData_Click(sender As Object, e As EventArgs) Handles BtnIssuesData.Click
SendC43DataToRCU()
End Sub
''' <summary>
''' 下发C43数据到RCU
''' </summary>
Private Sub SendC43DataToRCU()
Dim sendPacket As Byte()
Dim serialAppPacket As Byte()
Dim C43Data As Byte()
Dim blockCount As Integer
_port485 = $"&H{Hex(NudC43Port.Value)}"
_timeouts = $"&H{Hex(NudC43TimeOut.Value)}"
C43Data = C43cfgDataTakeOut() '取表中数据
blockCount = GetBlocks(C43Data) '获取总发送块数
For blockIndex = 0 To blockCount - 1 '循环发送数据块
serialAppPacket = FillC43SendPacket(C43Data, blockIndex blockCount) '组C43透传数据包
sendPacket = FillPacket(serialAppPacket)
SendData(sendPacket)
Dim Interval As Integer = NudInterval.Value
Thread.Sleep(Interval) '发送间隔
Next
End Sub
''' <summary>
''' 取出C43配置数据
''' </summary>
''' <returns></returns>
Private Function C43cfgDataTakeOut() As Byte()
Dim C43DataList As New List(Of Byte)
For row = 1 To GrdC43cfg.Rows - 1
Dim dataOne As Byte
If GrdC43cfg.Cell(row, EnListed.ButtonName).Text = Nothing Then
GrdC43cfg.Cell(row, EnListed.ButtonName).Text = 1
dataOne = &H1
Else
dataOne = $"&H{Hex(GrdC43cfg.Cell(row, EnListed.ButtonName).Text)}"
End If
Dim dataTwo As Byte
If GrdC43cfg.Cell(row, EnListed.ButtonType).Text = Nothing Then
GrdC43cfg.Cell(row, EnListed.ButtonType).Text = "点按"
dataTwo = &H1
ElseIf GrdC43cfg.Cell(row, EnListed.ButtonType).Text = "点按" Then
dataTwo = &H1
ElseIf GrdC43cfg.Cell(row, EnListed.ButtonType).Text = "松开" Then
dataTwo = &H2
ElseIf GrdC43cfg.Cell(row, EnListed.ButtonType).Text = "长按" Then
dataTwo = &H3
End If
Dim dataThree As Byte
If GrdC43cfg.Cell(row, EnListed.DeviceType).Text = Nothing Then
GrdC43cfg.Cell(row, EnListed.DeviceType).Text = "场景"
dataThree = &H0
ElseIf GrdC43cfg.Cell(row, EnListed.DeviceType).Text = "场景" Then
dataThree = &H0
ElseIf GrdC43cfg.Cell(row, EnListed.DeviceType).Text = "本地继电器" Then
dataThree = &H1
ElseIf GrdC43cfg.Cell(row, EnListed.DeviceType).Text = "本地服务信息" Then
dataThree = &H4
ElseIf GrdC43cfg.Cell(row, EnListed.DeviceType).Text = "干节点窗帘" Then
dataThree = &H5
ElseIf GrdC43cfg.Cell(row, EnListed.DeviceType).Text = "空调" Then
dataThree = &H7
ElseIf GrdC43cfg.Cell(row, EnListed.DeviceType).Text = "LED扩展" Then
dataThree = &H10
ElseIf GrdC43cfg.Cell(row, EnListed.DeviceType).Text = "背景音乐" Then
dataThree = &H15
ElseIf GrdC43cfg.Cell(row, EnListed.DeviceType).Text = "监控" Then
dataThree = &H24
End If
Dim dataFour As Byte
If GrdC43cfg.Cell(row, EnListed.DeviceNumber).Text = Nothing Then
GrdC43cfg.Cell(row, EnListed.DeviceNumber).Text = 1
dataFour = &H1
Else
dataFour = $"&H{Hex(GrdC43cfg.Cell(row, EnListed.DeviceNumber).Text)}"
End If
Dim dataFive As Byte
If GrdC43cfg.Cell(row, EnListed.CarryWayL).Text = Nothing Then
GrdC43cfg.Cell(row, EnListed.CarryWayL).Text = 1
dataFive = &H1
Else
dataFive = $"&H{Hex(GrdC43cfg.Cell(row, EnListed.CarryWayL).Text)}"
End If
Dim dataSix As Byte
If GrdC43cfg.Cell(row, EnListed.CarryWayH).Text = Nothing Then
GrdC43cfg.Cell(row, EnListed.CarryWayH).Text = 0
dataSix = &H0
Else
dataSix = $"&H{Hex(GrdC43cfg.Cell(row, EnListed.CarryWayH).Text)}"
End If
Dim dataSeven As Byte
If GrdC43cfg.Cell(row, EnListed.SceneControlWay).Text = Nothing Then
GrdC43cfg.Cell(row, EnListed.SceneControlWay).Text = "场景单控"
dataSeven = &H1
ElseIf GrdC43cfg.Cell(row, EnListed.SceneControlWay).Text = "场景单控" Then
dataSeven = &H1
ElseIf GrdC43cfg.Cell(row, EnListed.SceneControlWay).Text = "场景可关闭" Then
dataSeven = &H2
End If
Dim dataEight As Byte
If GrdC43cfg.Cell(row, EnListed.DelayToPerform).Text = Nothing Then
GrdC43cfg.Cell(row, EnListed.DelayToPerform).Text = 0
dataEight = &H0
Else
dataEight = $"&H{Hex(GrdC43cfg.Cell(row, EnListed.DelayToPerform).Text)}"
End If
If GrdC43cfg.Cell(row, EnListed.IsSelected).BooleanValue = True Then
C43DataList.Add(dataOne)
C43DataList.Add(dataTwo)
C43DataList.Add(dataThree)
C43DataList.Add(dataFour)
C43DataList.Add(dataFive)
C43DataList.Add(dataSix)
C43DataList.Add(dataSeven)
C43DataList.Add(dataEight)
Else
Console.WriteLine("此行数据未选中!")
End If
Next
Return C43DataList.ToArray
End Function
''' <summary>
''' 填充C43发送数据包
''' </summary>
''' <param name="C43Data"></param>
''' <param name="blockIndex"></param>
''' <param name="blockCount"></param>
''' <returns></returns>
Private Function FillC43SendPacket(C43Data As Byte(), blockIndex As Integer blockCount As Integer) As Byte()
Dim dataList As New List(Of Byte)
dataList.AddRange(FillC43AddrData(blockIndex)) '地址4个字节
dataList.AddRange(FillC43SendPacketByBlock(C43Data, blockIndex blockCount)) '按块填充表格数据
dataList.Add(GetSumCheck(dataList.ToArray)) '求块数据和校验
Return dataList.ToArray
End Function
''' <summary>
''' 填充C43地址数据
''' </summary>
''' <param name="blockIndex"></param>
''' <returns></returns>
Private Function FillC43AddrData(blockIndex As Integer) As Byte()
Dim C43Address As Byte = $"&H{Hex(NudAddress.Value)}" '获取发送数据部分
Dim C43Data(3) As Byte
C43Data(0) = C43Address
C43Data(1) = &HA7
C43Data(2) = &H1
C43Data(3) = blockIndex
Return C43Data
End Function
''' <summary>
''' 按块填充C43表格数据
''' </summary>
''' <param name="C43Data"></param>
''' <param name="blockIndex"></param>
''' <param name="blockCount"></param>
''' <returns></returns>
Private Function FillC43SendPacketByBlock(C43Data As Byte(), blockIndex As Integer blockCount As Integer) As Byte()
Dim blockPacket As New List(Of Byte)
If blockIndex = 0 Then blockPacket.AddRange(C43FirstBlockHeadData) '第0块额外添加8个字节数据
If blockIndex = blockCount - 1 Then
For index = 128 * blockIndex To C43Data.Length - 1
blockPacket.Add(C43Data(index))
Next
Else
For index = 128 * blockIndex To 128 * (blockIndex + 1) - 1
blockPacket.Add(C43Data(index))
Next
End If
Return blockPacket.ToArray
End Function
''' <summary>
''' C43表格配置按键
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub TSBtnC43cfgData_Click(sender As Object, e As EventArgs) Handles TsBtnC43cfgData.Click
GrdC43cfg.Rows = 1 '清除表格数据
C43ConfigData()
End Sub
''' <summary>
''' 增加数据行
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub TSBtnAddRows_Click(sender As Object, e As EventArgs) Handles TsBtnAddRows.Click
GrdC43cfg.AddItem("")
GrdC43cfg.Cell(GrdC43cfg.Rows - 1, EnListed.IsSelected).Text = "1"
End Sub
''' <summary>
''' 删除数据行
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub TSBtnDelete_Click(sender As Object, e As EventArgs) Handles TsBtnDelete.Click
GrdC43cfg.Selection.DeleteByRow()
End Sub
''' <summary>
''' 保存C43数据
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub TSBtnSave_Click(sender As Object, e As EventArgs) Handles TsBtnSave.Click
Dim dirPath As String = Application.StartupPath & "/" & "C43Config"
If Directory.Exists(dirPath) = False Then Directory.CreateDirectory(dirPath)
SaveFileDialog1.InitialDirectory = dirPath
SaveFileConfig()
End Sub
''' <summary>
''' 保存文件配置
''' </summary>
Private Sub SaveFileConfig()
With SaveFileDialog1
.Filter = "保存为xml|*.xml"
.FileName = "C43"
.DefaultExt = "xml"
.AddExtension = True
End With
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
GrdC43cfg.ExportToXML(SaveFileDialog1.FileName)
End If
End Sub
''' <summary>
''' 读取数据
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub TSBtnRead_Click(sender As Object, e As EventArgs) Handles TsBtnRead.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
GrdC43cfg.LoadFromXML(OpenFileDialog1.FileName)
End If
End Sub
'Private Function FillSerialAppPacket(serialAppPacket As Byte()) As Byte()
' '获取Set发送内容Data部分
' Dim serialCmd As Byte = &H3
' Dim port485 As Byte = $"&H{Hex(NudC43Port.Value)}"
' Dim timeOut As Byte = $"&H{Hex(NudC43TimeOut.Value)}"
' Dim dataLen As Integer = serialAppPacket.Length + 3
' Dim data(dataLen - 1) As Byte
' data(0) = serialCmd
' data(1) = port485
' data(2) = timeOut
' Array.Copy(serialAppPacket, 0, data, 3, serialAppPacket.Length)
' '获取发送数据Head部分和CRC16校验值部分
' Dim byteHead As Byte() = SendDataHead(data) '获取DataHaed
' Dim crc16 As Byte() = CheckSendData(byteHead, data) '获取Data检验值
' Return FillSendPacket(byteHead, data, crc16)
'End Function
#End Region
End Class