如何检测系统串口个数?

canoegao 2003-10-20 09:28:33
如何检测系统串口个数? 求救!
...全文
144 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sss2002 2003-12-29
  • 打赏
  • 举报
回复
用串口查询控件

这里下载
http://www.refound.net/download/soft.asp?id=25
lirh 2003-10-27
  • 打赏
  • 举报
回复
studing....
webljf 2003-10-26
  • 打赏
  • 举报
回复
TRegistry *my=new TRegistry;
my->RootKey=HKEY_LOCAL_MACHINE;
my->OpenKey("HARDWARE\\DEVICEMAP\\SERIALCOMM",true);
COMlist->Sorted=false;
my->GetValueNames(COMlist->Items);
COMlist->Items->BeginUpdate();
for(int i=0;i<COMlist->Items->Count;i++)
COMlist->Items->Strings[i]=my->ReadString(COMlist->Items->Strings[i]);
COMlist->Items->EndUpdate();
COMlist->Sorted=true;

以上程序可以读取所有串口并加入到一个ComBox里
rainstormmaster 2003-10-26
  • 打赏
  • 举报
回复
用api函数实现的,一个按钮,一个listbox:
Option Explicit
Const HKEY_LOCAL_MACHINE = &H80000002
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As Any) As Long
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long

Private Sub Command1_Click()
Dim hKey As Long, Cnt As Long, sSave As String
RegOpenKey HKEY_LOCAL_MACHINE, "Hardware\DeviceMap\SerialComm", hKey
Cnt = 0
List1.Clear
Do
'Create a bufferHKEY_LOCAL_MACHINE\Hardware\DeviceMap\SerialComm
sSave = String(255, 0)
'enumerate the values
If RegEnumValue(hKey, Cnt, sSave, 255, 0, ByVal 0&, ByVal 0&, ByVal 0&) <> 0 Then Exit Do
'pritn the results to the form
List1.AddItem StripTerminator(sSave)
Cnt = Cnt + 1
Loop
'Close the registry
RegCloseKey hKey
End Sub

'This function is used to stripoff all the unnecessary chr$(0)'s
Private Function StripTerminator(sInput As String) As String
Dim ZeroPos As Integer
'Search the first chr$(0)
ZeroPos = InStr(1, sInput, vbNullChar)
If ZeroPos > 0 Then
StripTerminator = Left$(sInput, ZeroPos - 1)
Else
StripTerminator = sInput
End If
End Function

intocsdn 2003-10-21
  • 打赏
  • 举报
回复
学习ing
canoegao 2003-10-21
  • 打赏
  • 举报
回复
多谢,这样做的前提就是所有的串口都是关闭的。
有没有系统函数可以直接取得串口资源?
rainstormmaster 2003-10-20
  • 打赏
  • 举报
回复
用错误陷阱:
Private Sub Command2_Click()
On Error GoTo myerr
Dim i As Long
For i = 1 To 1000
Open "com" + CStr(i) For Binary As #1
Close #1
Next
Exit Sub
myerr:
MsgBox "共有" + CStr(i - 1) + "个串口"
End Sub

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧