option explicit

'
' Debugging trace flags, to enable debug output trace message
' change gDebugFlag to true.
'
const kDebugTrace = 1
const kDebugError = 2
dim gDebugFlag

gDebugFlag = false

'
' Operation action values.
'
const kErrorSuccess     = 0
const kErrorFailure     = 1

'
' Generic strings
'
const L_Help_Help_General01_Text   = "用法: CScript CodifyCOMSetting.vbs [-c port name] [-i port info] [-p ini file] "
const L_Help_Help_General02_Text   = "参数:"
const L_Help_Help_General03_Text   = "-c     - 指定的串口名称"
const L_Help_Help_General04_Text   = "-i     - 串口参数：9600,n,8,1,p"
const L_Help_Help_General05_Text   = "		       第二个值：n--无校验位;	e--偶校验;	o--奇校验;	m--标志校验;	s--空格校验"
const L_Help_Help_General06_Text   = "		       第五个值：x--Xon/Xoff;	p--硬件;放空表示无流控制"
const L_Help_Help_General07_Text   = "-p     - 存放串口参数的ini文件名"
const L_Help_Help_General08_Text   = "-?     - 显示命令用法"
const L_Help_Help_General09_Text   = "例如:"
const L_Help_Help_General10_Text   = "CScript CodifyCOMSetting.vbs -c COM1: -i 9600,n,8,1"
const L_Help_Help_General11_Text   = "CScript CodifyCOMSetting.vbs -c COM1: -i 9600,n,8,1 -p NewPort.ini" 
const L_Help_Help_General12_Text   = "备注:"
const L_Help_Help_General13_Text   = "ini 文件只是按行存放文本信息，不用遵守ini文件格式"

'
' Messages to be displayed if the scripting host is not cscript
'
const L_Help_Help_Host01_Text      = "此脚本应该用 CSCRIPT.EXE 在命令提示符下执行。"
const L_Help_Help_Host02_Text      = "例如: CScript script.vbs 参数"
const L_Help_Help_Host03_Text      = ""
const L_Help_Help_Host04_Text      = "要设置 CScript 作为运行 .vbs 文件的默认应用程序，运行如下:"
const L_Help_Help_Host05_Text      = "     CScript //H:CScript //S"
const L_Help_Help_Host06_Text      = "然后可以运行 ""script.vbs arguments""，而无需在运行脚本前先使用 CScript。"

'
' Miscellaneous messages
'
const L_Text_Msg_General01_Text    = "串口参数设置成功"
const L_Text_Msg_General02_Text    = "串口参数设置失败"


AddPrinter


'
' AddPrinter execution starts here
sub AddPrinter
    dim iRetval
	dim strComputer
    dim strHostAddress
    dim strPrinterName
    dim strPortName
    dim strPortNnumber
    dim strModel
    dim strPath
    dim uVersion
    dim strEnvironment
    dim WshShell 
    dim strAddPrinterDriverCMD
    dim objWMIService
    dim objNewPort
    dim  intResult
	Dim oReg
	Dim objPrinter
	Dim intMaxPrinterNumber
	Dim strTempPrinterNumber
	Dim intPosi
	Dim strKeyPath
	Dim strPortInfo

	strComputer = "."
    strPortName = "COM1:"
    strPortInfo   = "9600,n,8,1"
    'strPortNnumber   = "9100"
    strPath=""
    uVersion=""
    strEnvironment=""
    iRetval=0

    '
    ' Abort if the host is not cscript
    '
    if not IsHostCscript() then

        call wscript.echo(L_Help_Help_Host01_Text & vbCRLF & L_Help_Help_Host02_Text & vbCRLF & _
                          L_Help_Help_Host03_Text & vbCRLF & L_Help_Help_Host04_Text & vbCRLF & _
                          L_Help_Help_Host05_Text & vbCRLF & L_Help_Help_Host06_Text & vbCRLF)

        wscript.quit

    end if

    '
    ' Get command line parameters
    '
    iRetval = ParseCommandLine(strPortName, strPortInfo, strPath)
			
    if iRetval = kErrorSuccess  Then
    
		'通过修改注册表修改串口参数
		const HKEY_LOCAL_MACHINE = &H80000002 
		strComputer = "." 
		  
		Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv") 
		  
		strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports" 
		'stPortName = "COM1:" 
		'strPortInfo = "14400,n,8,0" 
		oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strPortName,strPortInfo 
            			
		if Err.Number <> 0 then
				wscript.echo L_Text_Msg_General02_Text & vbCRLF & Err.Description
		else 
				wscript.echo L_Text_Msg_General01_Text
		end If
		
    end if

End Sub

'
' Parse the command line into its components
'
function ParseCommandLine(strPortName,strPortInfo, strPath)

    on error resume next

'    DebugPrint kDebugTrace, L_Text_Dbg_Msg05_Text

    dim oArgs
    dim iIndex
    dim line1,line2,line3

    iIndex = 0

    set oArgs = wscript.Arguments

    while iIndex < oArgs.Count

        select case oArgs(iIndex)
            case "-c"
                iIndex = iIndex + 1
                strPortName= oArgs(iIndex)

            case "-i"
                iIndex = iIndex + 1
                strPortInfo = oArgs(iIndex)

            case "-p"
                iIndex = iIndex + 1				
                if(ReadContentsFromFiles(oArgs(iIndex),line1,line2)) then
                    strPortName=line1
                    strPortInfo=line2					
                end if    
           
            case "-?"
                Usage(true)
                exit function

            case else
                Usage(true)
                exit function

        end select

        iIndex = iIndex + 1

    wend

    if Err.Number <> 0 then
        wscript.echo "ParseCommandLine Error:" & Err.Description
        ParseCommandLine = kErrorFailure
    else
        ParseCommandLine = kErrorSuccess
    end if

end  function

function ReadContentsFromFiles(fileName,line1,line2)
dim fso
dim f
dim forReading
dim content

    forReading=1
    set fso = CreateObject("Scripting.FileSystemObject")
    set f = fso.OpenTextFile(fileName,forReading)

'    '读取文件的内容
    line1=f.ReadLine()
    line2=f.ReadLine()    
    f.close()

    if Err.Number <> 0 then
        wscript.echo "ReadContentsFromFiles Error:" & Err.Description
        ReadContentsFromFiles = false
    else
        ReadContentsFromFiles = true
    end if
end function

'
' Display command usage.
'
sub Usage(bExit)

    wscript.echo L_Help_Help_General01_Text
    wscript.echo L_Help_Help_General02_Text
    wscript.echo L_Help_Help_General03_Text
    wscript.echo L_Help_Help_General04_Text
    wscript.echo L_Help_Help_General05_Text
    wscript.echo L_Help_Help_General06_Text
    wscript.echo L_Help_Help_General07_Text
    wscript.echo L_Help_Help_General08_Text
    wscript.echo L_Help_Help_General09_Text
    wscript.echo L_Help_Help_General10_Text
    wscript.echo L_Help_Help_General11_Text
    wscript.echo L_Help_Help_General12_Text
    wscript.echo L_Help_Help_General13_Text
    
    if bExit then

        wscript.quit(1)

    end if

end sub


'
' Determines which program is being used to run this script.
' Returns true if the script host is cscript.exe
'
function IsHostCscript()

    on error resume next

    dim strFullName
    dim strCommand
    dim i, j
    dim bReturn

    bReturn = false

    strFullName = WScript.FullName

    i = InStr(1, strFullName, ".exe", 1)

    if i <> 0 then

        j = InStrRev(strFullName, "\", i, 1)

        if j <> 0 then

            strCommand = Mid(strFullName, j+1, i-j-1)

            if LCase(strCommand) = "cscript" then

                bReturn = true

            end if

        end if

    end if

    if Err <> 0 then

        wscript.echo L_Text_Error_General01_Text & L_Space_Text & L_Error_Text & L_Space_Text _
                     & L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description

    end if

    IsHostCscript = bReturn

end function
