批处理:修改COM端口号
發(fā)現(xiàn)萬能的WMI居然沒有實現(xiàn)修改COM端口號的方法,不過用來遍歷端口信息還是可以的,參考http://msdn.microsoft.com/en-us/library/aa394413(v=vs.85).aspx。 沒有辦法只能通過修改注冊表的方式來實現(xiàn),下面獻上代碼,自己看吧,里面有用到的2個utilities都是微軟的工具:
?
COMPortNumberChanger.bat:
?
setlocal EnableDelayedExpansion @echo off set oldCOMPort=%~1 set newCOMPort=%~2 :ReadDeviceId for /f %%a in ('.\tools\devcon findall =port * ^| find /i "%oldCOMPort%"') do set DEVICEID=%%a if "%DEVICEID%" == "" ( echo Error: Cannot find device id for %oldCOMPort%, please check if device is installed correctly and try again. echo Press any key to detect again. pause goto ReadDeviceId ):ReadFriendlyName for /f "skip=4 tokens=1,2,*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\%DEVICEID%" /v FriendlyName') do set oldFriendlyName="%%c" if %oldFriendlyName%=="" ( echo Error: Cannot find friendly name for %oldCOMPort%, check regedit: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\%DEVICEID%. Check if device is installed correctly and try again. echo Press any key to detect again. pause goto ReadFriendlyName ):FindDeviceMap for /f "skip=4 tokens=1,2,3" %%a in ('reg query "HKLM\HARDWARE\DEVICEMAP\SERIALCOMM" /s') do ( if /i "%oldCOMPort%" == "%%c" ( set SerialCommKey="%%a" ) )if %SerialCommKey%=="" ( echo Error: cannot find Device map for %oldCOMPort%, check regedit: HKLM\HARDWARE\DEVICEMAP\SERIALCOMM. Check if device is installed correctly and try again. echo Press any key to detect again. pause goto FindDeviceMap )devcon disable @"%DEVICEID%"subinacl /keyreg "HKEY_LOCAL_MACHINE\System\CurrentControlSet\ENUM\%DEVICEID%" /grant=administrators=f subinacl /keyreg "HKEY_LOCAL_MACHINE\System\CurrentControlSet\ENUM\%DEVICEID%\Device Parameters" /grant=administrators=fREG ADD "HKLM\SYSTEM\CurrentControlSet\ENUM\%DEVICEID%\DEVICE Parameters" /v PORTNAME /t REG_SZ /d %newCOMPort% /Fset newFriendlyName=!oldFriendlyName:%oldCOMPort%=%newCOMPort%!REG ADD "HKLM\SYSTEM\CurrentControlSet\ENUM\%DEVICEID%" /v FriendlyName /t REG_SZ /d %newFriendlyName% /FREG ADD "HKLM\HARDWARE\DEVICEMAP\SERIALCOMM" /v %SerialCommKey% /t REG_SZ /d %newCOMPort% /FREG ADD "HKLM\SYSTEM\CurrentControlSet\Control\COM Name Arbiter" /v "ComDB" /t REG_BINARY /d 0%newComPort:~3,1%00000000000000000000000000000000000000000000000000000000000000 /fdevcon enable @"%DEVICEID%"?
調(diào)用方法:
COMPortNumberChanger.bat COM3 COM5?
共修改了4處注冊表項。 Devcon是微軟提供的設(shè)備管理工具,下載地址:http://support.microsoft.com/kb/311272 ,subinacl是一款訪問權(quán)限控制工具,下載地址:http://www.microsoft.com/downloads/en/details.aspx?familyid=e8ba3e56-d8fe-4a91-93cf-ed6985e3927b&displaylang=en
?
參考:
http://communities.intel.com/message/11471
?
Binhua Liu原創(chuàng),轉(zhuǎn)載請注明!
轉(zhuǎn)載于:https://www.cnblogs.com/Binhua-Liu/archive/2011/06/22/2087559.html
總結(jié)
以上是生活随笔為你收集整理的批处理:修改COM端口号的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件开发薪资调查
- 下一篇: Unity3D入门其实很简单