📄 form1.frm
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "Mscomm32.ocx"
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 8625
ClientLeft = 60
ClientTop = 450
ClientWidth = 10830
LinkTopic = "Form1"
ScaleHeight = 8625
ScaleWidth = 10830
StartUpPosition = 3 '窗口缺省
Begin VB.PictureBox Picture1
AutoRedraw = -1 'True
Height = 4215
Left = 1800
ScaleHeight = 4155
ScaleWidth = 4755
TabIndex = 8
Top = 1200
Width = 4815
End
Begin VB.Timer Timer2
Left = 9840
Top = 7080
End
Begin VB.Timer Timer1
Interval = 500
Left = 9000
Top = 7080
End
Begin VB.CommandButton Command2
Caption = "采集"
BeginProperty Font
Name = "仿宋_GB2312"
Size = 14.25
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 495
Left = 3720
TabIndex = 7
Top = 6720
Width = 1215
End
Begin VB.CommandButton Command1
Caption = "退出"
BeginProperty Font
Name = "仿宋_GB2312"
Size = 15.75
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 495
Left = 6360
TabIndex = 6
Top = 6720
Width = 1215
End
Begin VB.TextBox Text3
Height = 975
Left = 7680
TabIndex = 2
Text = "Text3"
Top = 4080
Width = 1815
End
Begin VB.TextBox Text2
Height = 975
Left = 7680
TabIndex = 1
Text = "Text2"
Top = 1920
Width = 1815
End
Begin VB.TextBox Text1
Height = 975
Left = 720
TabIndex = 0
Text = "Text1"
Top = 6480
Width = 1815
End
Begin MSCommLib.MSComm MSComm1
Left = 8880
Top = 6120
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
End
Begin VB.Label Label3
Caption = "温度下限"
BeginProperty Font
Name = "仿宋_GB2312"
Size = 14.25
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H000000FF&
Height = 255
Left = 7920
TabIndex = 5
Top = 3600
Width = 1215
End
Begin VB.Label Label2
Caption = "温度上限"
BeginProperty Font
Name = "仿宋_GB2312"
Size = 14.25
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H000000FF&
Height = 255
Left = 7920
TabIndex = 4
Top = 1440
Width = 1215
End
Begin VB.Label Label1
Caption = "当前温度值"
BeginProperty Font
Name = "仿宋_GB2312"
Size = 14.25
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 255
Left = 840
TabIndex = 3
Top = 6120
Width = 1575
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'两个计时器,一个用于每隔多长时间采集一次温度,另一个用于每隔多长时间和极限值比较一次
Dim i%
Dim x%(30), y!(30) '图上只能画30个数据
Private Sub Command1_Click()
Unload Me
End Sub
Private Sub Command2_Click()
If Command2.Caption = "停止" Then
MSComm1.Output = "0" '0代表停止温度采集
Command2.Caption = "采集"
Timer1.Enabled = False
Timer2.Enabled = False
Else
Command2.Caption = "采集"
MSComm1.Output = "1"
Command2.Caption = "停止"
Timer1.Enabled = True
Timer2.Enabled = True
End If
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.PortOpen = True
MSComm1.Settings = "4800,N,8,1" '4800波特率,无校验,8位数据位,1位停止位
MSComm1.RThreshold = 1
MSComm1.OutBufferCount = 0 '清空发送缓冲区
MSComm1.InBufferCount = 0 '清空接收缓冲区
Text1.Text = ""
Text2.Text = "80"
Text3.Text = "20"
Timer1.Interval = 5000 '每次采集时间,时间不能太短,如太短,送过来的数据会乱码
Timer2.Interval = 5000
End Sub
Private Sub Form_Paint()
Picture1.ScaleMode = 3
Picture1.Scale (-2, 30)-(30, -2)
'设置绘线宽度
Picture1.DrawWidth = 1
'绘坐标系的X轴及箭头线
Picture1.Line (-2, 0)-(30, 0), vbBlue
Picture1.Line (29.5, 0.5)-(30, 0), vbBlue
Picture1.Line -(29.5, -0.5), vbBlue
Picture1.ForeColor = vbBlue
Picture1.CurrentX = 28.5
Picture1.CurrentY = -0.5
Picture1.Print "X"
'绘坐标系的Y轴及箭头线
Picture1.Line (0, -2)-(0, 30), vbBlue
Picture1.Line (-0.5, 29.5)-(0, 30), vbBlue
Picture1.Line -(0.5, 29.5), vbBlue
Picture1.CurrentX = -1
Picture1.CurrentY = 28.5
Picture1.Print "Y"
'指定位置显示原点O
Picture1.CurrentX = -1
Picture1.CurrentY = -0.5
Picture1.Print "O"
'重设绘线宽度
Picture1.DrawWidth = 2
Picture1.ForeColor = vbRed
End Sub
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive '接收字符数据
Dim Buffer As Variant
MSComm1.InputLen = 0
'当InputMode 属性值为0(文本模式)时,变量中含String型数据。
'当InputMode属性值为1(二进制模式)时,变量中含Byte型数组数据。
MSComm1.InputMode = comInputModeText
Buffer = MSComm1.Input
Text1.Text = Right(Buffer, 4)
'Open "C:\log.txt" For Append As #1
'Print #1, "串口收到的数据为:" & Text1.Text
'Print #1,
'Close #1
L1: x(i) = i
y(i) = Val(Text1.Text)
If i = 0 Then
Picture1.PSet (i, y(i))
Else
Picture1.Line (i - 1, y(i - 1))-(i, y(i)) '易错的地方就是别忘了在那个容器上画图,一定要在方法前加上容器名称
End If '如Picture1.什么
i = i + 1
If i > 30 Then
Picture1.Cls
i = 0
Call Form_Paint
GoTo L1
End If
Case Else
End Select
End Sub
Private Sub Timer1_Timer()
MSComm1.Output = "1" '开始数据采集
End Sub
Private Sub Timer2_Timer()
If Val(Text1.Text) > Val(Text2.Text) Then
MSComm1.Output = "2" '停止加热
Else
If Val(Text1.Text) < Val(Text2.Text) Then
MSComm1.Output = "3" '开始加热
End If
End If
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -