?? 串口數據傳輸.frm
字號:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Form1
Caption = "串口數據傳輸"
ClientHeight = 2865
ClientLeft = 60
ClientTop = 510
ClientWidth = 7545
LinkTopic = "Form1"
ScaleHeight = 2865
ScaleWidth = 7545
StartUpPosition = 3 '窗口缺省
Begin MSCommLib.MSComm MSComm1
Left = 6000
Top = 480
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
End
Begin VB.TextBox Text2
Height = 495
Left = 1920
TabIndex = 3
Top = 1200
Width = 3375
End
Begin VB.TextBox Text1
Height = 495
Left = 1920
TabIndex = 2
Top = 360
Width = 2415
End
Begin VB.CommandButton Command2
Caption = "關閉窗口"
Height = 495
Left = 4320
TabIndex = 1
Top = 2040
Width = 1455
End
Begin VB.CommandButton Command1
Caption = "發送"
Height = 495
Left = 1440
TabIndex = 0
Top = 2040
Width = 1335
End
Begin VB.Label Label2
Caption = "接收數據"
Height = 255
Left = 360
TabIndex = 5
Top = 1320
Width = 1215
End
Begin VB.Label Label1
Caption = "發送數據"
Height = 255
Left = 360
TabIndex = 4
Top = 480
Width = 1095
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'本例中在發送數據欄中輸入一個0-255之間的整數,并單擊"發送"按鈕,單片機將接收到這個數據并顯示該數據,
'然后作除2處理,結果再經串口返回到PC機端.
Private Sub Command1_Click()
Dim Number As Integer '定義要發送的變量
Dim OutByte(0) As Byte '定義發送的字節數組
Number = Val(Text1.Text) '類型轉換
OutByte(0) = CByte(Number) '轉換為二進制
MSComm1.OutBuffer = 0 '清空發送緩沖區
MSComm1.Output = OutByte '發送數據
End Sub
Private Sub Command2_Click()
MSComm1.PortOpen = False '關閉串口
Unload Form1 '卸載窗體
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1 '使用1號串口
MSComm1.Settings = "9600,N,8,1" '設置初始化參數
MSComm1.PortOpen = True '打開串口
End Sub
Private Sub MSComm1_OnComm()
Dim InData As Variant '定義變體變量
Dim Arr(0) As Byte '定義接收字節數組
Select Case MSComm1.Input
Case comEvReceive '觸發接收事件
InData = MSComm1.Input '接收數據
Arr(0) = AscB(InData) '類型轉換
Text2.Text = Arr(0) '顯示接收數據
MSComm1.InBufferCount = 0 '清空接收緩沖區
End Select
End Sub
Private Sub Text1_Change()
End Sub
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -