展开全部
上位机程序
Dim n As Integer
Dim t As Integer ‘定义全局变量
Private Sub Command2_Click() ‘单击按钮执行数据采集或停止采集
If Command2.Caption = "采集数据" Then
Command2.Caption = "停止采集"
Timer1.Enabled = True
MSComm1.Output = "A"
Else
Command2.Caption = "采集数据"
Timer1.Enabled = False
MSComm1.Output = "B"
MSComm1.Output = "B"
End If
End Sub
Private Sub Command4_Click() ‘单击按钮清空数据
Text1.Text = ""
Text4.Text = ""
End Sub
Private Sub Command5_Click() ‘单击按钮开关通信端口
If Command5.Caption = "开启通信" Then
Command5.Caption = "关闭通信"
COM.Enabled = False
Command2.Enabled = True
Command1.Enabled = True
Text2.Enabled = True
Select Case COM.ListIndex '指定端口号(COM)
Case 0 MSComm1.CommPort = 1
Case 1 MSComm1.CommPort = 2
Case 2 MSComm1.CommPort = 3
Case 3 MSComm1.CommPort = 4
End Select
MSComm1.Settings = "9600,n,8,1" '9600波特,无校验,8位数据位,1位停止位
MSComm1.InputMode = comInputModeText '接收文本数据
MSComm1.InputLen = 0 '读出接收缓冲区的所有内容
MSComm1.InBufferSize = 1024
MSComm1.OutBufferSize = 1024
MSComm1.RThreshold = 1 '每接收到1个字符,发生1次事件
MSComm1.PortOpen = True '打开串口
Else
Timer1.Enabled = False
MSComm1.Output = "B"
MSComm1.Output = "B"
Command5.Caption = "开启通信"
COM.Enabled = True
MSComm1.PortOpen = False
Command2.Caption = "采集数据"
Command2.Enabled = False
Command1.Enabled = False
Text2.Enabled = False
End If
End Sub
Private Sub Command7_Click() ‘单击按钮保存温度数据
CommonDialog1.Action = 2
Open CommonDialog1.FileName For Append As #1
Write #1, Now
Write #1, Text4.Text
Close #1
End Sub
Private Sub Form_Load()
COM.AddItem "COM1"
COM.AddItem "COM2"
COM.AddItem "COM3"
COM.AddItem "COM4"
End Sub
Private Sub MSComm1_OnComm() ‘串口接收到数据时读出数据
Select Case MSComm1.CommEvent
Case comEvReceive
Text1.Text = Text1.Text & MSComm1.Input
Case Else
End Select
End Sub
Private Sub Text1_Change() ‘对读出的数据进行处理
If (Len(Text1.Text) Mod 4 = 0) And (Len(Text1.Text) > 0) Then
Text3.Text = Right$(Text1.Text, 4) & "℃"
Text4.Text = Time & Chr$(6) & Chr$(6) & Chr$(6) & Text3.Text & " " & Text4.Text
Text7.Text = Time$
n = n + 1
End If
If n = 100 Then
n = 0
Text1.Text = ""
End If
End Sub
Private Sub Text5_Change() ‘改变读数据的时间
Timer1.Interval = Val(Text5.Text) * 1000
End Sub
Private Sub Timer1_Timer() ‘采集数据后进行绘图
MSComm1.Output = "A"
Dim a As Single
Dim a1 As String
Dim a2 As String
Dim a3 As String
Dim k As Integer
Static b(200) As Integer
Text7.Text = Time
For k = 1 To 199
b(k) = b(k + 1)
Next k
b(200) = Val(Text3.Text)
With MSChart1
For i = 1 To 200
a = Val(Mid(Text7.Text, 1, 2)) * 3600 + Val(Mid(Text7.Text, 4, 2)) * 60 + Val(Mid(Text7.Text, 7, 2)) - 200 * Val(Text5.Text) + i * Val(Text5.Text) + 1
If a < 0 Then
a = a + 86400
ElseIf a > 86400 Then
a = a - 86400
End If
If a \ 3600 < 10 Then
a1 = "0" & (a \ 3600)
Else
a1 = Str(a \ 3600)
End If
If (a Mod 3600) \ 60 < 10 Then
a2 = "0" & ((a Mod 3600) \ 60)
Else
a2 = (a Mod 3600) \ 60
End If
If ((a Mod 3600) Mod 60) < 10 Then
a3 = "0" & ((a Mod 3600) Mod 60)
Else
a3 = ((a Mod 3600) Mod 60)
End If
.Row = i
.Data = b(i)
.RowLabel = a1 & ":" & a2 & ":" & a3
Next i
End With
End Sub
Private Sub Timer2_Timer() ‘设置温度时间隔开发送数据的时间,以使
If t = 0 Then ‘单片机能正常处理
MSComm1.Output = Mid(Text8.Text, 1, 1)
t = t + 1
ElseIf t = 1 Then
MSComm1.Output = Mid(Text8.Text, 2, 1)
t = t + 1
ElseIf t = 2 Then
MSComm1.Output = Mid(Text8.Text, 3, 1)
t = t + 1
ElseIf t = 3 Then
MSComm1.Output = Mid(Text8.Text, 4, 1)
t = t + 1
ElseIf t = 4 Then
MSComm1.Output = Mid(Text8.Text, 5, 1)
t = 0
Timer2.Enabled = False
End If
End Sub
Dim n As Integer
Dim t As Integer ‘定义全局变量
Private Sub Command2_Click() ‘单击按钮执行数据采集或停止采集
If Command2.Caption = "采集数据" Then
Command2.Caption = "停止采集"
Timer1.Enabled = True
MSComm1.Output = "A"
Else
Command2.Caption = "采集数据"
Timer1.Enabled = False
MSComm1.Output = "B"
MSComm1.Output = "B"
End If
End Sub
Private Sub Command4_Click() ‘单击按钮清空数据
Text1.Text = ""
Text4.Text = ""
End Sub
Private Sub Command5_Click() ‘单击按钮开关通信端口
If Command5.Caption = "开启通信" Then
Command5.Caption = "关闭通信"
COM.Enabled = False
Command2.Enabled = True
Command1.Enabled = True
Text2.Enabled = True
Select Case COM.ListIndex '指定端口号(COM)
Case 0 MSComm1.CommPort = 1
Case 1 MSComm1.CommPort = 2
Case 2 MSComm1.CommPort = 3
Case 3 MSComm1.CommPort = 4
End Select
MSComm1.Settings = "9600,n,8,1" '9600波特,无校验,8位数据位,1位停止位
MSComm1.InputMode = comInputModeText '接收文本数据
MSComm1.InputLen = 0 '读出接收缓冲区的所有内容
MSComm1.InBufferSize = 1024
MSComm1.OutBufferSize = 1024
MSComm1.RThreshold = 1 '每接收到1个字符,发生1次事件
MSComm1.PortOpen = True '打开串口
Else
Timer1.Enabled = False
MSComm1.Output = "B"
MSComm1.Output = "B"
Command5.Caption = "开启通信"
COM.Enabled = True
MSComm1.PortOpen = False
Command2.Caption = "采集数据"
Command2.Enabled = False
Command1.Enabled = False
Text2.Enabled = False
End If
End Sub
Private Sub Command7_Click() ‘单击按钮保存温度数据
CommonDialog1.Action = 2
Open CommonDialog1.FileName For Append As #1
Write #1, Now
Write #1, Text4.Text
Close #1
End Sub
Private Sub Form_Load()
COM.AddItem "COM1"
COM.AddItem "COM2"
COM.AddItem "COM3"
COM.AddItem "COM4"
End Sub
Private Sub MSComm1_OnComm() ‘串口接收到数据时读出数据
Select Case MSComm1.CommEvent
Case comEvReceive
Text1.Text = Text1.Text & MSComm1.Input
Case Else
End Select
End Sub
Private Sub Text1_Change() ‘对读出的数据进行处理
If (Len(Text1.Text) Mod 4 = 0) And (Len(Text1.Text) > 0) Then
Text3.Text = Right$(Text1.Text, 4) & "℃"
Text4.Text = Time & Chr$(6) & Chr$(6) & Chr$(6) & Text3.Text & " " & Text4.Text
Text7.Text = Time$
n = n + 1
End If
If n = 100 Then
n = 0
Text1.Text = ""
End If
End Sub
Private Sub Text5_Change() ‘改变读数据的时间
Timer1.Interval = Val(Text5.Text) * 1000
End Sub
Private Sub Timer1_Timer() ‘采集数据后进行绘图
MSComm1.Output = "A"
Dim a As Single
Dim a1 As String
Dim a2 As String
Dim a3 As String
Dim k As Integer
Static b(200) As Integer
Text7.Text = Time
For k = 1 To 199
b(k) = b(k + 1)
Next k
b(200) = Val(Text3.Text)
With MSChart1
For i = 1 To 200
a = Val(Mid(Text7.Text, 1, 2)) * 3600 + Val(Mid(Text7.Text, 4, 2)) * 60 + Val(Mid(Text7.Text, 7, 2)) - 200 * Val(Text5.Text) + i * Val(Text5.Text) + 1
If a < 0 Then
a = a + 86400
ElseIf a > 86400 Then
a = a - 86400
End If
If a \ 3600 < 10 Then
a1 = "0" & (a \ 3600)
Else
a1 = Str(a \ 3600)
End If
If (a Mod 3600) \ 60 < 10 Then
a2 = "0" & ((a Mod 3600) \ 60)
Else
a2 = (a Mod 3600) \ 60
End If
If ((a Mod 3600) Mod 60) < 10 Then
a3 = "0" & ((a Mod 3600) Mod 60)
Else
a3 = ((a Mod 3600) Mod 60)
End If
.Row = i
.Data = b(i)
.RowLabel = a1 & ":" & a2 & ":" & a3
Next i
End With
End Sub
Private Sub Timer2_Timer() ‘设置温度时间隔开发送数据的时间,以使
If t = 0 Then ‘单片机能正常处理
MSComm1.Output = Mid(Text8.Text, 1, 1)
t = t + 1
ElseIf t = 1 Then
MSComm1.Output = Mid(Text8.Text, 2, 1)
t = t + 1
ElseIf t = 2 Then
MSComm1.Output = Mid(Text8.Text, 3, 1)
t = t + 1
ElseIf t = 3 Then
MSComm1.Output = Mid(Text8.Text, 4, 1)
t = t + 1
ElseIf t = 4 Then
MSComm1.Output = Mid(Text8.Text, 5, 1)
t = 0
Timer2.Enabled = False
End If
End Sub
追问
其实我只想要个简单的既有上位机又有下位机程序的例子,如果有的话再麻烦您传下,
谢谢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询