实例介绍
【实例简介】
【实例截图】
【核心代码】
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Begin VB.UserControl comv
ClientHeight = 5760
ClientLeft = 0
ClientTop = 0
ClientWidth = 5010
ScaleHeight = 5760
ScaleWidth = 5010
Begin VB.CommandButton Command3
Caption = "清空数据重新接收"
Height = 1095
Left = 4320
TabIndex = 17
Top = 120
Width = 615
End
Begin VB.TextBox Text6
Height = 375
Left = 3360
TabIndex = 16
Top = 960
Width = 855
End
Begin VB.CommandButton Command2
Caption = "开始接收"
Height = 375
Left = 3360
TabIndex = 15
Top = 5280
Width = 1215
End
Begin VB.PictureBox Picture2
BackColor = &H8000000E&
Height = 3255
Left = 0
ScaleHeight = 3195
ScaleWidth = 4875
TabIndex = 13
Top = 1320
Visible = 0 'False
Width = 4935
End
Begin VB.CommandButton Command1
Caption = "数据曲线显示"
Height = 375
Left = 1440
TabIndex = 12
Top = 5280
Width = 1455
End
Begin VB.Timer Timer1
Interval = 100
Left = 4200
Top = 4080
End
Begin VB.TextBox Text5
Enabled = 0 'False
Height = 270
Left = 3240
TabIndex = 7
Top = 4785
Width = 1575
End
Begin VB.TextBox Text4
Enabled = 0 'False
Height = 270
Left = 600
TabIndex = 6
Top = 4785
Width = 1575
End
Begin VB.PictureBox Picture1
BackColor = &H8000000E&
Height = 3255
Left = 0
ScaleHeight = 3195
ScaleWidth = 4875
TabIndex = 0
Top = 1320
Width = 4935
Begin VB.TextBox Text3
BorderStyle = 0 'None
Height = 270
Left = 4560
TabIndex = 3
Top = 2760
Width = 255
End
Begin VB.TextBox Text2
BorderStyle = 0 'None
Height = 270
Left = 2280
TabIndex = 2
Top = 600
Width = 255
End
Begin VB.TextBox Text1
BorderStyle = 0 'None
Height = 270
Left = 120
TabIndex = 1
Top = 2760
Width = 255
End
End
Begin MSCommLib.MSComm MSComm1
Left = 3600
Top = 0
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
CommPort = 3
DTREnable = -1 'True
InBufferSize = 1
InputLen = 1
RThreshold = 1
End
Begin MSComDlg.CommonDialog CD
Left = 3720
Top = 480
_ExtentX = 847
_ExtentY = 847
_Version = 393216
End
Begin VB.Label Label7
Caption = "第0个数据(max50)"
Height = 495
Left = 120
TabIndex = 14
Top = 5160
Width = 1335
End
Begin VB.Label Label6
Caption = "接受结果:"
Height = 375
Left = 0
TabIndex = 11
Top = 840
Width = 975
End
Begin VB.Label Label3
Caption = "接收数据:"
Height = 375
Left = 0
TabIndex = 10
Top = 240
Width = 975
End
Begin VB.Label Label5
Caption = "com口地址:"
Height = 375
Left = 2280
TabIndex = 9
Top = 4800
Width = 1095
End
Begin VB.Label Label4
Caption = "数据:"
Height = 375
Left = 120
TabIndex = 8
Top = 4800
Width = 735
End
Begin VB.Label Label2
Height = 375
Left = 960
TabIndex = 5
Top = 840
Width = 3255
End
Begin VB.Label Label1
Height = 375
Left = 960
TabIndex = 4
Top = 240
Width = 3255
End
End
Attribute VB_Name = "comv"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Dim dat(0 To 20) As Integer
Dim test
Dim xiaoshu, check1, check2, i, j, k, count, flat As Integer
Dim x, y, r, rad As Single
Dim n
Dim dL As New Collection
Dim CL As New Collection
Dim DRW As New clsLineGraph
Public Sub load()
Dim temp
Dim i, j As Integer
If count <> 0 Then
res = MsgBox("当前数据数否保存?", vbYesNo)
If res = 6 Then
Call store
End If
End If
CD.ShowOpen
i = 0
If CD.FileName <> "" Then
Open CD.FileName For Input As #1
Do While Not EOF(1)
For j = 1 To 3
Input #1, temp
If j = 1 Then
timenow(i) = temp
End If
If j = 2 Then
comad(i) = temp
End If
If j = 3 Then
data(i) = temp
i = i 1
End If
Next j
Loop
Close 1
count = i
Form1.Text4 = timenow(0)
Form1.Text5 = timenow(count - 1)
Form1.Text1 = maxdata()
Form1.Text2 = mindata()
Form1.Text3 = avdata()
MsgBox ("数据读取完成!")
Command2.Enabled = False
End If
End Sub
Public Sub store()
Dim str As String
j = 0
g = count
' Open "data.txt" For Append As #1
' Print #1, " 时间 " & " 地址 " & " 数据"
' Close 1
If count <> 0 Then
str = Format(Now, "yyyymmddhhmmss") & ".txt"
Open str For Append As #1
Do While j <> g
Print #1, timenow(j) & " " & comad(j) & " " & data(j)
j = j 1
Loop
Close 1
MsgBox ("完成数据储存!")
Else
MsgBox ("无数据,无法储存!")
End If
End Sub
Private Sub Command2_Click()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
Command2.Caption = "开始接收"
Else
Command2.Caption = "停止接收"
MSComm1.PortOpen = True
If count = 0 Then
Form1.Text4.Text = Format(Now, "yyyy-mm-dd-hh:mm:ss")
End If
Call Draw(x - r, y)
End If
End Sub
Private Sub Command3_Click()
Command2.Enabled = True
If count <> 0 Then
res = MsgBox("当前数据数否保存?", vbYesNo)
If res = 6 Then
Call store
End If
End If
count = 0
data(0) = 0
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Form1.Text1.Text = ""
Form1.Text2.Text = ""
Form1.Text3.Text = ""
End Sub
Private Sub MSComm1_OnComm()
temp = MSComm1.Input
'Private Function com(temp As Integer)
If temp = Chr(2) Then
Label1.Caption = "start "
Label2.Caption = " "
xiaoshu = 0
check1 = 0
check2 = 0
i = 1
Else
If temp = Chr(3) Then
Label1.Caption = Label1.Caption " over"
check1 = dat(1)
For j = 2 To 8
check1 = dat(j) Xor check1
Next j
check2 = ansii_hex(dat(9)) * 16 ansii_hex(dat(10))
If check1 = check2 Then
xiaoshu = dat(8) - 48
n = (dat(2) - 48) * 100 (dat(3) - 48) * 10 (dat(4) - 48) * 1 (dat(5) - 48) * 0.1 (dat(6) - 48) * 0.01 (dat(7) - 48) * 0.001
For j = 1 To 3 - xiaoshu
n = n * 10
Next j
angle = (n * 180 / 1000) * rad
If flat = 1 Then
a = n / 10
CL.Add Int(a)
DRW.Draw
Else
'n = Val(Mid(Label1.Caption, 3, 6))
'xiaoshu = Asc(Mid(Label1.Caption, 9, 1)) - 48
Call Draw(x - (r - 100) * Cos(angle), y - (r - 100) * Sin(angle))
End If
Label2.Caption = " 数据正确接收成功:" & n
Text4.Text = n
Text6.Text = n
data(count) = n
Form1.Text1 = maxdata()
Form1.Text2 = mindata()
Form1.Text3 = avdata()
Form1.Text5 = Format(Now, "yyyy-mm-dd-hh:mm:ss")
timenow(count) = Format(Now, "yyyymmddhhmmss")
comad(count) = MSComm1.CommPort
count = count 1
Label7.Caption = "第" & count & "个数据(max50)"
If count = 50 Then
res = MsgBox("系统预设保存数据个数(50)不足,是否将现有数据保存到文件,并开始新的采集?", vbYesNo)
If res = 6 Then
Call store
count = 0
Else
End
End If
End If
Else
Label2.Caption = "校验失败,数据有误"
End If
Text5.Text = MSComm1.CommPort
Else
dat(i) = Asc(temp)
i = i 1
Label1.Caption = Label1.Caption temp
End If
End If
End Sub
Public Function ansii_hex(a As Integer) As Integer
If a > 58 Then
ansii_hex = a - 65 10
Else
ansii_hex = a - 48
End If
End Function
Private Sub Command1_Click()
If flat = 0 Then
flat = 1
Set DRW.DataCollection = CL
Set DRW.PictureBox = Picture2
Do: CL.Add 0: Loop While CL.count < 200
DRW.GridColor = &H808000
DRW.GridVisible = True
Picture1.Visible = False
Picture2.Visible = True
Command1.Caption = "电压表显示"
DRW.PicBackground = vbBlack
DRW.PicForeground = vbGreen
For j = 0 To count - 1
CL.Add (data(j) / 10)
DRW.Draw
Next j
Else
flat = 0
Command1.Caption = "数据曲线显示"
Picture1.Visible = True
Picture2.Visible = False
Call Draw(x - r 100, y)
End If
End Sub
'Private Sub Timer1_Timer()
'If k <> 12 Then
'com (test(k))
'k = k 1
'Else
'Timer1.Enabled = False
'k = 0
'End If
'End Sub
Private Sub UserControl_Initialize()
Timer1.Enabled = False
x = Picture1.ScaleWidth / 2 Picture1.Left
y = Picture1.ScaleHeight Picture1.ScaleTop - 200
r = Picture1.ScaleWidth / 2 - 200
rad = Atn(1) / 45
flat = 0
count = 0
k = 0
test = Array(2, 43, 48, 49, 50, 51, 48, 49, 51, 49, 57, 3)
End Sub
Public Sub Draw(ByVal dx As Single, ByVal dy As Single)
Picture1.Cls
Picture1.Circle (x, y), r, vbBlack
Picture1.Line (x, y)-(dx, dy), B
Text1.Text = 0
Text2.Text = 500
Text3.Text = 1000
End Sub
Public Function maxdata() As Double
max = data(0)
For p = 1 To count - 1
If max < data(p) Then
max = data(p)
End If
Next p
maxdata = max
End Function
Public Function mindata() As Double
min = data(0)
For p = 1 To count - 1
If min > data(p) Then
min = data(p)
End If
Next p
mindata = min
End Function
Public Function avdata() As Double
con = data(0)
For p = 1 To count
con = con data(p)
Next p
av = con / (count 1)
avdata = av
End Function
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论