Ⅰ 怎樣在vb中自動檢測到可用串口號的具體程序代碼
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,
0
GetCOMCount
=
cntCOM
End
Function
Private
Sub
Command1_Click()
MsgBox
"您的機器有
"
&
GetCOMCount
&
"
個串口。
",
vbOKOnly,
"串口數量
"
End
Sub
Ⅱ 用VB6如何偵測U盤的插入並阻止U盤的自動運行
C++寫成DLL,VB調用~
/*--------------------------U盤的安全防護----------------------------*/
/*------------------本程序簡單實現了U盤病毒免疫功能------------------*/
#include <windows.h>
#include "Shlwapi.h"
#include <Dbt.h>
#pragma comment(lib,"shlwapi.lib") //連入庫文件
//function
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);//窗口過程
//獲取盤符
TCHAR FirstDriveFromMask (ULONG unitmask);
//病毒從U盤啟動時用到的函數
BOOL SetFileAttrib(TCHAR *path);//設置path所指文件的屬性
//從C盤啟動時用到函數
BOOL CopyToUAndSet();//復制自己到U盤
//global variable
TCHAR U[2];//保存U盤的盤符
//constant
const TCHAR *szAutoRunFile="AutoRun.inf";
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[]=TEXT ("UUUUUU");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style =0;
wndclass.lpfnWndProc =WndProc;
wndclass.cbClsExtra =0;
wndclass.cbWndExtra =0;
wndclass.hInstance =hInstance;
wndclass.hIcon =0;
wndclass.hCursor =0;
wndclass.hbrBackground =0;
wndclass.lpszMenuName =NULL;
wndclass.lpszClassName =szAppName;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL,"窗口注冊失敗!", szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow (szAppName, "U盤的安全防護",
WS_DISABLED,
//WS_OVERLAPPEDWINDOW,
0,0,
0,0,
NULL, NULL, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
LRESULT OnDeviceChange(HWND hwnd,WPARAM wParam, LPARAM lParam)
{
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
switch(wParam)
{
case DBT_DEVICEARRIVAL: //插入
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
U[0]=FirstDriveFromMask(lpdbv -> dbcv_unitmask);//得到u盤盤符,即邏輯驅動器號
MessageBox(0,U,"Notice!",MB_OK);
CopyToUAndSet();//拷到u盤
}
break;
case DBT_DEVICEREMOVECOMPLETE: //設備刪除
break;
}
return LRESULT();
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_CREATE: //處理一些下面要用到的全局變數
U[1]=':';
return 0;
case WM_DEVICECHANGE: //檢測U盤插入拔出的消息映射
OnDeviceChange(hwnd,wParam,lParam);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
//將響應WM_DEVICECHANGE消息事件的內容(即驅動器掩碼)作為輸入,和系統定義的掩碼相比較,
//返回發生變化事件的驅動器的邏輯名稱
TCHAR FirstDriveFromMask(ULONG unitmask)
{
char i;
for (i = 0; i < 26; ++i)
{
if (unitmask & 0x1)//看該驅動器的狀態是否發生了變化
break;
unitmask = unitmask >> 1;
}
return (i + 'A');
}
//設置path所指文件的屬性
BOOL SetFileAttrib(TCHAR *path)
{
//設置該文件夾屬性為隱藏、只讀的
return SetFileAttributes(path,FILE_ATTRIBUTE_HIDDEN|
FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_SYSTEM);
}
BOOL CopyToUAndSet()
{
TCHAR szAutoFile[MAX_PATH];
lstrcpy(szAutoFile,U);
lstrcat(szAutoFile,"\\");
lstrcat(szAutoFile,szAutoRunFile);
if(!PathIsDirectory(szAutoFile))
{
CreateDirectory(szAutoFile,NULL);
SetFileAttrib(szAutoFile);
MessageBox(0,"U盤免疫成功!","U盤病毒免疫盒",MB_OK);
}
else
MessageBox(0,"此盤已進行了免疫!","U盤病毒免疫盒",MB_OK);
return FALSE;
}
Ⅲ VB 如何檢測一個程序是否在運行中
看是否運行:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Sub Command5_Click()
Dim lHwnd As Long
lHwnd = FindWindow(vbNullString, "程序的Title或Caption")
If lHwnd <> 0 Then
MsgBox "程序正在運行!"
End If
End Sub
向它發送指令:
AppActivate "程序的Title或Caption"
SendKeys "指令"
要啟動程序:
Call Shell("完整路徑和程序名稱.exe")
Ⅳ 怎樣在vb中自動檢測到可用串口號的具體程序代碼
j = 0x0dx0aFor i = 1 To 16 Step 1x0dx0aIf comISP.PortOpen = True Then '先關閉串口x0dx0acomISP.PortOpen = Falsex0dx0aEnd Ifx0dx0ax0dx0acomISP.CommPort = ix0dx0aOn Error Resume Next '說明當一個運行時錯誤發生時,控制項轉到緊接著發生錯誤的語句之後的語句,並在此繼續運行。訪問對象時要使用這種形式而不使用 On Error GoTo。x0dx0acomISP.PortOpen = Truex0dx0aIf Err.Number <> 8002 Then '無效的串口號。這樣可以檢測到虛擬串口,如果用Err.Number = 0的話檢測不到虛擬串口x0dx0aIf j = 0 Thenx0dx0aj = ix0dx0aEnd Ifx0dx0acboPort.AddItem "COM" & i '生成串口選擇列表x0dx0aEnd Ifx0dx0acomISP.PortOpen = Falsex0dx0aNext ix0dx0ax0dx0aIf j >= 1 Thenx0dx0acboPort.Text = "COM" & j '自動打開可用的最小串口號x0dx0acomISP.CommPort = jx0dx0acomISP.PortOpen = Truex0dx0acmdOpenCom.Caption = "關閉串口"x0dx0ashpCOM.FillColor = vbGreenx0dx0aIf Err.Number = 8005 Then '串口已打開,vbExclamation 'x0dx0acomISP.PortOpen = Falsex0dx0acboPort.Text = ""x0dx0acmdOpenCom.Caption = "打開串口"x0dx0ashpCOM.FillColor = vbRedx0dx0aEnd Ifx0dx0aEnd If
Ⅳ VB6.0中如何自動檢測有沒有連接上網路
這個可以用API函數來檢測,比如InternetGetConnetedStateEx這個函數,具體用法請問度娘
一打開軟體就檢測 這個可以把檢測的代碼放在窗體的Form_Load()事件里
Ⅵ 怎樣在vb中自動檢測到可用串口號的具體程序代碼
j = 0
For i = 1 To 16 Step 1
If comISP.PortOpen = True Then '先關閉串口
comISP.PortOpen = False
End If
comISP.CommPort = i
On Error Resume Next '說明當一個運行時錯誤發生時,控制項轉到緊接著發生錯誤的語句之後的語句,並在此繼續運行。訪問對象時要使用這種形式而不使用 On Error GoTo。
comISP.PortOpen = True
If Err.Number <> 8002 Then '無效的串口號。這樣可以檢測到虛擬串口,如果用Err.Number = 0的話檢測不到虛擬串口
If j = 0 Then
j = i
End If
cboPort.AddItem "COM" & i '生成串口選擇列表
End If
comISP.PortOpen = False
Next i
If j >= 1 Then
cboPort.Text = "COM" & j '自動打開可用的最小串口號
comISP.CommPort = j
comISP.PortOpen = True
cmdOpenCom.Caption = "關閉串口"
shpCOM.FillColor = vbGreen
If Err.Number = 8005 Then '串口已打開,vbExclamation '
comISP.PortOpen = False
cboPort.Text = ""
cmdOpenCom.Caption = "打開串口"
shpCOM.FillColor = vbRed
End If
End If
Ⅶ 如何用VB6.0編寫一段程序,能實現某個程序運行結束後(例如QQ.EXE運行結束),電腦自動關機.謝謝,重酬.
在模塊中加入如下代碼:
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32MoleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
'上面聲明常數 和 API函數
Function exitproc(ByVal exefile As String) As Boolean
exitproc = False '先將該函數過程賦值為假
Dim hSnapShot As Long, uProcess As PROCESSENTRY32
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&) '給系統進程創建快照 調用成功 則返回快照的句柄 調用失敗 返回INVAID_HANDLE_VALUE
uProcess.dwSize = Len(uProcess)
r = Process32First(hSnapShot, uProcess) '初始遍歷進程
Do While r
If Left$(uProcess.szExeFile, IIf(InStr(1, uProcess.szExeFile, Chr$(0)) > 0, InStr(1, uProcess.szExeFile, Chr$(0)) - 1, 0)) = exefile Then 'left截取null前的字元~也就是進程列表~再判斷是否有qq.exe
exitproc = True '該函數過程為真 即有此進程
Exit Do '退出循環
End If
r = Process32Next(hSnapShot, uProcess) '遍歷下一個進程
Loop
End Function
窗體代碼如下 一個timer控制項 一個按鈕(可要可不要)
Private Sub Command1_Click()
Timer1.Enabled = False '停止監控
End Sub
Private Sub Form_Load()
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
If exitproc("QQ.exe") Then '檢測QQ.exe進程是否存在
Else
x = MsgBox("15秒後關閉計算機", vbYesNo, "警告") '彈出對話框
Timer1.Enabled = False '先將timer控制項暫停監視
If x = vbYes Then '如果點擊對話框「是」按鈕 則執行關機
Shell "cmd /c shutdown -s -t 15", 0
Else
Timer1.Enabled = True '如果點 」否「則繼續監控
End If
End If
End Sub
Ⅷ 用VB做一個超聲波檢測成像的程序
首先,你需要測試單片機通訊的所有操作步驟,保證單片機通訊正常,此步驟通過串口調試工具實現(如串口助手、串口大師等等,網上很多),確認發送的指令及接受的返回數據正常。
然後你就可以開始編程實現手動的功能,包括發送查詢指令,返回數據的接收處理(比如搞清楚傳回的圖像如何轉化為點陣圖之類的協議問題)。
功能全部實現後,根據情況選擇增加一部分意外處理,比如通訊中斷、返回數據無效等,這樣就算完成了。