Private Sub Form_Load()
On Error Resume Next
Dim i As Long
ComBo1.Clear
For i = 1 To 20 '心理最大值
If CheckComIsIn(i) Then
ComBo1.AddItem "Com" & i
End If
Next i
End Sub
Private Function CheckComIsIn(ComIndex As Long) As Boolean
On Error GoTo MyError
CheckComIsIn = False
MSComm1.CommPort = ComIndex
MSComm1.PortOpen = True
MSComm1.PortOpen = False
CheckComIsIn = True
MyError:
End Function
我以为用combobox选择一下 然后点击“打开串口”按纽command1
Private Sub Command1_Click()
If Not MSComm1.PortOpen Then
MSComm1.CommPort = Combobox1.Text
MSComm1.PortOpen = True
End If
End Sub
Private Sub Form_Load()
For i = 1 To 3
MSComm1.CommPort = i
Combo1.AddItem i
Next
end sub
你们看行吗,就是少了出错处理啊。我只有串口1是有效的,如果是别的就会出错提示,
并提示重新选择并检测有效串口。
Private Sub VerifySettings(Index As Long)
Dim disposition As Long
Dim sTmp As String
Dim iX As Long
iX = Index
On Error GoTo ErrTrap
sSettings = comPort(iX).Settings
sPortNum = comPort(iX).CommPort
sSubKey = "Software\Damage Inc\ComPort Utility"
If RegOpenKeyEx(lMainKey, sSubKey, 0, KEY_READ, hnd) Then
If RegCreateKeyEx(lMainKey, sSubKey, 0, 0, 0, 0, 0, hnd, disposition) Then
Err.Raise 1001, "VerifySettings() Sub", "Could Not Create Registry Key"
End If
End If
'The Key Has Been Found/or Created, Now Check To See If Previous Settings Are Present
'Check For The Settings Subkey and Retrieve Value If Present, Then Set ComPort 'Settings' Property
sKeyValue = Space$(lLength) 'Pad The sKeyValue Variable
If RegQueryValueEx(hnd, sSettingsKey & Chr$(iX + 65), 0, REG_SZ, ByVal sKeyValue, lLength) Then '0 Return OK
If RegOpenKeyEx(lMainKey, sSubKey, 0, KEY_WRITE, hnd) Then '0 Return if Successful
Err.Raise 1001, "VerifySettings() Sub", "Could Not Open Registry Key"
Else 'The Value Was Not Present, Set To Default Port 'Settings' Property
If RegSetValueEx(hnd, sSettingsKey & Chr$(iX + 65), 0, REG_SZ, ByVal sSettings, Len(sSettings)) Then
Err.Raise 1001, "VerifySettings() Sub", "Could Not Set Registry Key Settings Value"
End If
End If
Else 'Read Value From Key And Set The Port 'Settings' Property To The Value In The Registry
comPort(iX).Settings = sKeyValue
End If
'Check For The Port Subkey and Retrieve Value If Present, Then Set ComPort 'Port' Property
sKeyValue = Space$(lLength) 'Pad The sKeyValue Variable
If RegQueryValueEx(hnd, sPortKey & Chr$(iX + 65), 0, REG_SZ, ByVal sKeyValue, lLength) Then '0 Return OK
If RegOpenKeyEx(lMainKey, sSubKey, 0, KEY_WRITE, hnd) Then '0 Return if Successful
Err.Raise 1001, "VerifySettings() Sub", "Could Not Open Registry Key"
Else 'The Value Was Not Present, Set To Default Port 'Port' Property
If RegSetValueEx(hnd, sPortKey & Chr$(iX + 65), 0, REG_SZ, ByVal sPortNum, Len(sPortNum)) Then
Err.Raise 1001, "VerifySettings() Sub", "Could Not Set Registry Key Port Value"
End If
End If
Else 'Read Value From Key And Set The Port 'Port' Property To The Value In The Registry
comPort(iX).CommPort = sKeyValue
End If
RegCloseKey hnd
Exit Sub
ErrTrap:
MsgBox Err.Number & " " & Err.Description & vbCr & " Error Generated By " & Err.Source, vbCritical, _
"System Error Trap !"
End Sub