怎么自动检测可用串口号?

lijuan1215_2009 2010-12-17 09:49:32
我在使用串口采集数据之前,想用程序直接能检测到我当前使用的串口号,这个怎么实现啊?谢谢。
...全文
1652 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
dsy963422098 2013-04-18
  • 打赏
  • 举报
回复
BOOL flag=Serialport.InitPort(this,1,19200,'N',8,1);//检测串口是否可用,初始化串口 if(flag==FALSE){ GetDlgItem(IDC_COM1)->EnableWindow(flag); } Serialport.ClosePort(); flag=Serialport.InitPort(this,2,19200,'N',8,1);//检测串口是否可用,初始化串口 if(flag==FALSE){ GetDlgItem(IDC_COM2)->EnableWindow(flag); } Serialport.ClosePort(); flag=Serialport.InitPort(this,3,19200,'N',8,1);//检测串口是否可用,初始化串口 if(flag==FALSE){ GetDlgItem(IDC_COM3)->EnableWindow(flag); } Serialport.ClosePort(); flag=Serialport.InitPort(this,4,19200,'N',8,1);//检测串口是否可用,初始化串口 if(flag==FALSE){ GetDlgItem(IDC_COM4)->EnableWindow(flag); } Serialport.ClosePort(); 这样的话,可用串口在界面上的RadioButton中是可以选择的否,不可用的就不能选择,这样是不是可以呢 ,VC程序哈
XuNuoChengZai 2012-07-29
  • 打赏
  • 举报
回复
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 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, ByVal lpData As String, lpcbData As Long) As Long
'Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_SZ = 1
Dim i&, ComStr$(), S
Private Sub Command1_Click()
MsgBox GetAllPort
End Sub

Public Function GetAllPort() As String
On Error Resume Next
S = GetSerialPort(HKEY_LOCAL_MACHINE, "HARDWARE\DEVICEMAP\SERIALCOMM")
If ComStr(0) = "" Then Exit Function
GetAllPort = ""
For i = 0 To UBound(S)
GetAllPort = GetAllPort & S(i) & ","
Next i
End Function

这个亲测,绝对可用,返回所有可用的serialport,声明是可以用public
菜田田 2010-12-28
  • 打赏
  • 举报
回复
就那几个串口,设定后不会变的,编程时都设置好了,按号就座,有必要检测么
stzhcn 2010-12-28
  • 打赏
  • 举报
回复
我也正需要,有高人明白吗?
jiashie 2010-12-17
  • 打赏
  • 举报
回复
逐个试
yachong 2010-12-17
  • 打赏
  • 举报
回复
逐个串口发送TEST指令,哪个串口收到了返回,说明哪个串口接了下位机。
前提是已经约定好了TEST指令。
lijuan1215_2009 2010-12-17
  • 打赏
  • 举报
回复

MSComm1.Output = "at " & Chr$(13)
if MSComm1.Input= "ok " then
debug.print "端口"
end if


这样是不是可以啊
咸清 2010-12-17
  • 打赏
  • 举报
回复
真郁闷,我真的没看懂~~
lijuan1215_2009 2010-12-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 of123 的回复:]
“想用程序直接能检测到我当前使用的串口号”是什么意思?
[/Quote]
我的上位机界面用VB做的,和下位机之间的通信是用串口。
我想在我的程序里加上自动找串口的功能,不用自己手动选串口号,就是连接上下位机之后,能知道上位机是使用的哪个串口
of123 2010-12-17
  • 打赏
  • 举报
回复
“想用程序直接能检测到我当前使用的串口号”是什么意思?
lijuan1215_2009 2010-12-17
  • 打赏
  • 举报
回复
没有人知道吗。我先自己回一个,帖子沉得好快啊
lijuan1215_2009 2010-12-17
  • 打赏
  • 举报
回复
我在com_test2()里面是一个一个试的,单步调试的时候,MSCcom1.input是接收到数据的,但是我运行程序就不对,是为什么啊?郁闷啊郁闷啊
Summer_King 2010-12-17
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 lijuan1215_2009 的回复:]

引用 9 楼 photel123456 的回复:
我正在使用的程序。
For a = 1 To 16
Comm.CommPort = a ’串口控件
Comm.PortOpen = True
If Comm.PortOpen = True Then
cboPort.AddItem Trim$(Str$(a)) '列表控件
Comm.PortOpen = False
End If
……
[/Quote]一个一个的试,直到某个口有数据到达,
lijuan1215_2009 2010-12-17
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 photel123456 的回复:]
我正在使用的程序。
For a = 1 To 16
Comm.CommPort = a ’串口控件
Comm.PortOpen = True
If Comm.PortOpen = True Then
cboPort.AddItem Trim$(Str$(a)) '列表控件
Comm.PortOpen = False
End If
Next
[/Quote]

这个只能找到可用的串口,我还需要知道已经连接上的是哪个串口。谢谢
lijuan1215_2009 2010-12-17
  • 打赏
  • 举报
回复

Private Sub com_test()
On Error Resume Next
For comp = 1 To 8
MSComm1.CommPort = comp
MSComm1.PortOpen = True
If MSComm1.PortOpen = True Then
portp(comp - 1) = comp
End If
MSComm1.PortOpen = False
Next
End Sub
Private Sub com_test2()

For comp = 0 To 7
If portp(comp) > 0 Then
MSComm1.CommPort = portp(comp)
MSComm1.Settings = "9600,N,8,1"
MSComm1.InputLen = 1024 '接收缓冲区的大小
MSComm1.InputMode = 1 '接收二进制型数据
MSComm1.PortOpen = True
bbb = MSComm1.Input
ffff = Len(bbb)
If ffff > 0 Then
Text11.Text = portp(comp)
End If
MSComm1.PortOpen = False
End If

Next comp


End Sub


我写了两个子程序,在form_load()里调用,并且下位机和电脑的com4是连接好的,下位机在不停地给上位机发数据。
为什么我单步调试,就可以,找到text11是等于4的。
我运行的时候就不对呢?
嗷嗷叫的老马 2010-12-17
  • 打赏
  • 举报
回复
串口本身是没有回应的,打开或关闭都是这样.

好比一个电灯线路,你能打开或关闭开关,却不能只通过操作这个开关,而得知灯座上是否有装一个电灯,以及它是否是好的.

为了知道有没有电灯及好坏情况,需要从别的角度得到一些反馈信息,比如测试电流情况,或者看看有没有光产生.

所以需要知道串口上设备的情况,然后以设备约定的格式发送一个已知回应的指令过去,如果在一定时间内有回应,说明插了这个设备的,如果超时了,说明没接或损坏.
lliai 2010-12-17
  • 打赏
  • 举报
回复
我正在使用的程序。
For a = 1 To 16
Comm.CommPort = a ’串口控件
Comm.PortOpen = True
If Comm.PortOpen = True Then
cboPort.AddItem Trim$(Str$(a)) '列表控件
Comm.PortOpen = False
End If
Next
cbm6666 2010-12-17
  • 打赏
  • 举报
回复
使用注册表列举 有效可用的端口号
'*********************************读取注册表串口值
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 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, ByVal lpData As String, lpcbData As Long) As Long
'Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_SZ = 1
Dim i&, ComStr$(), S
Private Sub Command1_Click()
MsgBox GetAllPort
End Sub

Public Function GetAllPort() As String
On Error Resume Next
S = GetSerialPort(HKEY_LOCAL_MACHINE, "HARDWARE\DEVICEMAP\SERIALCOMM")
If ComStr(0) = "" Then Exit Function
GetAllPort = ""
For i = 0 To UBound(S)
GetAllPort = GetAllPort & S(i) & ","
Next i
End Function

Public Function GetSerialPort(RegAddr&, Items$) As String()
On Error Resume Next
Dim hKey&, S1$, S2$, L&, L1&, J&
RegOpenKey RegAddr, Items, hKey
ReDim Preserve ComStr$(0)
ComStr(0) = "": i = 0: J = 0: Rtn = 0
Do
L = 1000: L1 = 1000
S1 = Space(L): S2 = Space(L)
Rtn = RegEnumValue(hKey, i, S1, L, 0, REG_SZ, S2, L1)
If Rtn = 0 Then
If InStr(S1, Chr(0)) > 0 And InStr(S2, Chr(0)) > 0 Then
S1 = UCase(Left(S1, InStr(S1, Chr(0)) - 1))
S2 = UCase(Left(S2, InStr(S2, Chr(0)) - 1))
If InStr(S2, "COM") > 0 Then
ReDim Preserve ComStr$(J)
ComStr(J) = S2
J = J + 1
End If
End If
End If
i = i + 1
Loop Until Rtn <> 0
GetSerialPort = ComStr()
End Function


'***************** 使用上面的代码 将检测到的可用端口号 循环检测
Private Sub Command1_Click()
On Error GoTo Errh
MSComm1.CommPort = 1 'MSComm1.CommPort = i
With MSComm1 '宣告MsComm的结构体
.Settings = "9600,N,8,1" '设定通讯协议 9600波特率,无奇偶校验,8位数据,一个停止位
.InputLen = 0 '设置Input一次从接收缓冲读取
.InBufferSize = 1 '设置缓冲区接收数据为1字节
.RThreshold = 1 '设置接收一个字节就产生OnComm事件
.InputMode = comInputModeText
.InBufferCount = 0 '缓冲区清空
End With
If Not MSComm1.PortOpen Then MSComm1.PortOpen = True: MsgBox "串口使用成功并已打开!"
Errh:
If Err > 0 Then MsgBox "串口使用中!"
End Sub

7,763

社区成员

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

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