无法使用SerialCommhelper在C 中打开/初始化串行端口

Cannot open/initialize serial port in C++ using SerialCommHelper

本文关键字:初始化 串行端口 SerialCommhelper      更新时间:2023-10-16

我们正在使用USB - 塞端口转换器来建立串行端口连接。我们已经在没有串行端口的计算机上对其进行了测试,并且能够通过转换器成功地将命令发送到设备。一旦我们将.EXE文件发布到具有相同的USB串行转换器的另一台PC,它将无法打开COM端口。

我们唯一需要更改代码中的东西是端口号,我们确保从设备管理器中正确。在工作计算机上的com6,而在非工作人员上进行com11。我们还尝试将COM11更改为COM2(未使用的端口号)。我们试图使其工作的PC确实已经有3个真实的串行端口(COM1、3和4),但是它们会以某种方式干扰此端口吗?

我们使用serialCommhelper.cpp代码来初始化端口。

HRESULT CSerialCommHelper:: Init(std::string szPortName, DWORD dwBaudRate,BYTE byParity,BYTE byStopBits,BYTE byByteSize) 
{
HRESULT hr = S_OK;
try
{

    m_hDataRx  = CreateEvent(0,0,0,0);
    //open the COM Port
    //LPCWSTR _portName =LPCWSTR( szPortName.c_str());
    wchar_t* wString=new wchar_t[4096];
   MultiByteToWideChar(CP_ACP, 0, szPortName.c_str(), -1, wString, 4096);
    m_hCommPort = ::CreateFile(wString,
                                GENERIC_READ|GENERIC_WRITE,//access ( read and write)
                                0,  //(share) 0:cannot share the COM port                       
                                0,  //security  (None)              
                                OPEN_EXISTING,// creation : open_existing
                                FILE_FLAG_OVERLAPPED,// we want overlapped operation
                                0// no templates file for COM port...
                                );
    if ( m_hCommPort == INVALID_HANDLE_VALUE )
    {
        TRACE ( "CSerialCommHelper : Failed to open COM Port Reason: %d",GetLastError());
        ASSERT ( 0 );
        std::cout << "This is where the error happens" << std::endl;
        return E_FAIL;
    }

,我们使用

称此为此
if( m_serial.Init(comPort, 38400, 0, 1, 8) != S_OK )

正确设置了哪个comport,但init永远不会返回s_ok。

任何帮助将不胜感激!谢谢你!

com端口名称语法更改com10及更高。您需要:" \。 com10"

如下所示...http://support.microsoft.com/kb/115831/en-us