如何设置 CreateFile() 打开 COM 端口时将使用的 DTR/RTS 状态

发布于 2024-07-22 06:52:46 字数 1456 浏览 4 评论 0原文

我编写/支持了一个名为 uCon 的终端模拟器 (http://www.umonfw.com/ucon) 。 它全部基于“good-ole”Win32,并且完全采用“C”语言。 最近,我被要求支持将 uCon 连接到 COM 端口并为 RS232 流量控制之外的目的设置 DTR/RTS 的功能。 我知道我可以在使用 EscapeCommFunction() 和/或 SetCommState() 调用 CreateFile() 后执行此操作; 但是,这些函数只能在 CreateFile() 返回打开的端口的句柄之后调用。 不幸的是,当 CreateFile() 打开端口时,它将 DTR/RTS 设置为其默认状态,这可能(或可能不)与我希望保留 DTR 的状态不同。

例如,假设用户连接了一个板到PC的串行端口,DTR线用于将板置于某种非标准状态。 当 DTR 不活动时,主板运行“正常”,但有时 DTR 活动用于将硬件转换到其他状态。

在我见过的大多数情况下,CreateFile() 使 DTR 处于活动状态,然后我调用清除 DTR 将其恢复为非活动状态; 然而,这是我需要避免的一个小故障。 我发现了一个名为 GetDefaultCommConfig() & 的函数集 SetDefaultCommConfig() 但无法让它们成功工作。 所以,我的问题是……

有没有办法预先定义调用 CreateFile() 时在 RS232 控制线上建立的默认状态? 有人成功使用过 GetDefaultCommConfig()/SetDefaultCommConfig() 吗?

在我看来,这应该允许我预先确定 DTR 的值是 当调用 CreateFile() 时使用...

 
int
EstablishDefaultDTR(char *comPortName, int dtr)
{
    COMMCONFIG  cc;
    DWORD   bsize = sizeof(COMMCONFIG);

    if (GetDefaultCommConfig(comPortName,&cc,&bsize) == 0) {
        ShowLastError("GetDefaultCommConfig()");
        return(-1);
    }

    if (dtr)
       cc.dcb.fDtrControl = DTR_CONTROL_ENABLE ;
    else
       cc.dcb.fDtrControl = DTR_CONTROL_DISABLE ;

    if (SetDefaultCommConfig(comPortName,&cc,bsize) == 0) {
        ShowLastError("SetDefaultCommConfig()");
        return(-1);
    }
}

但是,正如您可能已经猜到的那样,事实并非如此。 有任何想法吗?

I wrote/support a terminal emulator called uCon (http://www.umonfw.com/ucon). Its all based on "good-ole" Win32, and is entirely in 'C'. I was recently asked to support the ability to have uCon attach to a COM port and set up DTR/RTS for purposes outside of RS232 flow control. I know I can do this after CreateFile() is called using EscapeCommFunction() and/or SetCommState(); however, these functions can only be called AFTER CreateFile() returns a handle to the opened port. Unfortunately, when CreateFile() opens the port, it sets DTR/RTS to their default state, which may (or may not) be different than the state that I wish to keep DTR in.

For example, assume the user has a board connected to the PC's serial port, and the DTR line is used to put the board in some non-standard state. With DTR inactive, the board runs "normal", but occasionally DTR-active is used to transition the hardware to some other state.

In most cases I've seen, CreateFile() brings DTR active, then my call to clear DTR brings it back to inactive; however, that's a glitch I need to avoid. I found a function set called GetDefaultCommConfig() & SetDefaultCommConfig() but have not been able to get them to work successfully. So, my question is this...

Is there a way to pre-define the default state that will be established on the RS232 control lines when CreateFile() is called? Has anyone used GetDefaultCommConfig()/SetDefaultCommConfig() successfully?

It seems to me that this should allow me to pre-establish the value of DTR to be
used when CreateFile() is called...

 
int
EstablishDefaultDTR(char *comPortName, int dtr)
{
    COMMCONFIG  cc;
    DWORD   bsize = sizeof(COMMCONFIG);

    if (GetDefaultCommConfig(comPortName,&cc,&bsize) == 0) {
        ShowLastError("GetDefaultCommConfig()");
        return(-1);
    }

    if (dtr)
       cc.dcb.fDtrControl = DTR_CONTROL_ENABLE ;
    else
       cc.dcb.fDtrControl = DTR_CONTROL_DISABLE ;

    if (SetDefaultCommConfig(comPortName,&cc,bsize) == 0) {
        ShowLastError("SetDefaultCommConfig()");
        return(-1);
    }
}

But, as you may have already guessed, it doesn't.
Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

输什么也不输骨气 2024-07-29 06:52:46

可能不是最快的方法,但这是有效的:

#include <stdlib.h>
#include <stdio.h>

int
EstablishDefaultDTR(char *comPortName, int dtr){
    char commandString[256];
    if ( !system(NULL) ){
        ShowLastError("system()");
        return(-1);
    }        
    sprintf( commandString, "MODE %s dtr=%s%", comPortName, dtr? "on":"off"  );
    return system( commandString );
}

Might not be the fastest way, but this works:

#include <stdlib.h>
#include <stdio.h>

int
EstablishDefaultDTR(char *comPortName, int dtr){
    char commandString[256];
    if ( !system(NULL) ){
        ShowLastError("system()");
        return(-1);
    }        
    sprintf( commandString, "MODE %s dtr=%s%", comPortName, dtr? "on":"off"  );
    return system( commandString );
}
甜中书 2024-07-29 06:52:46

您没有初始化 COMMCONFIG 结构。 这很可能是问题所在,因为文档明确指出您必须至少设置 dwSize

cc.dwSize = sizeof( COMMCONFIG );

You're not initializing the COMMCONFIG structure. That could well be the problem since the documentation explicitly says that you must set dwSize at least

cc.dwSize = sizeof( COMMCONFIG );

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文