简体   繁体   English

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

[英]Cannot open/initialize serial port in C++ using SerialCommHelper

We are using a usb-serial port converter to establish a serial port connection. 我们正在使用USB串行端口转换器建立串行端口连接。 We've tested it on a computer with no serial port and were able to initialize and send command through the converter to the device successfully. 我们已经在没有串行端口的计算机上对其进行了测试,并且能够初始化并成功通过转换器将命令发送到设备。 Once we release the .exe file to another PC with the same usb-serial converter, it fails to open com port. 一旦将.exe文件释放到具有相同USB串行转换器的另一台PC上,它将无法打开com端口。

The only thing we thought we need to change in the code is the port number, which we made sure were correct from device manager. 我们认为我们需要更改代码的唯一一件事就是端口号,我们确保从设备管理器获得了正确的端口号。 COM6 on the working computer, and COM11 on the non-working one. COM6在正常工作的计算机上,而COM11在非正常工作的计算机上。 We also tried to change COM11 to COM2 (an unused port number). 我们还尝试将COM11更改为COM2(未使用的端口号)。 The PC we try to make it work on does already have 3 real serial port (COM1, 3 and 4), but would they somehow be interfering this port? 我们试图使其运行的PC已经具有3个真实的串行端口(COM1、3和4),但是它们会以某种方式干扰该端口吗?

We are using SerialCommHelper.cpp code to initialize the port. 我们正在使用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;
    }

And we call this using 我们称之为

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

which comPort is set correctly, but Init never returns S_OK. 哪个comPort设置正确,但是Init从不返回S_OK。

Any help is appreciated! 任何帮助表示赞赏! Thank you! 谢谢!

The COM port name syntax changes for COM10 and higher. COM端口名称语法针对COM10及更高版本而更改。 You need: "\\\\.\\COM10" 您需要:“ \\\\。\\ COM10”

as documented here... http://support.microsoft.com/kb/115831/en-us 如此处所述... http://support.microsoft.com/kb/115831/en-us

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM