代码之家  ›  专栏  ›  技术社区  ›  Re0sless

在Delphi中查找串行端口设置

  •  0
  • Re0sless  · 技术社区  · 15 年前

    嗨,我需要找到串行端口的波特率和其他设置,在网上查看,我应该使用 GetCommConfig ,这将返回一个tcommconfig记录,其中包含我认为需要的数据。问题是函数I wote返回了错误的值。

    下面的代码看起来工作正常,但波特率始终为1200,在Windows设备管理器中查找(并更改端口设置)是错误的。

    我试着这样称呼它:

    ComPort('com1');
    ComPort('COM1');
    ComPort('COM1:');
    ComPort('COM4');
    ComPort('COM9');
    

    前4个有效,但返回1200,第5个无效,返回0

    function ComPort(l_port:String):TCommConfig;
    {Gets the comm port settings}
        var
        ComFile: THandle;
        PortName: array[0..80] of Char;
        size: cardinal;
        CommConfig:TCommConfig;
    begin
        FillChar(Result, SizeOf(TCommConfig), 0);//blank return value
    
        try
            StrPCopy(PortName,l_port);
            ComFile := CreateFile(PortName,GENERIC_READ or GENERIC_WRITE,0,nil,OPEN_EXISTING,0{ FILE_ATTRIBUTE_NORMAL},0);
            try
                if (ComFile <> INVALID_HANDLE_VALUE) then
                begin
                    FillChar(CommConfig, SizeOf(TCommConfig), 0);//blank record
                    CommConfig.dwSize := sizeof(TCommConfig);//set size
                    //CommConfig.dcb.DCBlength := SizeOf(_dcb);
                    size := sizeof(TCommConfig);
    
                    if (GetCommConfig(ComFile,CommConfig,size)) then
                    begin
                        Result := CommConfig;
                    end;
                end;
            finally
               CloseHandle(ComFile);
            end;
        except
            Showmessage('Unable to open port ' + l_port);
        end;
    end;
    

    单步执行代码时,前4个总是碰到行 结果:=commconfig; ,所以getcommconfig正在重新调整一个有效的代码,所以我一定遗漏了一些东西。

    我尝试过很多其他的事情,比如设置DCB记录的长度,但是都有与波特率1200相同的结果。

    有人知道我哪里出错了吗?

    2 回复  |  直到 12 年前
        1
  •  3
  •   lg.    15 年前

    串行端口的波特率和其他设置是在串行端口打开时设置的。 我想你在读默认值。

        2
  •  3
  •   Re0sless    15 年前

    结果发现我用的是错误的函数,我应该用 GetDefaultCommConfig 而不是 GetCommConfig 我用的。

    通过查看它是否正确,如果我错了请纠正我,getdefaultcommconfig从Windows返回设置,getcommconfig返回到端口的打开连接的设置,writefile根据需要打开端口(忽略默认设置),这是1200波特率的来源。

    如果这对将来的任何人都有帮助,那么我想到的就是这个功能。

    function ComPort(l_port:String):TCommConfig;
    {Gets the comm port settings (use '\\.\' for com 10..99) }
        var
        size: cardinal;
        CommConfig:TCommConfig;
    begin
        FillChar(Result, SizeOf(TCommConfig), 0);
    
        //strip trailing : as it does not work with it
        if (RightStr(l_port,1) = ':') then l_port := LeftStr(l_port,Length(l_port)-1);
    
        try
            FillChar(CommConfig, SizeOf(TCommConfig), 0);
            CommConfig.dwSize := sizeof(TCommConfig);
    
            size := sizeof(TCommConfig);
    
            if (GetDefaultCommConfig(PChar(l_port),CommConfig,size)) then
            begin
                Result := CommConfig;
            end
            //if port is not found add unc path and check again
            else if (GetDefaultCommConfig(PChar('\\.\' + l_port),CommConfig,size)) then
            begin
                Result := CommConfig;
            end
        except
            Showmessage('Unable to open port ' + l_port);
        end;
    end;
    
    推荐文章