Imports System.IO.Ports Imports System.Text Imports System.Threading Imports SharedFunction Public Class Form1 #Region "窗体加载" '富文本打印变量 Public G_RichTextPrint As RichTextPrint Public G_CommunicationProtocol As CommunicationProtocol Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Text = $"{Application.ProductName}_V{Application.ProductVersion}" G_RichTextPrint = New RichTextPrint(RichTextBox1) G_CommunicationProtocol = New CommunicationProtocol(G_RichTextPrint, SerialPort1) G_CommunicationProtocol.AddControlDic(&H18, Label4) G_CommunicationProtocol.AddControlDic(&H19, TextBox6) recvBufferli = New List(Of Byte)() End Sub #End Region #Region "串口事务" Public isread As Boolean = False '串口关闭标志 Public isListen As Boolean = False '串口等待关闭标志 Private Sub Btn_OpenPort_Click(sender As Object, e As EventArgs) Handles Btn_OpenPort.Click If Btn_OpenPort.Text = "关闭" Then Btn_OpenPort.Text = "打开" Btn_OpenPort.BackColor = Color.Transparent Cbo_Port.Enabled = True Cbo_Baud.Enabled = True G_CommunicationProtocol.setIsFlowList(False) G_CommunicationProtocol.ClearFlowNode(True) SharedFunction.CloseSerial(SerialPort1, isread, isListen) Else Cbo_Port.Enabled = False Cbo_Baud.Enabled = False Btn_OpenPort.Text = "关闭" Btn_OpenPort.BackColor = Color.OrangeRed Dim vmbaud As Integer = 0 '判断端口号是否合法 If String.IsNullOrEmpty(Cbo_Port.Text) Then MsgBox("请选择端口号!") Btn_OpenPort_Click(Nothing, Nothing) Return End If '判断波特率是否合法 If String.IsNullOrEmpty(Cbo_Baud.Text) OrElse Not Integer.TryParse(Cbo_Baud.Text, vmbaud) Then MsgBox("请选择波特率!") Btn_OpenPort_Click(Nothing, Nothing) Return End If '打开串口 If SharedFunction.OpenSerial(SerialPort1, Cbo_Port.Text, vmbaud) Then Else MsgBox("串口打开失败!") Btn_OpenPort_Click(Nothing, Nothing) Return End If End If End Sub Private _recvBuffer(4095) As Byte Public recvBufferli As List(Of Byte) Public recvBufferliindex As Integer = 0 Private Sub SerialPort1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived If isread Then Return isListen = True Static bytes As Integer = 0 Dim sp As SerialPort = DirectCast(sender, SerialPort) Try Do bytes = sp.BytesToRead If bytes <= 0 Then Exit Sub Dim buf(bytes - 1) As Byte sp.Read(buf, 0, bytes) recvBufferli.AddRange(buf) Thread.Sleep(5) Loop While sp.BytesToRead > 0 If recvBufferli.Count > 0 Then Dim buf(recvBufferli.Count - 1) As Byte Array.Copy(recvBufferli.ToArray, 0, buf, 0, buf.Length) recvBufferli.Clear() G_RichTextPrint.AddQueue(RichTextBox1, New RichTextNodeConfig($"RX:", Color.Green, New Font("宋体", 8), buf), 1) G_CommunicationProtocol.ProcessRecvData(buf) End If isListen = False Catch ex As Exception isListen = False 'AppendTipText($"串口接收数据失败,原因:{ex.Message}", Color.Red) G_RichTextPrint.AddQueue(RichTextBox1, New RichTextNodeConfig($"串口接收数据失败,原因:{ex.Message}", Color.Red), 1) 'RichTextPrint.OutputLogsToTheControl(m_Control, New RuningLogConfig($"串口接收数据失败,原因:{ex.Message}", Color.Red), 1) End Try End Sub Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed Timer1.Enabled = False If Not IsNothing(G_RichTextPrint) Then G_RichTextPrint.Close() End If If SerialPort1.IsOpen Then SharedFunction.CloseSerial(SerialPort1, isread, isListen) End If End Sub Private Sub Cbo_Port_DropDown(sender As Object, e As EventArgs) Handles Cbo_Port.DropDown Cbo_Port.Items.Clear() Cbo_Port.Items.AddRange(SerialPort.GetPortNames()) End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If G_CommunicationProtocol.getIsFlowList Then MsgBox("正在通信中!") Return Else G_CommunicationProtocol.ClearFlowNode() G_CommunicationProtocol.setIsFlowList(True) End If Dim li As List(Of Byte) = New List(Of Byte)() Dim OSDstr As String = TextBox1.Text Dim OSDbuf As Byte() = Encoding.ASCII.GetBytes(OSDstr) If IsNothing(OSDbuf) OrElse OSDbuf.Length = 0 Then MsgBox("请输入正确的OSD内容!") Return End If li.Add(OSDbuf.Length) li.AddRange(OSDbuf) OSDbuf = NT_CAM.PackData(&HA0, li.ToArray) Dim gSendNode As New SendNode(1) gSendNode.SetSendData(OSDbuf) G_CommunicationProtocol.AddFlowNode("设置OSD内容", gSendNode) End Sub Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click If G_CommunicationProtocol.getIsFlowList Then MsgBox("正在通信中!") Return Else G_CommunicationProtocol.ClearFlowNode() G_CommunicationProtocol.setIsFlowList(True) End If '获取当前按钮 Dim btn As Button = DirectCast(sender, Button) '获取当前按钮的+Tag值 Dim ntag As Integer = CInt(btn.Tag) Dim OSDbuf As Byte() = {ntag} G_CommunicationProtocol.ClearFlowNode() OSDbuf = NT_CAM.PackData(&HA1, OSDbuf) Dim gSendNode As New SendNode(1) G_CommunicationProtocol.setIsFlowList(True) gSendNode.SetSendData(OSDbuf) G_CommunicationProtocol.AddFlowNode("设置报警状态", gSendNode) End Sub Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click, Button8.Click If G_CommunicationProtocol.getIsFlowList Then MsgBox("正在通信中!") Return Else G_CommunicationProtocol.ClearFlowNode() G_CommunicationProtocol.setIsFlowList(True) End If '获取当前按钮 Dim btn As Button = DirectCast(sender, Button) '获取当前按钮的+Tag值 Dim ntag As Integer = CInt(btn.Tag) Dim nComb, mComb As Integer '获取下拉框数值 If Integer.TryParse(ComboBox1.Text.Trim, nComb) = False Then MsgBox("请输入正确的音乐序号!") Return End If If Integer.TryParse(ComboBox3.Text.Trim, mComb) = False Then MsgBox("请输入正确的音乐序号!") Return End If Dim OSDbuf As Byte() = {mComb, nComb, 0, 1} '{ntag, nComb} G_CommunicationProtocol.ClearFlowNode() OSDbuf = NT_CAM.PackData(&H21, OSDbuf) Dim gSendNode As New SendNode(1) G_CommunicationProtocol.setIsFlowList(True) gSendNode.SetSendData(OSDbuf) G_CommunicationProtocol.AddFlowNode("播放语音", gSendNode) End Sub Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click '获取TextBox2文本 Dim OSDstr As String = TextBox2.Text '将文本转HEX 数据 If G_CommunicationProtocol.getIsFlowList Then MsgBox("正在通信中!") Return Else G_CommunicationProtocol.ClearFlowNode() G_CommunicationProtocol.setIsFlowList(True) End If G_CommunicationProtocol.ClearFlowNode() OSDstr = SharedFunction.StringToHexString(OSDstr) If IsNothing(OSDstr) OrElse OSDstr.Length = 0 Then MsgBox("请输入正确的透传数据!") Return End If Dim OSDbuf As Byte() = SharedFunction.HexStringToByte(OSDstr) OSDbuf = NT_CAM.PackData(&H17, OSDbuf) Dim gSendNode As New SendNode(1) gSendNode.SetSendData(OSDbuf) G_CommunicationProtocol.setIsFlowList(True) G_CommunicationProtocol.AddFlowNode("透传命令", gSendNode) 'Console.WriteLine(OSDstr) End Sub Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress, TextBox5.KeyPress e.Handled = Not $"0123456789ABCDEFabcdef{vbBack} ".Contains(e.KeyChar) '如果要只允许输入数字, End Sub Private Sub SplitContainer2_Panel2_Click(sender As Object, e As EventArgs) Dim btn As Button = DirectCast(sender, Button) If G_CommunicationProtocol.getIsFlowList Then MsgBox("正在通信中!") Return Else G_CommunicationProtocol.ClearFlowNode() G_CommunicationProtocol.setIsFlowList(True) End If '获取当前按钮的+Tag值 Dim nComb As Byte() = SharedFunction.HexStringToByte(ComboBox1.Text.Trim) If IsNothing(nComb) OrElse nComb.Length = 0 Then MsgBox("请输入正确的命令!") Return End If Dim ntag As Integer = CInt(btn.Tag) Dim OSDbuf As Byte() = {ntag} G_CommunicationProtocol.ClearFlowNode() OSDbuf = NT_CAM.PackData(nComb(0), OSDbuf) Dim gSendNode As New SendNode(1) gSendNode.SetSendData(OSDbuf) G_CommunicationProtocol.setIsFlowList(True) G_CommunicationProtocol.AddFlowNode("IPC请求回复", gSendNode) End Sub Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click '清空富文本 RichTextBox1.Clear() End Sub Private Sub ComboBox1_DropDown(sender As Object, e As EventArgs) Handles ComboBox1.DropDown, ComboBox3.DropDown '将sender 转换为ComboBox Dim mcomboBox As ComboBox = DirectCast(sender, ComboBox) mcomboBox.Items.Clear() '添加1到255 For i As Integer = 1 To 255 mcomboBox.Items.Add(i) Next End Sub Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click Dim OSDstr As String = TextBox3.Text Dim OSDbuf As Byte() = Encoding.ASCII.GetBytes(OSDstr) If IsNothing(OSDbuf) OrElse OSDbuf.Length = 0 Then MsgBox("请输入正确的产品ID!") Return End If If G_CommunicationProtocol.getIsFlowList Then MsgBox("正在通信中!") Return Else G_CommunicationProtocol.ClearFlowNode() G_CommunicationProtocol.setIsFlowList(True) End If OSDbuf = NT_CAM.PackData(&H9, OSDbuf) Dim gSendNode As New SendNode(1) G_CommunicationProtocol.setIsFlowList(True) gSendNode.SetSendData(OSDbuf) G_CommunicationProtocol.AddFlowNode("设置产品ID", gSendNode) End Sub Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click '获取TextBox2文本 Dim OSDstr As String = TextBox5.Text '将文本转HEX 数据 If G_CommunicationProtocol.getIsFlowList Then MsgBox("正在通信中!") Return Else G_CommunicationProtocol.ClearFlowNode() G_CommunicationProtocol.setIsFlowList(True) End If G_CommunicationProtocol.ClearFlowNode() OSDstr = SharedFunction.StringToHexString(OSDstr) If IsNothing(OSDstr) OrElse OSDstr.Length = 0 Then MsgBox("请输入正确的透传数据!") Return End If Dim OSDbuf As Byte() = SharedFunction.HexStringToByte(OSDstr) OSDbuf = NT_CAM.PackData(&H20, OSDbuf) Dim gSendNode As New SendNode(1) gSendNode.SetSendData(OSDbuf) G_CommunicationProtocol.setIsFlowList(True) G_CommunicationProtocol.AddFlowNode("透传命令", gSendNode) End Sub Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click Dim OSDstr As String = TextBox4.Text Dim OSDbuf As Byte() = Encoding.ASCII.GetBytes(OSDstr) If IsNothing(OSDbuf) OrElse OSDbuf.Length = 0 Then MsgBox("请输入正确的产品ID!") Return End If If G_CommunicationProtocol.getIsFlowList Then MsgBox("正在通信中!") Return Else G_CommunicationProtocol.ClearFlowNode() G_CommunicationProtocol.setIsFlowList(True) End If OSDbuf = NT_CAM.PackData(&H17, OSDbuf) Dim gSendNode As New SendNode(1) G_CommunicationProtocol.setIsFlowList(True) gSendNode.SetSendData(OSDbuf) G_CommunicationProtocol.AddFlowNode("设置产品ID", gSendNode) End Sub Private Sub TextBox7_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox7.KeyPress e.Handled = Not $"0123456789{vbBack} ".Contains(e.KeyChar) '如果要只允许输入数字, End Sub Public SendingInterval As Integer = 0 Public SendingInterval1 As Integer = 0 Public SendingDate As String = "" Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged If CheckBox1.Checked Then TextBox7.Enabled = False TextBox8.Enabled = False '获取TextBox7 代表的发生间隔 SendingInterval = 30 Integer.TryParse(TextBox7.Text, SendingInterval) SendingDate = TextBox8.Text If SendingInterval < 1 AndAlso String.IsNullOrEmpty(SendingDate) Then MsgBox("请输入正确的发送间隔和发送内容!") CheckBox1.Checked = False Return End If '开启定时任务 Timer1.Enabled = True Else Timer1.Enabled = False TextBox7.Enabled = True TextBox8.Enabled = True End If End Sub Public Timer1cnt As Integer = 0 Public Timer2cnt As Integer = 0 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick If Timer1cnt = SendingInterval Then Timer1cnt = 1 Dim OSDstr As String = SendingDate '将文本转HEX 数据 If G_CommunicationProtocol.getIsFlowList Then MsgBox("正在通信中!") Return Else G_CommunicationProtocol.ClearFlowNode() G_CommunicationProtocol.setIsFlowList(True) End If G_CommunicationProtocol.ClearFlowNode() OSDstr = SharedFunction.StringToHexString(OSDstr) If IsNothing(OSDstr) OrElse OSDstr.Length = 0 Then MsgBox("请输入正确的透传数据!") Return End If Dim OSDbuf As Byte() = SharedFunction.HexStringToByte(OSDstr) OSDbuf = NT_CAM.PackData(&H20, OSDbuf) Dim gSendNode As New SendNode(1, 1) gSendNode.SetSendData(OSDbuf) gSendNode.IsRecvData = False G_CommunicationProtocol.setIsFlowList(True) G_CommunicationProtocol.AddFlowNode("透传命令", gSendNode) Else Timer1cnt += 1 End If End Sub Private Sub TextBox8_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox8.KeyPress e.Handled = Not $"0123456789ABCDEFabcdef{vbBack} ".Contains(e.KeyChar) '如果要只允许输入数字, End Sub Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click '获取TextBox2文本 ‘55 AA 01 0B 00 00 0B 'Dim OSDstr As String = TextBox5.Text '将文本转HEX 数据 If G_CommunicationProtocol.getIsFlowList Then MsgBox("正在通信中!") Return Else G_CommunicationProtocol.ClearFlowNode() G_CommunicationProtocol.setIsFlowList(True) End If G_CommunicationProtocol.ClearFlowNode() 'OSDstr = SharedFunction.StringToHexString(OSDstr) 'If IsNothing(OSDstr) OrElse OSDstr.Length = 0 Then ' MsgBox("请输入正确的透传数据!") ' Return 'End If Dim OSDbuf As Byte() '= SharedFunction.HexStringToByte(OSDstr) OSDbuf = {&H55, &HAA, &H1, &HB, &H0, &H0, &HB} Dim gSendNode As New SendNode(1) gSendNode.SetSendData(OSDbuf) G_CommunicationProtocol.setIsFlowList(True) G_CommunicationProtocol.AddFlowNode("透传命令", gSendNode) End Sub Private Sub TextBox9_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox9.KeyPress e.Handled = Not $"0123456789ABCDEFabcdef{vbBack} ".Contains(e.KeyChar) '如果要只允许输入数字, End Sub Public OSDbufSN As Integer = 0 Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged If CheckBox2.Checked Then '判断串口是否打开 TextBox9.Enabled = False '获取TextBox7 代表的发生间隔 SendingInterval1 = 500 Integer.TryParse(TextBox9.Text, SendingInterval1) 'SendingDate = TextBox8.Text If SendingInterval1 < 1 Then MsgBox("请输入正确的发送间隔!") CheckBox2.Checked = False Return End If '开启定时任务 OSDbufSN = 1 Timer2.Interval = 10 SendingInterval1 = SendingInterval1 / 10 Timer2.Start() Else OSDbufSN = 1 'Timer2.Enabled = False Timer2.Stop() TextBox9.Enabled = True End If End Sub Public cntSendingInterval1 As Integer = 0 Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick ' If Timer2cnt = SendingInterval1 Then ' Timer2cnt = 1 If cntSendingInterval1 < SendingInterval1 Then cntSendingInterval1 = cntSendingInterval1 + 1 Else If G_CommunicationProtocol.getIsFlowList1 Then cntSendingInterval1 = SendingInterval1 Return 'MsgBox("正在通信中!") Else G_CommunicationProtocol.ClearFlowNode() G_CommunicationProtocol.setIsFlowList(True) End If Dim li As List(Of Byte) = New List(Of Byte)() Dim m_snsn As Integer = GetNewSNTimeDate(OSDbufSN) Dim snbuf As String = GetSnString(m_snsn) Dim OSDstr As String = $"R:{snbuf} B:{snbuf} D:{snbuf} TD:{snbuf} HD:{snbuf} S:{snbuf} HC:{snbuf} HR:{snbuf}" Dim OSDbuf As Byte() = Encoding.ASCII.GetBytes(OSDstr) 'If IsNothing(OSDbuf) OrElse OSDbuf.Length = 0 Then ' MsgBox("请输入正确的OSD内容!") ' Return 'End If li.Add(OSDbuf.Length) li.AddRange(OSDbuf) OSDbuf = NT_CAM.PackData(&HA0, li.ToArray) OSDbuf(6) = 14 OSDbuf(21) = 11 OSDbuf(33) = 16 OSDbuf(OSDbuf.Length - 1) = 0 OSDbuf(OSDbuf.Length - 1) = SharedFunction.GetCheckSumMod2(OSDbuf) Dim gSendNode As New SendNode(1) gSendNode.SetSendData(OSDbuf, 800) G_CommunicationProtocol.ClearFlowNode() 'G_CommunicationProtocol.setIsFlowList(True) G_CommunicationProtocol.AddFlowNode("设置OSD内容", gSendNode) Console.WriteLine("发送OSD内容") 'Else ' Timer2cnt += 1 'End If cntSendingInterval1 = 0 End If End Sub Public Function GetNewSNTimeDate(ByRef sn As Integer) As Integer Dim result As Integer = 1 If sn > 9 Then result = 1 sn = 1 Else sn = sn + 1 result = sn End If Return result End Function Public Function GetSnString(sn As Integer) As String '如果sn只有个位数则前面补0 Dim snbuf As String '判断sn是否小于10 ,小于则前面补0 If sn < 10 Then snbuf = $"0{sn}" Else snbuf = sn.ToString End If Return snbuf End Function #End Region End Class