界面如下:
代碼如下:
Private Sub Form_Load()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
Else
End If
Combo1.AddItem "COM1"
Combo1.AddItem "COM2"
Combo1.AddItem "COM3"
Combo1.AddItem "COM4"
Combo1.AddItem "COM5"
Combo1.AddItem "COM6"
Combo1.AddItem "COM7"
Combo1.AddItem "COM8"
Combo1.AddItem "COM9"
Combo1.AddItem "COM10"
Combo1.AddItem "COM11"
Combo1.AddItem "COM12"
Combo1.AddItem "COM13"
Combo1.AddItem "COM14"
Combo1.AddItem "COM15"
Combo1.AddItem "COM16"
Combo1.ListIndex = 2
Combo2.AddItem "256000"
Combo2.AddItem "128000"
Combo2.AddItem "115200"
Combo2.AddItem "57600"
Combo2.AddItem "38400"
Combo2.AddItem "28800"
Combo2.AddItem "19200"
Combo2.AddItem "14400"
Combo2.AddItem "12800"
Combo2.AddItem "11520"
Combo2.AddItem "9600"
Combo2.AddItem "4800"
Combo2.AddItem "2400"
Combo2.AddItem "1200"
Combo2.AddItem "600"
Combo3.AddItem "無None"
Combo3.AddItem "奇Odd"
Combo3.AddItem "偶Even"
Combo4.AddItem "4"
Combo4.AddItem "5"
Combo4.AddItem "6"
Combo4.AddItem "7"
Combo4.AddItem "8"
Combo5.AddItem "1"
Combo5.AddItem "2"
MSComm1.CommPort = Combo1.ListIndex + 1
MSComm1.Settings = "9600,n,8,1"
ComOpen.Caption = "打開串口"
Shape1.FillColor = &HFFFFC0
End Sub
Private Sub ComOpen_Click()
On Error GoTo uerror '發(fā)現(xiàn)錯誤跳轉到錯誤處理
If ComOpen.Caption = "關閉串口" Then
MSComm1.PortOpen = False
ComOpen.Caption = "打開串口" '按鈕文字改變
Shape1.FillColor = &HFFFFC0 '燈顏色改變
Else
MSComm1.PortOpen = True
ComOpen.Caption = "關閉串口"
Shape1.FillColor = &HFF
End If
Exit Sub
uerror:
msg$ = "無效端口號" '錯誤顯示
Title$ = "串口調試助手"
X = MsgBox(msg$, 48, Title$) '48標示顯示警告圖標
End Sub
Private Sub MSComm1_OnComm()
Dim BytReceived() As Byte
Dim strBuff As String
Dim i As Integer
Select Case MSComm1.CommEvent '事件發(fā)生
Case 2
Cls
MSComm1.InputLen = 0 '讀入緩沖區(qū)全部內(nèi)容
strBuff = MSComm1.Input '讀入到緩沖區(qū)
Label10.Caption = Label10.Caption + Len(strBuff) '接收計數(shù)
If MSComm1.InputMode = comInputModeBinary Then
BytReceived() = strBuff '如果是二進制接收模式則進行數(shù)據(jù)處理,否則直接顯示字符串
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i)) & " " '如果只有一個字符,則前補0,如F顯示0F,最后補空格
Else '方便顯示觀察如: 00 0F FE
strData = strData & Hex(BytReceived(i)) & " "
End If
Next
TextReceive = TextReceive & strData
strData = ""
Else
TextReceive = TextReceive & strBuff
End If
End Select
End Sub
Private Sub ComSend1_Click() '手動發(fā)送
Dim Temp(0) As Byte
Dim strBuff As String
If Option1.Value = True Then '如果顯示16進制發(fā)送則進行16進制處理 ,這里只發(fā)送一個
Temp(0) = "&H" & TextSend
MSComm1.Output = Temp '發(fā)送一個16進制
Else
strBuff = TextSend
End If
If MSComm1.PortOpen = False Then
MsgBox "請打開串口"
End If
On Error GoTo uerror
MSComm1.Output = strBuff
Label11.Caption = Label11.Caption + Len(strBuff) '發(fā)送計數(shù)
uerror:
End Sub
Private Sub ComSend2_Click()
If ComSend2.Caption = "自動發(fā)送" Then
ComSend2.Caption = "關閉自動發(fā)送"
Timer1.Interval = TextTime.Text
Timer1.Enabled = True
Else
ComSend2.Caption = "自動發(fā)送"
Timer1.Enabled = False
End If
End Sub
Private Sub ComClean1_Click()
TextSend.Text = "" '清空發(fā)送窗口
End Sub
Private Sub Option3_Click()
MSComm1.InputMode = comInputModeBinary '選擇接收方式
End Sub
Private Sub Option4_Click()
MSComm1.InputMode = comInputModeText '選擇接收方式
End Sub
Private Sub Timer1_Timer()
Call ComSend1_Click '定時調用手動發(fā)送
End Sub
Private Sub Timer2_Timer()
If Combo3 = "無None" Then
MSComm1.Settings = Str(Combo2) + "N" + Str(Combo4) + Str(Combo5)
ElseIf Combo3 = "奇Odd" Then
MSComm1.Settings = Str(Combo2) + "O" + Str(Combo4) + Str(Combo5)
ElseIf Combo3 = "偶Even" Then
MSComm1.Settings = Str(Combo2) + "E" + Str(Combo4) + Str(Combo5)
End If
End Sub
Private Sub Combo1_Click()
If MSComm1.PortOpen = True Then '如果串口打開先關閉后再進行其他操作
MSComm1.PortOpen = False
End If
MSComm1.CommPort = Combo1.ListIndex + 1 '讀取com口號
End Sub
Private Sub ComClean3_Click()
Label10.Caption = 0
Label11.Caption = 0
End Sub
Private Sub ComClean2_Click()
TextReceive.Text = "" '接收窗口
End Sub
功能上:實現(xiàn)了字符串的發(fā)送和接收,8位數(shù)據(jù)的十六進制發(fā)送和接收,有端口,波特率等設置。
缺點:
1:只可以發(fā)送兩位十六進制數(shù)。
2:接收和發(fā)送的顯示數(shù)據(jù)上有點不同步,十六進制的沒有算進去。
3:沒有TXT的文件發(fā)送和接收的功能。
如需要源程序的,可下載本文的圖片,下載后把圖片的.jpg格式改為.rar格式就可以解壓出來了。
附:MSComm控件的屬性:
屬性
說明
(Name)
MSComm控件的名稱
(自定義)
打開屬性頁
CommPort
獲得或設置通訊端口號
DTREnable
決定在通訊過程中是否使數(shù)據(jù)終端機狀態(tài)線有效。取值為:
True
False
EOFEnable
獲得或設置是否搜索EOF字符。取值為:
True
False
Handshaking
獲得或設置軟件的握手協(xié)議。取值為:
0 comNone
1 comXOnXoff
2 comRTS
3 comRTSXOnXOff
InBufferSize
獲得或設置接收緩沖區(qū)的大小,以字節(jié)數(shù)為單位。
Index
在對象數(shù)組中的編號
InputLen
獲得或設置輸入屬性從接收緩沖區(qū)讀出的字符數(shù)。
InputMode
獲得或設置輸入屬性檢索的數(shù)據(jù)類型。取值為:
0 comInputModeText
1 comInputModeBinary
Left
距離容器左邊框的距離
NullDiscard
決定是否將空字符串從端口傳送到接收緩沖區(qū)。取值為:
True
False
OutBufferSize
獲得或設置傳輸緩沖區(qū)中的字符數(shù)
ParityReplace
獲得或設置當出現(xiàn)奇偶校驗錯誤時,用來替換數(shù)據(jù)流中無效字符的字符。
RThreshold
獲得或設置要接受的字符數(shù)。
RTSEnable
決定能否使行有效。取值為:
True
False
Settings
獲得或設置波特率、奇偶校驗、數(shù)據(jù)位和停止位參數(shù)。
SThreshold
獲得或設置傳輸中所能允許的最小字符數(shù)
Tag
存儲程序所需的附加數(shù)據(jù)
Top
距容器頂部邊界的距離
聯(lián)系客服