扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
using System.Net;
创新互联建站于2013年成立,是专业互联网技术服务公司,拥有项目成都网站建设、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元新华做网站,已为上家服务,为新华各地企业和个人服务,联系电话:028-86922220
if(!string.IsNullOrEmpty(txtPort.Text))
{
IPAddress ip = IPAddress.Parse(txtIp.Text);
IPEndPoint point=new IPEndPoint(ip,int.Parse(txtPort.Text));
try
{
TcpClient tcp=new TcpClient();
tcp.Connect(point);
MessageBox.Show("端口打开");
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
通过以上这个方法,只能判断你是否能链接这个端口。
通过以下这个方法,能判断端口是否开放
public static void GetTcpConnections()
{
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
string str="";
foreach (TcpConnectionInformation t in connections)
{
str+="Local endpoint:"+ t.LocalEndPoint.ToString()+",";
str+="Remote endpoint:"+ t.RemoteEndPoint.ToString()+",";
str+=t.State.ToString()+",";
}
MessageBox.Show(str);
}
vb.net的话
For
Each
sp
As
String
In
My.Computer.Ports.SerialPortNames
cbxport.Items.Add(sp)
Next
vb6.0的话要调用API查看串口相关信息存在的注册表。
HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
通过读注册表的方法获得串口数量,当然也可以获得串口号了。
Option
Explicit
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
RegQueryInfoKey
Lib
"advapi32.dll
"
Alias
"RegQueryInfoKeyA
"
(ByVal
hKey
As
Long,
ByVal
lpClass
As
String,
lpcbClass
As
Long,
ByVal
lpReserved
As
Long,
lpcSubKeys
As
Long,
lpcbMaxSubKeyLen
As
Long,
lpcbMaxClassLen
As
Long,
lpcValues
As
Long,
lpcbMaxValueNameLen
As
Long,
lpcbMaxValueLen
As
Long,
lpcbSecurityDescriptor
As
Long,
lpftLastWriteTime
As
Long)
As
Long
Private
Const
HKEY_LOCAL_MACHINE
=
H80000002
'
获得当前系统的
COM
口的数量
Function
GetCOMCount()
As
Integer
Dim
ret
As
Long,
cntCOM
As
Long
RegOpenKey
HKEY_LOCAL_MACHINE,
"HARDWARE\DEVICEMAP\SERIALCOMM
",
ret
RegQueryInfoKey
ret,
"
",
0,
0,
0,
0,
0,
cntCOM,
0,
0,
0,
GetCOMCount
=
cntCOM
End
Function
Private
Sub
Command1_Click()
MsgBox
"您的机器有
"
GetCOMCount
"
个串口。
",
vbOKOnly,
"串口数量
"
End
Sub
如下是检测串口是否存在,是否被占用的代码:
Option Explicit
Dim a As Integer
Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 1 '产生MSComm事件
MSComm1.InBufferSize = 1024
Shape1.FillStyle = 0
OpenPortNum
End Sub
Private Sub OpenPortNum()
On Error GoTo uerror
For a = 1 To 16
MSComm1.CommPort = a
MSComm1.PortOpen = True
If MSComm1.PortOpen = True Then
Print "可用Com号= "; a
MSComm1.PortOpen = False
End If
Next
Exit Sub
uerror:
Print "出错或占用Com号= "; a
Resume Next
End Sub
如要探测某个COM口是否有数据输入则需在打开某个串口后,用OnComm事件来接收.
打印机端口查看方法如下:
1、进入“控制面板”-“设备和打印机”;
2、右键需要查看的打印机,选择“属性”;
3、选择”Web服务“,即可查看IP端口。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流