写了一个VB的串口程序,用来调试指南针和加速度的。本人第一次写VB的程序。在网上找了两天,自己边写边找,总算是弄出了一个东西。但是目前的问题是上位机的程序感觉响应的速度很慢。不知道什么原因。请大侠指教。程序和界面如下。
Dim mouse As Boolean
Dim uart_len As Byte
Dim REC_DATA(12) As String
Dim MAX_AX, MAX_AY, MAX_AZ, MIN_AX, MIN_AY, MIN_AZ As Single
Dim MAX_HX, MAX_HY, MAX_HZ, MIN_HX, MIN_HY, MIN_HZ As Single
Private Sub Combo1_Click()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
Command1.Caption = "打开串口"
End If
If Combo1.Text = "COM1" Then
MSComm1.CommPort = 1
ElseIf Combo1.Text = "COM2" Then
MSComm1.CommPort = 2
ElseIf Combo1.Text = "COM3" Then
MSComm1.CommPort = 3
ElseIf Combo1.Text = "COM4" Then
MSComm1.CommPort = 4
End If
End Sub
Private Sub Combo2_Click()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
Command1.Caption = "打开串口"
End If
If Combo2.Text = "4800" Then
MSComm1.Settings = "4800,n,8,1"
ElseIf Combo1.Text = "9600" Then
MSComm1.Settings = "9600,n,8,1"
ElseIf Combo1.Text = "19200" Then
MSComm1.Settings = "19200,n,8,1"
End If
End Sub
Private Sub Command1_Click()
If Combo1.Text = "COM4" And Command1.Caption = "打开串口" Then
MSComm1.PortOpen = True
Command1.Caption = "关闭串口"
ElseIf Combo1.Text = "COM4" And Command1.Caption = "关闭串口" Then
MSComm1.PortOpen = False
Command1.Caption = "打开串口"
End If
End Sub
Private Sub Form_Load()
Dim DR_X, DR_Y As Single
MSComm1.CommPort = 4
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary
MSComm1.InputLen = 1
MSComm1.InBufferSize = 1
MSComm1.RThreshold = 1
'MSComm1.PortOpen = True
Command1.Caption = "打开串口"
Combo1.AddItem "COM1"
Combo1.AddItem "COM2"
Combo1.AddItem "COM3"
Combo1.AddItem "COM4"
Combo2.AddItem "4800"
Combo2.AddItem "9600"
Combo2.AddItem "19200"
Combo1.Text = "COM4"
Combo2.Text = "9600"
Label_X.Caption = "AX"
Label_Y.Caption = "AY"
Text1(0).Text = MAX_AX
Text1(1).Text = MIN_AX
Text1(2).Text = MAX_HX
Text1(3).Text = MIN_HX
Text2(0).Text = MAX_AY
Text2(1).Text = MIN_AY
Text2(2).Text = MAX_HY
Text2(3).Text = MIN_HY
Text3(0).Text = MAX_AZ
Text3(1).Text = MIN_AZ
Text3(2).Text = MAX_HZ
Text3(3).Text = MIN_HZ
For DR_X = 0 To 15000 Step 1
Picture1.PSet (DR_X, 5000), RGB(255, 0, 0)
Next DR_X
For DR_Y = 0 To 10000 Step 1
Picture1.PSet (7500, DR_Y), RGB(255, 0, 0)
Next DR_Y
For DR_Y = 0 To 4400 Step 400 '画Y轴坐标
For DR_X = 7500 To 7550 Step 1
Picture1.PSet (DR_X, 5400 + DR_Y), RGB(255, 0, 0)
Next DR_X
Next DR_Y
For DR_Y = 0 To 4400 Step 400
For DR_X = 7500 To 7550 Step 1
Picture1.PSet (DR_X, 4600 - DR_Y), RGB(255, 0, 0)
Next DR_X
Next DR_Y
For DR_X = 0 To 7200 Step 400 '画X轴坐标
For DR_Y = 4950 To 5000 Step 1
Picture1.PSet (7500 + DR_X, DR_Y), RGB(255, 0, 0)
Next DR_Y
Next DR_X
For DR_X = 0 To 7200 Step 400
For DR_Y = 4950 To 5000 Step 1
Picture1.PSet (7500 - DR_X, DR_Y), RGB(255, 0, 0)
Next DR_Y
Next DR_X
End Sub
Private Sub MSComm1_OnComm()
Dim i As Byte
Dim BIT_REC_OVER As Boolean
Dim DATA_AX, DATA_AY, DATA_AZ As Single
Dim DATA_HX, DATA_HY, DATA_HZ As Single
Dim DISPLAY_X, DISPLAY_Y As Single
Dim aa As String
Select Case MSComm1.CommEvent
Case comEvReceive
aa = MSComm1.Input
aa = AscB(aa)
'Text4.Text = Text4.Text + aa
If aa = "2" And uart_len = 0 Then
REC_DATA(uart_len) = aa
uart_len = uart_len + 1
ElseIf uart_len <> 0 And aa <> "2" Then
REC_DATA(uart_len) = aa
uart_len = uart_len + 1
'Text1(0).Text = uart_len
If uart_len = 13 Then
uart_len = 0
BIT_REC_OVER = True
End If
ElseIf aa = "2" And uart_len > 0 Then
uart_len = 0
Else
uart_len = 0
BIT_REC_OVER = False
End If
End Select
If BIT_REC_OVER = True Then
If REC_DATA(1) >= "16" And REC_DATA(1) < "32" Then
'AX
DATA_AX = ((REC_DATA(1) And &HF) * (2 ^ 12))
DATA_AX = (DATA_AX + ((REC_DATA(2) And &HF) * (2 ^ 8)))
DATA_AX = (DATA_AX + ((REC_DATA(3) And &HF) * (2 ^ 4)))
DATA_AX = (DATA_AX + ((REC_DATA(4) And &HF)))
'Text1(0).Text = DATA_AX
If DATA_AX > 32767 Then
DATA_AX = DATA_AX - 65535
End If
If DATA_AX > MAX_AX Then
MAX_AX = DATA_AX
Text1(0).Text = MAX_AX
End If
If DATA_AX < MIN_AX Then
MIN_AX = DATA_AX
Text1(1).Text = MIN_AX
End If
'Case &H20 'AY
DATA_AY = ((REC_DATA(5) And &HF) * (2 ^ 12))
DATA_AY = DATA_AY + ((REC_DATA(6) And &HF) * (2 ^ 8))
DATA_AY = DATA_AY + ((REC_DATA(7) And &HF) * (2 ^ 4))
DATA_AY = DATA_AY + ((REC_DATA(8) And &HF))
If DATA_AY > 32767 Then
DATA_AY = DATA_AY - 65535
End If
If DATA_AY > MAX_AY Then
MAX_AY = DATA_AY
Text2(0).Text = MAX_AY
End If
If DATA_AY < MIN_AY Then
MIN_AY = DATA_AY
Text2(1).Text = MIN_AY
End If
' Case &H30 'AZ
DATA_AZ = ((REC_DATA(9) And &HF) * (2 ^ 12))
DATA_AZ = DATA_AZ + ((REC_DATA(10) And &HF) * (2 ^ 8))
DATA_AZ = DATA_AZ + ((REC_DATA(11) And &HF) * (2 ^ 4))
DATA_AZ = DATA_AZ + ((REC_DATA(12) And &HF))
If DATA_AZ > 32767 Then
DATA_AZ = DATA_AZ - 65535
End If
If DATA_AZ > MAX_AZ Then
MAX_AZ = DATA_AZ
Text3(0).Text = MAX_AZ
End If
If DATA_AZ < MIN_AZ Then
MIN_AZ = DATA_AZ
Text3(1).Text = MIN_AZ
End If
ElseIf REC_DATA(1) >= "64" And REC_DATA(1) < "80" Then 'HX
DATA_HX = ((REC_DATA(1) And &HF) * (2 ^ 12))
DATA_HX = DATA_HX + ((REC_DATA(2) And &HF) * (2 ^ 8))
DATA_HX = DATA_HX + ((REC_DATA(3) And &HF) * (2 ^ 4))
DATA_HX = DATA_HX + ((REC_DATA(4) And &HF))
If DATA_HX > 32767 Then
DATA_HX = DATA_HX - 65535
End If
If DATA_HX > MAX_HX Then
MAX_HX = DATA_HX
Text1(2).Text = MAX_HX
End If
If DATA_HX < MIN_HX Then
MIN_HX = DATA_HX
Text1(3).Text = MIN_HX
End If
'Case &H50 'HY
DATA_HY = ((REC_DATA(5) And &HF) * (2 ^ 12))
DATA_HY = DATA_HY + ((REC_DATA(6) And &HF) * (2 ^ 8))
DATA_HY = DATA_HY + ((REC_DATA(7) And &HF) * (2 ^ 4))
DATA_HY = DATA_HY + ((REC_DATA(8) And &HF))
If DATA_HY > 32767 Then
DATA_HY = DATA_HY - 65535
End If
If DATA_HY > MAX_HY Then
MAX_HY = DATA_HY
Text2(2).Text = MAX_HY
End If
If DATA_HY < MIN_HY Then
MIN_HY = DATA_HY
Text2(3).Text = MIN_HY
End If
' Case &H60 'HZ
DATA_HZ = ((REC_DATA(9) And &HF) * (2 ^ 12))
DATA_HZ = DATA_HZ + ((REC_DATA(10) And &HF) * (2 ^ 8))
DATA_HZ = DATA_HZ + ((REC_DATA(11) And &HF) * (2 ^ 4))
DATA_HZ = DATA_HZ + ((REC_DATA(12) And &HF))
If DATA_HZ > 32767 Then
DATA_HZ = DATA_HZ - 65535
End If
If DATA_HZ > MAX_HZ Then
MAX_HZ = DATA_HZ
Text3(2).Text = MAX_HZ
End If
If DATA_HZ < MIN_HZ Then
MIN_HZ = DATA_HZ
Text3(3).Text = MIN_HZ
End If
End If
If Option1.Value = True Then 'AX-AY
DISPLAY_X = 7500 + (DATA_AX * 4)
DISPLAY_Y = 5000 - (DATA_AY * 4)
ElseIf Option2.Value = True Then 'AX-AZ
DISPLAY_X = 7500 + (DATA_AX * 4)
DISPLAY_Y = 5000 - (DATA_AZ * 4)
ElseIf Option3.Value = True Then 'AY-AZ
DISPLAY_X = 7500 + (DATA_AY * 4)
DISPLAY_Y = 5000 - (DATA_AZ * 4)
ElseIf Option4.Value = True Then 'HX-HY
DISPLAY_X = 7500 + (DATA_HX * 4)
DISPLAY_Y = 5000 - (DATA_HY * 4)
ElseIf Option5.Value = True Then 'HX-HZ
DISPLAY_X = 7500 + (DATA_HX * 4)
DISPLAY_Y = 5000 - (DATA_HZ * 4)
ElseIf Option6.Value = True Then 'HY-HZ
DISPLAY_X = 7500 + (DATA_HY * 4)
DISPLAY_Y = 5000 - (DATA_HZ * 4)
ElseIf Option7.Value = True Then
End If
BIT_REC_OVER = False
Picture1.PSet (DISPLAY_X, DISPLAY_Y), RGB(0, 0, 255)
End If
End Sub
Private Sub Option1_Click()
Label_X.Caption = "AX"
Label_Y.Caption = "AY"
Picture1.Picture = Nothing
Picture1.Picture = LoadPicture("")
For DR_X = 0 To 15000 Step 1
Picture1.PSet (DR_X, 5000), RGB(255, 0, 0)
Next DR_X
For DR_Y = 0 To 10000 Step 1
Picture1.PSet (7500, DR_Y), RGB(255, 0, 0)
Next DR_Y
For DR_Y = 0 To 4400 Step 400 '画Y轴坐标
For DR_X = 7500 To 7550 Step 1
Picture1.PSet (DR_X, 5400 + DR_Y), RGB(255, 0, 0)
Next DR_X
Next DR_Y
For DR_Y = 0 To 4400 Step 400
For DR_X = 7500 To 7550 Step 1
Picture1.PSet (DR_X, 4600 - DR_Y), RGB(255, 0, 0)
Next DR_X
Next DR_Y
For DR_X = 0 To 7200 Step 400 '画X轴坐标
For DR_Y = 4950 To 5000 Step 1
Picture1.PSet (7500 + DR_X, DR_Y), RGB(255, 0, 0)
Next DR_Y
Next DR_X
For DR_X = 0 To 7200 Step 400
For DR_Y = 4950 To 5000 Step 1
Picture1.PSet (7500 - DR_X, DR_Y), RGB(255, 0, 0)
Next DR_Y
Next DR_X
End Sub
Private Sub Option2_Click()
Label_X.Caption = "AX"
Label_Y.Caption = "AZ"
Picture1.Picture = Nothing
Picture1.Picture = LoadPicture("")
For DR_X = 0 To 15000 Step 1
Picture1.PSet (DR_X, 5000), RGB(255, 0, 0)
Next DR_X
For DR_Y = 0 To 10000 Step 1
Picture1.PSet (7500, DR_Y), RGB(255, 0, 0)
Next DR_Y
For DR_Y = 0 To 4400 Step 400 '画Y轴坐标
For DR_X = 7500 To 7550 Step 1
Picture1.PSet (DR_X, 5400 + DR_Y), RGB(255, 0, 0)
Next DR_X
Next DR_Y
For DR_Y = 0 To 4400 Step 400
For DR_X = 7500 To 7550 Step 1
Picture1.PSet (DR_X, 4600 - DR_Y), RGB(255, 0, 0)
Next DR_X
Next DR_Y
For DR_X = 0 To 7200 Step 400 '画X轴坐标
For DR_Y = 4950 To 5000 Step 1
Picture1.PSet (7500 + DR_X, DR_Y), RGB(255, 0, 0)
Next DR_Y
Next DR_X
For DR_X = 0 To 7200 Step 400
For DR_Y = 4950 To 5000 Step 1
Picture1.PSet (7500 - DR_X, DR_Y), RGB(255, 0, 0)
Next DR_Y
Next DR_X
End Sub
Private Sub Option3_Click()
Label_X.Caption = "AY"
Label_Y.Caption = "AZ"
Picture1.Picture = Nothing
Picture1.Picture = LoadPicture("")
For DR_X = 0 To 15000 Step 1
Picture1.PSet (DR_X, 5000), RGB(255, 0, 0)
Next DR_X
For DR_Y = 0 To 10000 Step 1
Picture1.PSet (7500, DR_Y), RGB(255, 0, 0)
Next DR_Y
For DR_Y = 0 To 4400 Step 400 '画Y轴坐标
For DR_X = 7500 To 7550 Step 1
Picture1.PSet (DR_X, 5400 + DR_Y), RGB(255, 0, 0)
Next DR_X
Next DR_Y
For DR_Y = 0 To 4400 Step 400
For DR_X = 7500 To 7550 Step 1
Picture1.PSet (DR_X, 4600 - DR_Y), RGB(255, 0, 0)
Next DR_X
Next DR_Y
For DR_X = 0 To 7200 Step 400 '画X轴坐标
For DR_Y = 4950 To 5000 Step 1
Picture1.PSet (7500 + DR_X, DR_Y), RGB(255, 0, 0)
Next DR_Y
Next DR_X
For DR_X = 0 To 7200 Step 400
For DR_Y = 4950 To 5000 Step 1
Picture1.PSet (7500 - DR_X, DR_Y), RGB(255, 0, 0)
Next DR_Y
Next DR_X
End Sub
Private Sub Option4_Click()
Label_X.Caption = "HX"
Label_Y.Caption = "HY"
Picture1.Picture = Nothing
Picture1.Picture = LoadPicture("")
For DR_X = 0 To 15000 Step 1
Picture1.PSet (DR_X, 5000), RGB(255, 0, 0)
Next DR_X
For DR_Y = 0 To 10000 Step 1
Picture1.PSet (7500, DR_Y), RGB(255, 0, 0)
Next DR_Y
For DR_Y = 0 To 4400 Step 400 '画Y轴坐标
For DR_X = 7500 To 7550 Step 1
Picture1.PSet (DR_X, 5400 + DR_Y), RGB(255, 0, 0)
Next DR_X
Next DR_Y
For DR_Y = 0 To 4400 Step 400
For DR_X = 7500 To 7550 Step 1
Picture1.PSet (DR_X, 4600 - DR_Y), RGB(255, 0, 0)
Next DR_X
Next DR_Y
For DR_X = 0 To 7200 Step 400 '画X轴坐标
For DR_Y = 4950 To 5000 Step 1
Picture1.PSet (7500 + DR_X, DR_Y), RGB(255, 0, 0)
Next DR_Y
Next DR_X
For DR_X = 0 To 7200 Step 400
For DR_Y = 4950 To 5000 Step 1
Picture1.PSet (7500 - DR_X, DR_Y), RGB(255, 0, 0)
Next DR_Y
Next DR_X
End Sub
Private Sub Option5_Click()
Label_X.Caption = "HX"
Label_Y.Caption = "HZ"
Picture1.Picture = Nothing
Picture1.Picture = LoadPicture("")
For DR_X = 0 To 15000 Step 1
Picture1.PSet (DR_X, 5000), RGB(255, 0, 0)
Next DR_X
For DR_Y = 0 To 10000 Step 1
Picture1.PSet (7500, DR_Y), RGB(255, 0, 0)
Next DR_Y
For DR_Y = 0 To 4400 Step 400 '画Y轴坐标
For DR_X = 7500 To 7550 Step 1
Picture1.PSet (DR_X, 5400 + DR_Y), RGB(255, 0, 0)
Next DR_X
Next DR_Y
For DR_Y = 0 To 4400 Step 400
For DR_X = 7500 To 7550 Step 1
Picture1.PSet (DR_X, 4600 - DR_Y), RGB(255, 0, 0)
Next DR_X
Next DR_Y
For DR_X = 0 To 7200 Step 400 '画X轴坐标
For DR_Y = 4950 To 5000 Step 1
Picture1.PSet (7500 + DR_X, DR_Y), RGB(255, 0, 0)
Next DR_Y
Next DR_X
For DR_X = 0 To 7200 Step 400
For DR_Y = 4950 To 5000 Step 1
Picture1.PSet (7500 - DR_X, DR_Y), RGB(255, 0, 0)
Next DR_Y
Next DR_X
End Sub
Private Sub Option6_Click()
Label_X.Caption = "HY"
Label_Y.Caption = "HZ"
Picture1.Picture = Nothing
Picture1.Picture = LoadPicture("")
For DR_X = 0 To 15000 Step 1
Picture1.PSet (DR_X, 5000), RGB(255, 0, 0)
Next DR_X
For DR_Y = 0 To 10000 Step 1
Picture1.PSet (7500, DR_Y), RGB(255, 0, 0)
Next DR_Y
For DR_Y = 0 To 4400 Step 400 '画Y轴坐标
For DR_X = 7500 To 7550 Step 1
Picture1.PSet (DR_X, 5400 + DR_Y), RGB(255, 0, 0)
Next DR_X
Next DR_Y
For DR_Y = 0 To 4400 Step 400
For DR_X = 7500 To 7550 Step 1
Picture1.PSet (DR_X, 4600 - DR_Y), RGB(255, 0, 0)
Next DR_X
Next DR_Y
For DR_X = 0 To 7200 Step 400 '画X轴坐标
For DR_Y = 4950 To 5000 Step 1
Picture1.PSet (7500 + DR_X, DR_Y), RGB(255, 0, 0)
Next DR_Y
Next DR_X
For DR_X = 0 To 7200 Step 400
For DR_Y = 4950 To 5000 Step 1
Picture1.PSet (7500 - DR_X, DR_Y), RGB(255, 0, 0)
Next DR_Y
Next DR_X
End Sub
Private Sub Option7_Click()
Label_X.Caption = "X"
Label_Y.Caption = "Y"
Picture1.Picture = Nothing
Picture1.Picture = LoadPicture("")
For DR_X = 0 To 15000 Step 1
Picture1.PSet (DR_X, 5000), RGB(255, 0, 0)
Next DR_X
For DR_Y = 0 To 10000 Step 1
Picture1.PSet (7500, DR_Y), RGB(255, 0, 0)
Next DR_Y
For DR_Y = 0 To 4400 Step 400 '画Y轴坐标
For DR_X = 7500 To 7550 Step 1
Picture1.PSet (DR_X, 5400 + DR_Y), RGB(255, 0, 0)
Next DR_X
Next DR_Y
For DR_Y = 0 To 4400 Step 400
For DR_X = 7500 To 7550 Step 1
Picture1.PSet (DR_X, 4600 - DR_Y), RGB(255, 0, 0)
Next DR_X
Next DR_Y
For DR_X = 0 To 7200 Step 400 '画X轴坐标
For DR_Y = 4950 To 5000 Step 1
Picture1.PSet (7500 + DR_X, DR_Y), RGB(255, 0, 0)
Next DR_Y
Next DR_X
For DR_X = 0 To 7200 Step 400
For DR_Y = 4950 To 5000 Step 1
Picture1.PSet (7500 - DR_X, DR_Y), RGB(255, 0, 0)
Next DR_Y
Next DR_X
End Sub
(原文件名:QQ截图20111029110302.jpg)
一周热门 更多>