[Win32 API]检查RS-232端口是否已连接 [英] [Win32 API] Check if RS-232 port is connected

查看:275
本文介绍了[Win32 API]检查RS-232端口是否已连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与标准RS-232串行连接进行通信,同时在其周围构建错误捕获。我正在使用Win32 API文件函数来执行此操作。我的问题是我找不到一种有效的方法来判断RS232电缆是否已拔下。如果以这种方式执行,检查端口是否打开的本机
方式将始终返回true:


bool IsOpen
void
const
{ return < span style ="color:#c0c0c0">
文件
!= 0 );
}


最终创建了一个isOpen函数,该函数通过向串行线发送查询来检查电缆是否已连接,该查询将生成已知响应。

 bool isOpen()
{
string written = Q_OPC + TERM; //要写入串行的字符串
int testConnection = 255; //要对
char response [200]检查的值;
DWORD bytesread;
if(device.IsOpen())
{
if(static_cast< int>(device.Write(written.c_str()))== 0)
{
eventSleep(50);
if(static_cast< int>(device.Read(response,200,& bytesread))== 0)
{
response [bytesread] ='\0';
testConnection = strtoi(string(response)); // serial connection为测试变量返回一个新值
eventSleep(50);
device.Purge();
eventSleep(50);
if(testConnection!= 1)
{
device.Close(); //如果进程失败,请关闭并重新初始化设备
eventSleep(50);
device = CSerialEx();
返回false;
}
返回true;
}
其他
{
device.Purge();
eventSleep(50);
device.Close();
eventSleep(50);
device = CSerialEx();
返回false;
}
}
其他
{
device.Purge();
eventSleep(50);
device.Close();
eventSleep(50);
device = CSerialEx();
返回false;
}
}
其他
{
device = CSerialEx();
返回false;
}
}

这个函数运行正常,但是当它在主代码中应用时,我遇到了另一个问题。


以下是我希望主代码如何操作的伪代码(仅为了清晰起见)

 

if(RS232)设备已插入)
{

执行主要代码

if(RS232设备有通信错误)

{do error code }

}
if(RS232设备未插入)
{不执行任何操作/跳过}

由于检查端口是否打开的方法是与设备通信,当出现设备错误或设备被拔出时,编译器会跳过主代码。我希望代码只在拔出设备时跳过。这是我的
实际代码:

 

if(device.isOpen())
{
//主代码
int portCheck = device.readOutputParameters(channel1Mode,channel2Mode,channel1Value,channel2Value);
///////////////////////
//如果存在通信错误,代码的这一部分将永远不会执行isOpen函数
if(portCheck!= 0)
{
if(portCheck == -1)
{this-> appendText("设备不存在或是没有正确初始化"); }
休息;
}
///////////////////////
}


请注意,这是我的代码中过于简化的部分。为了解决这个问题,任何人都可以告诉我是否有一种方法可以判断标准RS232连接器是否在C ++ Win32 API中拔出,以防止拔出插头和错误状态相互分离?
提前致谢。









解决方案

您无法检查电缆(或外围设备)插入端口。


您可以检查控制线的状态(CTS等)或是否有一些数据。 


<希望这有帮助,


- pa


I'm trying to communicate with a standard RS-232 serial connection while building error trapping around it. I am using the Win32 API file functions to do so. My problem is that I cannot find an effective way to tell if the RS232 cable is unplugged. The native way to check if the port is open will always return true if done this way:

bool IsOpen (void) const { return (File != 0); }

I ended up creating a isOpen function that checks if the cable is connected by sending a query down the serial line that will generate a known response.

bool isOpen()
{
    string written = Q_OPC + TERM; //string to be written to serial line
    int testConnection = 255; //value to be checked against
    char response[200];
    DWORD bytesread;
    if(device.IsOpen())
    {
        if(static_cast<int>(device.Write(written.c_str())) == 0)
        {
            eventSleep(50);
            if (static_cast<int>(device.Read(response, 200, &bytesread)) == 0)
            {
                response[bytesread] = '\0';
                testConnection = strtoi(string(response)); //serial connection returns a new value to the test variable
                eventSleep(50);
                device.Purge();
                eventSleep(50);
                if(testConnection != 1)
                {
                    device.Close(); //close and reinitialize the device if process failed
                    eventSleep(50);
                    device = CSerialEx();
                    return false;
                }
                return true;
            }
            else
            {
                device.Purge();
                eventSleep(50);
                device.Close();
                eventSleep(50);
                device = CSerialEx();
                return false;
            }
        }
        else
        {
            device.Purge();
            eventSleep(50);
            device.Close();
            eventSleep(50);
            device = CSerialEx();
            return false;
        }
    }
    else
    {
        device = CSerialEx();
        return false;
    }
}

This function works fine, but when it is applied in the main code, I run into another problem.

Here is the pseudo-code for how I want the main code to operate (just for clarity sake)

if(the RS232 device is plugged in) {

do the main code

if(the RS232 device has a communication error)

{ do the error code }

} if (the RS232 device is not plugged in) { do nothing/skip }

Since the way to check if the port is open is to communicate with the device, when there is a device error or when the device is unplugged, the compiler skips over the main code. I want the code to skip over only when the device is unplugged. Here is my actual code:

if(device.isOpen()) { //main code int portCheck = device.readOutputParameters(channel1Mode, channel2Mode, channel1Value, channel2Value); /////////////////////// //if there is a communication error this section of the code will never execute because of the isOpen function if(portCheck != 0) { if(portCheck == -1) { this->appendText("The device is not present or is not initialized correctly"); } break; } /////////////////////// }

Please note that this is an over-simplified part of my code. To wrap things up, can anyone tell me if there is a way to tell if a standard RS232 connector is unplugged in C++ Win32 API that keeps unplugged and error states separate from each other? Thanks in advance.




解决方案

You cannot check if the cable (or a peripheral device) is plugged into the port.

You can check the state of control lines (CTS and so on) or if some data is coming. 

Hope this helps,

--pa


这篇关于[Win32 API]检查RS-232端口是否已连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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