Windows串行端口无法正确连接?

时间:2018-07-26 05:08:39

标签: c++ windows arduino serial-port

我有一个代码可以与Arduino Uno通信。它是C ++,并且是从GUI运行的。我写了一个用于串行通信的类,将在GUI中使用。我的猜测是串行端口是一个问题,因为它是引发错误的原因。 类代码为:

#ifndef SERIALPORTV2_H
#define SERIALPORTV2_H
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>

class SerialPortV2
{
    public:
        SerialPortV2(std::string comPort, std::string baudRate)
        {
            open(comPort, baudRate);
        }

        SerialPortV2()
        {
            printf("New serial port created");
        }

        ~SerialPortV2()
        {
            close();
        }

        void open(std::string comx, std::string baud)
        {
            hComm = CreateFile(comx.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
            if (hComm == INVALID_HANDLE_VALUE)
            {
                std::cout<<"Error in opening serial port"<<std::endl;
                opened = false;
            }
            else
            {
                std::cout<<"opening serial port successful"<<std::endl;
                opened = true;
                DCB dcbSerialParams = { 0 }; // Initializing DCB structure
                dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
                GetCommState(hComm, &dcbSerialParams);
                dcbSerialParams.BaudRate = CBR_9600;
                dcbSerialParams.ByteSize = 8;         // Setting ByteSize = 8
                dcbSerialParams.StopBits = ONESTOPBIT;// Setting StopBits = 1
                dcbSerialParams.Parity   = NOPARITY;  // Setting Parity = None
                SetCommState(hComm, &dcbSerialParams);
            }
        }

        void close()
        {
            CloseHandle(hComm);
            opened = false;
        }

        bool isOpen()
        {
            return opened;
        }

        std::string readSerialPort()
        {
            std::string buff;
            char temp;
            DWORD bytesRead;
            std::cout<<"Reading from arduino..."<<std::endl;
            do
            {
                ReadFile(hComm, &temp, sizeof(temp), &bytesRead, NULL);
                std::cout<<"From serial reader: "<<temp<<std::endl;
                buff += temp;
            }
            while(bytesRead > 0);
            std::cout<<"Reading from arduino done"<<std::endl;
            buff.pop_back();
            return buff;
        }
        void writeSerialPort(std::string msg)
        {
            char buff[64];
            strcpy(buff, msg.c_str());
            DWORD dNoOFBytestoWrite;
            DWORD dNoOfBytesWritten = 0;
            dNoOFBytestoWrite = sizeof(buff);
            std::cout<<"TEST: "<<buff<<std::endl;
            WriteFile(hComm, buff, dNoOFBytestoWrite, &dNoOfBytesWritten, NULL);
        }

    private:
        HANDLE hComm;
        bool opened;

    protected:


};
#endif // SERIALPORTV2_H

当我使用此类编译程序时,会引发以下错误:

C:\Users\matthew\Desktop\PersonalHeaders\SerialPortV2.h|29|error: cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}' for argument '1' to 'void* CreateFileW(LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE)'|

因此,我将其更改为LPCWSTR(我使用了所有正确的转换方法),但随后抛出了相反的信息:“无法将LPCWSTR转换为LPCSTR”

我的解决方法是使用CreateFileA或CreateFileW,并进行适当的变量转换。这些工作都是在编译方面进行的。

当我运行代码(使用编译版本(CreateFileA或CreateFileW))并选择Arduino处于打开状态的COM端口(COM6)时,它将重置Arduino,但没有通过RX或TX发送任何信息(如从Arduino LED)

然后,我可以毫无问题地打开Arduino串行监视器,并且可以毫不费力地与Arduino通信。当我尝试重新连接GUI时,控制台显示COM端口打开失败。

我不知道为什么它能正常工作,然后如果我不做任何更改,它就停止工作,从那以后我一直感到困惑。

0 个答案:

没有答案