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端口。
文章名稱:vb.net端口檢測 端口 檢測
分享地址:http://vcdvsql.cn/article26/doisscg.html
成都網站建設公司_創新互聯,為您提供軟件開發、動態網站、外貿網站建設、App設計、微信小程序、移動網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯