📄 form1.frm
字号:
Left = 240
TabIndex = 7
Top = 960
Width = 975
End
Begin VB.CheckBox 节点2
Caption = "节点2"
Height = 375
Left = 1320
TabIndex = 6
Top = 360
Width = 855
End
Begin VB.CheckBox 节点1
Caption = "节点1"
Height = 375
Left = 240
TabIndex = 5
Top = 360
Width = 975
End
End
Begin VB.Label Label4
Caption = "上位机控制家用电器,并监控其工作状态"
BeginProperty Font
Name = "宋体"
Size = 10.5
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H000000FF&
Height = 855
Left = 720
TabIndex = 27
Top = 120
Width = 4575
End
Begin VB.Label Label3
Caption = "QQ:68421963 欢迎交流探讨"
Height = 255
Left = 2880
TabIndex = 26
Top = 7320
Width = 2295
End
Begin VB.Label Label1
Caption = "帮助"
Height = 255
Left = 240
TabIndex = 0
Top = 120
Width = 495
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim count1 As Integer
Dim count2 As Integer
Dim count3 As Integer
Dim count4 As Integer
Dim count5 As Integer
Dim count6 As Integer
Dim count7 As Integer
Dim count8 As Integer
Dim flag As Boolean
Dim flag_commu As Boolean
Dim receive As String
'软件复位代码
Private Sub Command1_Click()
节点1.Value = 0
节点2.Value = 0
节点3.Value = 0
节点4.Value = 0
节点5.Value = 0
节点6.Value = 0
节点7.Value = 0
节点8.Value = 0
If flag = False Then
MSComm1.Output = "R"
End If
Text11.Text = "0x00"
End Sub
'所有输出
Private Sub Command3_Click()
节点1.Value = 1
节点2.Value = 1
节点3.Value = 1
节点4.Value = 1
节点5.Value = 1
节点6.Value = 1
节点7.Value = 1
节点8.Value = 1
End Sub
'所有关闭
Private Sub Command4_Click()
节点1.Value = 0
节点2.Value = 0
节点3.Value = 0
节点4.Value = 0
节点5.Value = 0
节点6.Value = 0
节点7.Value = 0
节点8.Value = 0
End Sub
'所有关闭+显示关闭
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
' 初始化程序:主要完成对串口的设置,包括选择串口、设置波特率、设置数据格式、打开串口等。
' 注意:在程序开始之前,必须清空发送和接收缓冲区,以免出错。
MSComm1.Settings = "9600,n,8,1" ' 设置波特率和发送字符格式
' MSComm .Setting = "9600,s,8,1"
MSComm1.CommPort = 1 ' 设置通讯串口
MSComm1.InputLen = 0 ' 设置或返回一次从接收缓冲区中读取字节数,0表示一次读取所有数据
MSComm1.InBufferSize = 512 ' 设置接收缓冲区512Byte
MSComm1.InBufferCount = 0
MSComm1.InputMode = 0
MSComm1.OutBufferSize = 512 ' 设置发送缓冲区512Byte
MSComm1.OutBufferCount = 0
MSComm1.RThreshold = 1 ' 每个字符到接收缓冲区都触发接收事件
MSComm1.SThreshold = 1
'MSComm1.PortOpen = True
' MSComm1.PortOpen = True ' 打开串口
MSComm1.PortOpen = True
Label4.Visible = False
Timer3.Enabled = True
Timer1.Enabled = True
Timer1.Interval = 3000
Shape1.FillColor = &H0&
Shape2.FillColor = &H0&
Shape3.FillColor = &H0&
Shape4.FillColor = &H0&
Shape5.FillColor = &H0&
Shape6.FillColor = &H0&
Shape7.FillColor = &H0&
Shape8.FillColor = &H0&
Shape9.FillColor = &H0&
Shape10.FillColor = &H0&
End Sub
Private Sub Label1_Click()
Label4.Visible = True
End Sub
'接收数据
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent ' 设置oncomm事件,读取片机内存的值
Case comEvReceive
receive = MSComm1.Input
Select Case receive
Case "10"
Shape1.FillColor = &HFF&
Case "21"
Shape2.FillColor = &HFF&
Case "111"
Shape3.FillColor = &HFF&
Case "114"
Shape4.FillColor = &HFF&
Case "115"
Shape5.FillColor = &HFF&
Case "116"
Shape6.FillColor = &HFF&
Case "711"
Shape7.FillColor = &HFF&
Case "811"
Shape8.FillColor = &HFF&
Case "C"
flag_commu = True
Case Else
End Select
Case Else
End Select
End Sub
'发送通讯代码
Private Sub Timer1_Timer()
flag = True
MSComm1.Output = "C"
Timer2.Enabled = True
flag = False
End Sub
'判断通讯连接是否正常,正常亮绿灯,不正常亮红灯
Private Sub Timer2_Timer()
If flag_commu = True Then
Shape9.FillColor = &HC000&
Shape10.FillColor = &H0&
Text10.ForeColor = &H0&
flag_commu = False
ElseIf flag_commu = False Then
Shape9.FillColor = &H0&
Shape10.FillColor = &HFF&
Text10.ForeColor = &HFF&
End If
End Sub
Private Sub Timer3_Timer()
If count1 Mod 2 = 1 Then
Shape1.FillColor = &HFF&
Else
Shape1.FillColor = &H0&
End If
If count2 Mod 2 = 1 Then
Shape2.FillColor = &HFF&
Else
Shape2.FillColor = &H0&
End If
If count3 Mod 2 = 1 Then
Shape3.FillColor = &HFF&
Else
Shape3.FillColor = &H0&
End If
If count4 Mod 2 = 1 Then
Shape4.FillColor = &HFF&
Else
Shape4.FillColor = &H0&
End If
If count5 Mod 2 = 1 Then
Shape5.FillColor = &HFF&
Else
Shape5.FillColor = &H0&
End If
If count6 Mod 2 = 1 Then
Shape6.FillColor = &HFF&
Else
Shape6.FillColor = &H0&
End If
If count7 Mod 2 = 1 Then
Shape7.FillColor = &HFF&
Else
Shape7.FillColor = &H0&
End If
If count8 Mod 2 = 1 Then
Shape8.FillColor = &HFF&
Else
Shape8.FillColor = &H0&
End If
End Sub
'按钮事件
Private Sub 节点1_Click()
count1 = count1 + 1
If count1 = 10 Then
count1 = 0
End If
If flag = False Then
MSComm1.Output = "1"
End If
End Sub
Private Sub 节点2_Click()
count2 = count2 + 1
If count2 = 10 Then
count2 = 0
End If
If flag = False Then
MSComm1.Output = "2"
End If
End Sub
Private Sub 节点3_Click()
count3 = count3 + 1
If count3 = 10 Then
count3 = 0
End If
If flag = False Then
MSComm1.Output = "3"
End If
End Sub
Private Sub 节点4_Click()
count4 = count4 + 1
If count4 = 10 Then
count4 = 0
End If
If flag = False Then
MSComm1.Output = "4"
End If
End Sub
Private Sub 节点5_Click()
count5 = count5 + 1
If count5 = 10 Then
count5 = 0
End If
If flag = False Then
MSComm1.Output = "5"
End If
End Sub
Private Sub 节点6_Click()
count6 = count6 + 1
If count1 = 10 Then
count6 = 0
End If
If flag = False Then
MSComm1.Output = "6"
End If
End Sub
Private Sub 节点7_Click()
count7 = count7 + 1
If count7 = 10 Then
count7 = 0
End If
If flag = False Then
MSComm1.Output = "7"
End If
End Sub
Private Sub 节点8_Click()
count8 = count8 + 1
If count8 = 10 Then
count8 = 0
End If
If flag = False Then
MSComm1.Output = "8"
End If
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -