基于单片机的智能路灯控制系统..
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
元件清单
Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
'通信
Dim commflag As Boolean
Dim commstr(0 To 3) As Byte
'灯泡状态true 亮false 灭
Dim lightstate As Boolean
'故障指示灯状态true 亮false 灭
Dim errorledstate As Boolean
'联机指示
Dim connection As Boolean
Dim config As String
//当点击“联机”按钮时或动其他按钮时,如果计算机和硬件未连接,会出现”端口打开错误”的命令窗口;如果计算机和硬件连接了,会出现”系统已联机”的字样。
Private Sub Command1_Click()
On Error GoTo out
If Not connection Then
mPort = bo1.ListIndex + 1
Me.MSComm1.PortOpen = True
If Me.MSComm1.PortOpen = False Then
MsgBox "端口打开错误", vbOKOnly, "错误"
Else
connection = Not connection
bel8.Caption = "系统已联机"
bel8.ForeColor = RGB(255, 0, 0)
mand1.Caption = "断开"
commstr(0) = &HFF
commstr(1) = &H1
commstr(2) = &H0
commstr(3) = commstr(0) Xor commstr(1) Xor commstr(2)
commflag = True
Me.MSComm1.Output = commstr
End If
Else
connection = Not connection
Me.MSComm1.PortOpen = False
bel8.Caption = "系统未联机"
bel8.ForeColor = RGB(255, 255, 255)
mand1.Caption = "联机"
lightstate = False
errorledstate = False
Me.lederror(0).FillColor = RGB(100, 100, 100)
Me.ledstate(0).FillColor = RGB(100, 100, 100)
End If
GoTo out2
out: MsgBox "端口打开错误", vbOKOnly, "错误"
out2:
End Sub
Private Sub Form_Load()
Dim timestr As String
Me.ledstate(0).FillColor = RGB(100, 100, 100)
Me.lederror(0).FillColor = RGB(100, 100, 100)
bel3.Caption = Now()
bo1.ListIndex = 0
//端口的参数设置
'串口
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = 0 '采用文本接收
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 1 '产生MSComm事件
'恢复时间
config = App.Path + "\" + "config.ini"
Open config For Input As #1 '读取该汉字在16点阵字库中的原始字模Line Input #1, timestr
Me.DTPicker1.Value = timestr
Line Input #1, timestr
Me.DTPicker2.Value = timestr
'清发送标志
commflag = False
Me.Text1.Text = Str(Me.UpDown1.Value)
errorledstate = False
lightstate = False
connection = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
'保存时间
Dim timestr As String
config = App.Path + "\" + "config.ini"
Open config For Output As #1 '读取该汉字在16点阵字库中的原始字模
Print #1, Me.DTPicker1.Value
Print #1, Me.DTPicker2.Value
Close #1
End Sub
//当PC机发送“开灯”请求时,单片机回送“tuon”命令,开灯成功;当PC机发送“故障检测”请求时,单片机回送“trou”命令,说明灯泡故障,VB 界面中的故障灯会亮,反之单片机回送“norm”命令,说明灯泡是好的,正常的,VB界面种的故障灯不会发生变化。
Private Sub MSComm1_OnComm()
Dim strBuff As String
Select Case mEvent
Case 2
strBuff = MSComm1.Input
If strBuff = "ok" Then
commflag = False
ElseIf strBuff = "toff" Then
Me.ledstate(0).FillColor = RGB(100, 100, 100)
lightstate = False
commflag = False
ElseIf strBuff = "tuon" Then
Me.ledstate(0).FillColor = RGB(255, 0, 0)
lightstate = True
commflag = False
ElseIf strBuff = "trou" Then
Me.lederror(0).FillColor = RGB(255, 0, 0)
ElseIf strBuff = "norm" Then
Me.lederror(0).FillColor = RGB(100, 100, 100)
End If
End Select