gcc WinApi和Raspberry pi皮科的串行通信问题

fumotvh3  于 2023-03-12  发布在  其他
关注(0)|答案(1)|浏览(146)

希望大家都好。我遇到了一个奇怪的麻烦,而试图使与RPi-Pico -〉PC串行通信。从Pico我发送9字节包(来自ADC + \n的8字节数据)通过USB总线的串行通信,一秒一次。当从Windows阅读时,我的程序在第一手什么也没读到,只停留在if (!WaitCommEvent(hComm, &dwEventMask, NULL))阶段。但如果我运行putty与任何配置,然后数据显示在putty。关闭putty后,如果我运行我的程序,令人惊讶的是,它也阅读数据没有任何失败!!!!再次重新启动皮科后,同样的情况又回来了。到目前为止,这对我来说是一个非常未知的功能。你们中的任何人可以帮助我解决这个问题吗?我正在发送代码片段进行分析。提前感谢你。祝你有一个伟大的一天/晚上!

hComm = CreateFileA(commPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
        if (hComm == INVALID_HANDLE_VALUE)
            cout << "\nError Port " << commPortName << " can't be opened!!!\n";
        else
            cout << "\nPort " << commPortName << " is opened!!\n";
        dcbParams.DCBlength = sizeof(dcbParams);
        if (!GetCommState(hComm, &dcbParams))
            cout << "\nError in GetCommState()\n";
        dcbParams.BaudRate = CBR_115200; dcbParams.ByteSize = 8; 
        dcbParams.StopBits = ONESTOPBIT; dcbParams.Parity = NOPARITY;
        if (!SetCommState(hComm, &dcbParams))
            cout << "\nError in setting Device-Control Block(DCB)\n";
        else
        {
            cout << "\n\n    Setting DCB Structure Successfull" 
                << "\n       Baudrate --> " << dcbParams.BaudRate 
                << "\n       ByteSize --> " << (unsigned int)dcbParams.ByteSize 
                << "\n       StopBits --> " << (unsigned int)dcbParams.StopBits 
                << "\n       Parity   --> " << (unsigned int)dcbParams.Parity;
        }
        timeouts.ReadIntervalTimeout = 50;
        timeouts.ReadTotalTimeoutConstant = 50;
        timeouts.ReadTotalTimeoutMultiplier = 10;
        if (!SetCommTimeouts(hComm, &timeouts))
            cout << "\n\n    Error! in Setting Time Outs\n";
        else
            cout << "\n\n    Setting Serial Port Timeouts Successfull\n";
        if (!SetCommMask(hComm, EV_RXCHAR))
            cout << "\n\n    Error! in Setting Communication Mask\n";
        else
            cout << "\n\n    Setting Communication Mask successfull\n";
        cout << "\n\n    Waiting for Data Reception....\n";
        for (int i = 0; i <= 99; i+=1)
        {
            if (!WaitCommEvent(hComm, &dwEventMask, NULL))
                cout << "\n    Error! in Setting Wait for Communication Event...!\n";
            else
            {
                ReadFile(hComm, &readBuffer, sizeof(readBuffer), &noBytesRead, NULL);
                cout << "           " << readBuffer;
            }
        }
        CloseHandle(hComm);
hjzp0vay

hjzp0vay1#

正如@Luzian在问题中所说,这是一个设备配置问题(SetCommState),在检查putty源代码后,@Luzian的代码中没有启用DTR(数据终端就绪)流控制,并且RPi-Pico被证明是严格受DTR控制的。
下面的代码摘自putty。

/*
     * Set up the serial port parameters. If we can't even
     * GetCommState, we ignore the problem on the grounds that the
     * user might have pointed us at some other type of two-way
     * device instead of a serial port.
     */
    if (GetCommState(serport, &dcb)) {
        const char *str;

        /*
         * Boilerplate.
         */
        dcb.fBinary = true;
        dcb.fDtrControl = DTR_CONTROL_ENABLE;
        dcb.fDsrSensitivity = false;
        dcb.fTXContinueOnXoff = false;
        dcb.fOutX = false;
        dcb.fInX = false;
        dcb.fErrorChar = false;
        dcb.fNull = false;
        dcb.fRtsControl = RTS_CONTROL_ENABLE;
        dcb.fAbortOnError = false;
        dcb.fOutxCtsFlow = false;
        dcb.fOutxDsrFlow = false;
        ...
    }

相关问题