【问题标题】:C++ Serial communication with ArduinoC++ 与 Arduino 的串行通信
【发布时间】:2016-10-19 10:00:36
【问题描述】:

我正在开发一个在 Windows PC 和 Arduino Uno 卡之间进行串行通信的项目。

在 C++ 代码中,我有一个 SerialClass.h 和一个 Serial.cpp

我的问题是编译器错误:标识符“SP”未定义 在函数中

void Serial::SendtoArd(int val, int var)
{

if (SP->IsConnected())
{
    bool writeData = false;
    writeData = SP->WriteData("test",4);
}

我知道如果我在这个函数中定义了 SP,我就摆脱了这个错误,但是 我不想在该功能中激活串行端口。我想在这个函数里激活串口

bool Serial::OpenPtoArd()

{

Serial* SP = new Serial("\\\\.\\COM3");    // adjust as needed
if (SP->IsConnected())
{

    bool status = true;
}
}

只要我的应用程序正在运行,它就会保持活动状态。

谁能帮帮我?

这里是 SerialClass.h

#ifndef SERIALCLASS_H_INCLUDED
#define SERIALCLASS_H_INCLUDED

#define ARDUINO_WAIT_TIME 2000

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

class Serial
{
private:
//Serial comm handler
HANDLE hSerial;
//Connection status
bool connected;
//Get various information about the connection
COMSTAT status;
//Keep track of last error
DWORD errors;

public:
//Initialize Serial communication with the given COM port
Serial(char *portName);
//Close the connection
~Serial();
//Read data in a buffer, if nbChar is greater than the
//maximum number of bytes available, it will return only the
//bytes available. The function return -1 when nothing could
//be read, the number of bytes actually read.
int ReadData(char *buffer, unsigned int nbChar);
//Writes data from a buffer through the Serial connection
//return true on success.
bool WriteData(char *buffer, unsigned int nbChar);
//Check if we are actually connected
bool IsConnected();
bool OpenPtoArd();
void SendtoArd(int val, int var);


};

这里是 Serial.cpp

#endif // SERIALCLASS_H_INCLUDED


#include "stdafx.h"
#include "SerialClass.h"


#define LEN 1
bool status = false;
Serial::Serial(char *portName)
{
//We're not yet connected
this->connected = false;

//Try to connect to the given port throuh CreateFile
this->hSerial = CreateFile(portName,
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL);

//Check if the connection was successfull
if (this->hSerial == INVALID_HANDLE_VALUE)
{
    //If not success full display an Error
    if (GetLastError() == ERROR_FILE_NOT_FOUND) {

        //Print Error if neccessary
        printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);

    }
    else
    {
        printf("ERROR!!!");
    }
}
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;

        //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;
            //We wait 2s as the arduino board will be reseting
            Sleep(ARDUINO_WAIT_TIME);
        }
    }
}

}

Serial::~Serial()
{
//Check if we are connected before trying to disconnect
if (this->connected)
{
    //We're no longer connected
    this->connected = false;
    //Close the serial handler
    CloseHandle(this->hSerial);
}
}

int Serial::ReadData(char *buffer, unsigned int nbChar)
{
//Number of bytes we'll have read
DWORD bytesRead;
//Number of bytes we'll really ask to read
unsigned int toRead;

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

//Check if there is something to read
if (this->status.cbInQue>0)
{
    //If there is we check if there is enough data to read the required    number
    //of characters, if not we'll read only the available characters to prevent
    //locking of the application.
    if (this->status.cbInQue>nbChar)
    {
        toRead = nbChar;
    }
    else
    {
        toRead = this->status.cbInQue;
    }

    //Try to read the require number of chars, and return the number of read bytes on success
    if (ReadFile(this->hSerial, buffer, toRead, &bytesRead, NULL) && bytesRead != 0)
    {
        return bytesRead;
    }

}

//If nothing has been read, or that an error was detected return -1
return -1;

}


bool Serial::WriteData(char *buffer, unsigned int nbChar)
{
DWORD bytesSend;

//Try to write the buffer on the Serial port
if (!WriteFile(this->hSerial, (void *)buffer, nbChar, &bytesSend, 0))
{
    //In case it don't work get comm error and return false
    ClearCommError(this->hSerial, &this->errors, &this->status);

    return false;
}
else
    return true;
}

bool Serial::IsConnected()
{
//Simply return the connection status
return this->connected;
}



void readme()

{

Serial serial("COM3");

char c[LEN + 1];
int numBytes = 0;
while (true)
{
    numBytes = serial.ReadData(c, LEN);
    if (numBytes != -1)
    {
        // Terminate the string if we want to use c variable as a string
        c[numBytes] = 0;
        break;
    }
}

}

bool Serial::OpenPtoArd()

{

Serial* SP = new Serial("\\\\.\\COM3");    // adjust as needed
if (SP->IsConnected())
{

    bool status = true;
}
}

void Serial::SendtoArd(int val, int var)
{

if (SP->IsConnected())
{
    bool writeData = false;
    writeData = SP->WriteData("test",4);
}

}

【问题讨论】:

    标签: c++ serialization


    【解决方案1】:

    这段代码的问题

    bool Serial::OpenPtoArd()
    {
        Serial* SP = new Serial("\\\\.\\COM3");    // adjust as needed
        if (SP->IsConnected())
        {
            bool status = true;
        }
    }
    

    是您正在创建新的Serial - 然后在函数退出时丢失指向该Serial 的指针。如果您希望其他函数能够访问它,则需要将SP 变量OpenPtoArd() 函数之外。

    您可以(应该?)将其设为您班级的成员(顺便说一下, 会与 Arduino 的 Serial 班级发生冲突 - 称它为别的! ),或将其设为全局变量:将以下行放在文件顶部附近:

    YourSerialClass *SP = NULL;
    

    请注意,我将变量设置为NULL。您的其他代码需要知道 SP 端口是否已创建 - 如果尚未创建,使用它:

    if ((SP!=NULL) && (SP->IsConnected()) {
        ... do SP things
    } // if
    

    【讨论】:

    • 我添加了 Serial *SP = NULL;到 Serial.cpp 的顶部。在 Serail.cpp 中很好很好。
    • 我在 Serial.cpp 的顶部添加了 Serial *SP = NULL。现在编译好了。但是,当我从另一个类中使用 SP->OpenPtoArd() 添加对函数 Serial::OpenPtoArd() 的调用时,我遇到了与以前相同的编译器错误。这个其他类代码有#include“SerialClass”,所以我也尝试将Serial *SP = NULL;在那里,但仍然是问题
    • 在设置添加 Serial *SP = NULL 到另一个类的代码顶部后,我得到了编译确定。现在对函数的调用起作用了,但是一旦函数 OpenPtoArd 退出,SP 就会再次为 NULL。所以我又回到了问题的起点。
    • 我以为我得到了这个工作,但不,它不工作。如何将它声明为全局的,以便我可以从所有类的所有函数中访问它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    相关资源
    最近更新 更多