串行COM端口不开放 [英] Serial COM Port not opening

查看:134
本文介绍了串行COM端口不开放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Win32应用程序打开串行COM端口。端口正确打开,但我可以发送接收只是,如果我previously使用Teraterm或超级终端打开了它字节。

如果我停用,并激活设备管理器COM端口(所以端口是刚使用)我需要简单地打开带有Teraterm或腻子的COM端口和AFER我关闭它,然后我的软件,我可以发送和接收正常在任何时候。

我使用英巴卡迪诺X3与FileMonkey,使在同一时间的应用程序利用MACOSX和Win32兼容。 FileMonkey不suuport VCL。

在同一code是构建6 C ++工作正常使用VCL但它不应该反正影响它。我的code是非常简单的。

这是一个非常怪异的行为。好像我需要给init COM(我使用FTDI转换器)之前,我可以使用它,当电脑重新启动或者我停用和激活背部从设备管理COM看来这INIT正在消失,我只需要打开COM使用其他系列软件恢复,然后使用我的软件。

请注意:一旦端口INIT我从软件的完全控制。我相信我的code是真正开放的COM端口,因为COM出现繁忙一旦INIT是,如果我尝试使用其他软件使用它完成。因此,没有FLASE报警多的应用正在另一边至今。

因为我已经很久这个问题,我现在决定解决吧!

这是我的code:

  //全局变量
HANDLE hComm = NULL;
COMMTIMEOUTS ctmoNew = {0},ctmoOld;DCB dcbCommPort;
      hComm =的CreateFile(COM2                      GENERIC_READ | GENERIC_WRITE,
                      0,
                      0,
                      OPEN_EXISTING,
                      0,
                      0);    如果(hComm == INVALID_HANDLE_VALUE)
     {        应用 - >终止();
     }    GetCommTimeouts(hComm,&安培; ctmoOld);
    ctmoNew.ReadTotalTimeoutConstant = 100;
    ctmoNew.ReadTotalTimeoutMultiplier = 0;
    ctmoNew.WriteTotalTimeoutMultiplier = 0;
    ctmoNew.WriteTotalTimeoutConstant = 0;
    SetCommTimeouts(hComm,&安培; ctmoNew);
    dcbCommPort.DCBlength = sizeof的(DCB);
    GetCommState(hComm,&安培; dcbCommPort);
    BuildCommDCB(115200,N,8,1,&安培; dcbCommPort);
    SetCommState(hComm,&安培; dcbCommPort);


解决方案

只是一种可能性,不是肯定,这就是问题所在。

但是有一点不同,以我们典型的COM端口开放code是我们使用的 SetupCom 后直接的CreateFile成功,我们设置缓冲区大小,通常为4KB。

I am trying to open the serial COM PORT in a Win32 Application. The port is opening correctly but I can send a receive bytes just if I previously opened it using Teraterm or Hyperterminal.

If I deactivate and activate the COM port from Device Manager (so the port is freshly unused) I need to simply open the COM port with Teraterm or Putty and afer I closed it and then run my software I can send and receive properly at anytime.

I am using Embarcadero X3 with FileMonkey to make the application compatible with MACOSX and Win32 at the same time. FileMonkey does not suuport VCL.

The same code is working fine on Builder 6 c++ using VCL but it should not influence it in anyway. My code is really simple.

This is a very weird behavior. Seems like I need to INIT the COM (I am using FTDI converter) before I can use it and when the PC restarts or I deactivate and activate back the COM from Device Manager it seems this INIT is vanished and I simply need to open up the COM to revive it using another Serial Software and then use my software.

Note: Once the port is INIT I have the full control from my software. I am sure my code is truly opening the COM port because the COM appears busy once the iNIT is done if I try to use it with other softwares. So no flase alarm and more the application is working on the other side so far.

I have this problem since long and I now decided to solve it!

This is my code:

// GLOBAL VARIABLES
HANDLE hComm = NULL;
COMMTIMEOUTS ctmoNew = {0}, ctmoOld;

DCB dcbCommPort; 
      hComm = CreateFile("COM2",

                      GENERIC_READ | GENERIC_WRITE,
                      0,
                      0,
                      OPEN_EXISTING,
                      0,
                      0);

    if(hComm == INVALID_HANDLE_VALUE)
     {

        Application->Terminate();
     }

    GetCommTimeouts(hComm,&ctmoOld);
    ctmoNew.ReadTotalTimeoutConstant = 100;
    ctmoNew.ReadTotalTimeoutMultiplier = 0;
    ctmoNew.WriteTotalTimeoutMultiplier = 0;
    ctmoNew.WriteTotalTimeoutConstant = 0;
    SetCommTimeouts(hComm, &ctmoNew);
    dcbCommPort.DCBlength = sizeof(DCB);
    GetCommState(hComm, &dcbCommPort);
    BuildCommDCB("115200,N,8,1", &dcbCommPort);
    SetCommState(hComm, &dcbCommPort);

解决方案

Just a possibility, not certain that this is the problem.

But one difference to our typical COM port opening code is that we use SetupCom directly after the CreateFile succeeded and set our buffer sizes, typically 4KB.

这篇关于串行COM端口不开放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆