?? 串行通信_數據傳輸_接收端f.frm
字號:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Form1
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "數據傳輸"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 3630
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 3630
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command2
Caption = "結 束"
Height = 375
Left = 960
TabIndex = 3
Top = 2640
Width = 1815
End
Begin VB.CommandButton Command1
Caption = "接 收"
Height = 375
Left = 960
TabIndex = 2
Top = 1800
Width = 1815
End
Begin VB.TextBox Text1
Appearance = 0 'Flat
Height = 975
Left = 120
TabIndex = 1
Text = "Text1"
Top = 600
Width = 3375
End
Begin MSCommLib.MSComm MSComm1
Left = 1680
Top = 2160
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
CommPort = 2
DTREnable = -1 'True
RTSEnable = -1 'True
End
Begin VB.Label Label1
Alignment = 2 'Center
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "接收數據區"
ForeColor = &H80000008&
Height = 255
Left = 840
TabIndex = 0
Top = 120
Width = 1815
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'接收數據
'在代碼中凡以****作為注釋的,在以后的實時通信工程中必須刪除
Option Explicit
Dim strOut() As Byte
Private Sub Form_Load()
MSComm1.CommPort = 2 '使用Com2作為通信端口
MSComm1.PortOpen = True '打開通信端口
MSComm1.InputMode = comInputModeBinary
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputLen = 0
MSComm1.RThreshold = 16
DoEvents
End Sub
Private Sub Command1_Click()
Dim strOut() As Byte
MSComm1.InputLen = 0 'InputLen確定讀取的字符數,為0則指全部
MSComm1.RThreshold = 1
'沒有連線時,緩沖區中沒有數據。如果有連線,輸入緩沖區將從端口取得數據
strOut = MSComm1.Input '讀緩沖區中的數據
'在真正應用串口通信時下面的這一條語句是不用的
' strOut = GetSetting("TestMSComm", "SEC1", "Key1") '****
Text1.Text = strOut
End Sub
Private Sub Command2_Click()
MSComm1.PortOpen = False '關閉通信端口
Unload Me
End Sub
'Private Sub MSComm_OnComm()
' Select Case MSComm1.CommEvent
' ' Handle each event or error by placing
' ' code below each case statement
'
'' 錯誤
' Case comEventBreak ' 收到 Break。
' Case comEventCDTO ' CD (RLSD) 超時。
' Case comEventCTSTO ' CTS Timeout。
' Case comEventDSRTO ' DSR Timeout。
' Case comEventFrame ' Framing Error
' Case comEventOverrun '數據丟失。
' Case comEventRxOver '接收緩沖區溢出。
' Case comEventRxParity ' Parity 錯誤。
' Case comEventTxFull '傳輸緩沖區已滿。
' Case comEventDCB '獲取 DCB] 時意外錯誤
'
' ' 事件
' Case comEvCD ' CD 線狀態變化。
' Case comEvCTS ' CTS 線狀態變化。
' Case comEvDSR ' DSR 線狀態變化。
' Case comEvRing ' Ring Indicator 變化。
' Case comEvReceive ' 收到 RThreshold # of chars.
' Case comEvSend ' 傳輸緩沖區有 Sthreshold 個字符 '
' '
' Case comEvEOF ' 輸入數據流中發現 EOF 字符
' '
' End Select
'End Sub
'
Private Sub MSComm1_OnComm()
Debug.Print MSComm1.InBufferCount
Dim aaa(100) As Byte
strOut = MSComm1.Input
Debug.Print "output=", strOut
Text1.Text = strOut(0)
Dim i As Integer
For i = 0 To 99
Debug.Print strOut(i)
Next i
If strOut(0) = 68 Then
MsgBox "ksfdka"
End If
End Sub
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -