【问题标题】:Keep receiving NULL when reading from the arduino to the c++ program从 arduino 读取到 c++ 程序时继续接收 NULL
【发布时间】:2016-07-08 09:57:22
【问题描述】:

我在我的 c++ 程序中使用 windows API 通过串行连接来连接 arduino。

arduino 每秒会发送一个字节,c++ 程序应该读取该字节并在屏幕上打印。

这是我的 arduino 代码。

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.write('a');
  delay(1000);  
}

Serial 类中有成员

    //Serial comm handler
    HANDLE hSerial;
    //Connection status
    bool connected;
    //Get various information about the connection
    COMSTAT status;
    //Keep track of last error
    DWORD errors;
    //transform error message to be stored in a string variable

这是我的 C++ 程序中的读取函数

int Serial::ReadData(char buffer)
{
    //Number of bytes we'll have read
    DWORD bytesRead = 0;

    //Use the ClearCommError function to get status info on the Serial port
    ClearCommError(this->hSerial, &this->errors, &this->status);

    cout << this->status.cbInQue << " Bytes in the queue have not been read" << endl;
    //Check if there is something to read
    if(this->status.cbInQue>0)
    {
        //Try to read the require number of chars, and return the number of read bytes on success
        if(ReadFile(this->hSerial, (void *)&buffer, 1, &bytesRead, NULL) )
        {
            return bytesRead;
        }

    }
    //If nothing has been read, or that an error was detected return 0
    return 0;
}

如果需要,这是我的串行构造函数

Serial::Serial(string portName, char use)
{
    //We're not yet connected
    this->connected = false;

    DWORD authority;
    if (use == 'w'){
        authority = GENERIC_WRITE;
    }
    else if (use == 'r'){
        cout << "read" << endl;
        authority = GENERIC_READ;
    }
    else if (use == 'b'){
        authority = GENERIC_READ | GENERIC_WRITE; 
    }
    else{
        cout << "the second parameter of the initialization of the Serial object is wrong" << endl;
        cout << "It can only be w or r" << endl;
    }

    //Try to connect to the given port through CreateFile
    this->hSerial = CreateFile(portName.c_str(),
            authority,
            0,
            NULL,
            OPEN_EXISTING,
            0,
            NULL);

    //Check if the connection was successful
    if(this->hSerial==INVALID_HANDLE_VALUE)
    {
        cout << GetLastErrorAsString() << endl;
    }
    else
    {
        //If connected we try to set the comm parameters
        DCB dcbSerialParams = {0};

        //Try to get the current
        if (!GetCommState(this->hSerial, &dcbSerialParams))
        {
            //If impossible, show an error
            printf("failed to get current serial parameters!");
        }
        else
        {
            //Define serial connection parameters for the arduino board
            dcbSerialParams.BaudRate=CBR_9600;
            dcbSerialParams.ByteSize=8;
            dcbSerialParams.StopBits=ONESTOPBIT;
            dcbSerialParams.Parity=NOPARITY;
            //Setting the DTR to Control_Enable ensures that the Arduino is properly
            //reset upon establishing a connection
            dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE;

             //Set the parameters and check for their proper application
             if(!SetCommState(hSerial, &dcbSerialParams))
             {
                printf("ALERT: Could not set Serial Port parameters");
             }
             else
             {
                 //If everything went fine we're connected
                 this->connected = true;
                 //Flush any remaining characters in the buffers
                 PurgeComm(this->hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR);
                 //We wait 2s as the arduino board will be reseting
                 Sleep(ARDUINO_WAIT_TIME);
             }
        }
    }
}

这是我的主程序

int main(int argc, _TCHAR* argv[])
{     
    char use = 'r'; //r for read, w for write, b for both
    Serial* SP = new Serial("COM4", use);    // adjust as needed

    char input;
    int num_bytes_read;
    while(SP->IsConnected())
    {
        cout << "connect successes!" << endl;
        num_bytes_read = SP->ReadData(input);
        cout << num_bytes_read << " " << (int) input << endl;
        Sleep(1000);
    }
    return 0;
}

打印到屏幕时,我将 char 变量 'input' 转换为 int 以检查 char 变量中存储的值的 ascii 码。

最后是我的c++程序的输出

connect successes!
1 Bytes in the queue have not been read
1 0

好像确实收到了数据,但是为什么打印的是NULL而不是'a'呢?

我使用 Arduino/Genuino Uno(这是我在 Arduino IDE 中检查“工具”按钮时看到的)和 Windows 10

感谢您考虑我的要求!

【问题讨论】:

    标签: c++ winapi arduino


    【解决方案1】:

    在 C++ 中,参数是通过 vaule... 传递的,除非它们是通过引用传递的。所以改变这一行:

    int Serial::ReadData(char buffer)
    

    到:

    int Serial::ReadData(char &buffer)
    

    否则,当您读入buffer 时,您实际上是在读入ReadData 的局部变量,并且不要从main 修改input

    可以说,如果你把它改成类似这样的东西会更好:

    int Serial::ReadData(char *buffer, int len)
    

    然后,在main 你称之为写作:

    num_bytes_read = SP->ReadData(&input, 1);
    

    这样,将来当您想读取更多数据时,只需一个调用即可。

    【讨论】:

    • 非常感谢!我完全忘记了!我已经使用 char* 来完成这个任务。只是出于好奇,当我使用 string& 时,为什么它打印出乱码?以下是部分乱码。 (((((((((((((((((((((() abcdefghijklmnopqrstuvwxyz VS140COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 14.0) 非常感谢!))
    • 我支持双参数方法,但投票给size_t len
    • @DavidChen:关于string&amp;,您应该展示如何使用它,因为如果您使用ReadFile(..., &amp;string, ...),那将无法正常工作,因为string 是一个复杂的类而不是简单的字节数组。
    • @rodrigo 我只是按照您提到的方式使用string&amp;。现在我知道为什么它不起作用了,因为你的解释,非常感谢!
    猜你喜欢
    • 2015-05-13
    • 1970-01-01
    • 2013-04-06
    • 2018-10-01
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    相关资源
    最近更新 更多