类中的重载方法(arduino)

时间:2017-03-11 17:49:05

标签: c++ arduino

标题代码

/*
 * MngCommunication.h
 *
 *  Created on: 26 gen 2017
 *      Author: Giuliano
 */

#ifndef _MNGCOMMUNICATION_H_
#define _MNGCOMMUNICATION_H_

// ------------------------- N.B. MngCommunication is a Singleton     class!!! ----------------------------
#if (ARDUINO <  100)
#include <WProgram.h>
#else
#include <Arduino.h>
#endif

class MngCommunication
{
public:
    static MngCommunication* getInstance();


    size_t LogLine(const __FlashStringHelper *ifsh, boolean end=false);
    size_t LogLine(const String &str, boolean end=false);
    size_t LogLine(const char str[], boolean end=false);
    size_t LogLine(const unsigned char n, boolean end=false);
    size_t LogLine(const int n, boolean end=false);
    size_t LogLine(const unsigned int n, boolean end=false);
    size_t LogLine(const long n, boolean end=false);
    size_t LogLine(const unsigned long n, boolean end=false);
    size_t LogLine(const double n, boolean end=false);
    size_t LogLine(const Printable&str, boolean end=false);

protected:
    static MngCommunication* _inst_;
    MngCommunication();
};

#endif /* MNGCOMMUNICATION_MNGCOMMUNICATION_H_ */

C ++代码

/*
 * MngCommunication.cpp
 *
 *  Created on: 26 gen 2017
 *      Author: Giuliano
 */

#include <string.h>
#include "MngCommunication.h"


MngCommunication* MngCommunication::_inst_ = NULL;


MngCommunication::MngCommunication()
{
    Serial.begin(57600);

}


MngCommunication* MngCommunication::getInstance()
{
    if (_inst_ == NULL) _inst_ = new MngCommunication;

    return _inst_;
}

//--------------------------------------------------------------------
//--------------------------------------------------------------------
// ------------------------ LOGGING FUNCTION -------------------------
//--------------------------------------------------------------------
//--------------------------------------------------------------------
boolean started=false;

inline size_t startLine(void)
{
    static char timebuffer[8];
    size_t sz = 0;

    sprintf(timebuffer,"%08lu", millis());

    sz += Serial.print("#L-");
    sz += Serial.print(timebuffer);
    sz += Serial.print("-");
    started=true;
    return sz;

}

inline size_t endline(void)
{
    started = false;
    return Serial.write("\r\n");
}

size_t MngCommunication::LogLine(const __FlashStringHelper *ifsh, boolean end)
{
    size_t sz=0;
    if (!started)
    {
    sz += startLine();
    started = true;
    }

    sz += Serial.print(ifsh);

    if (end)
    {
        sz += endline();
    }


    return sz;
}

size_t MngCommunication::LogLine(const String &str, boolean end)
{
    size_t sz=0;
    if (!started)
    {
        sz += startLine();
        started = true;
    }

    sz += Serial.print(str);

    if (end)
    {
        sz += endline();
    }

    return sz;
}


size_t MngCommunication::LogLine(const char str[], boolean end)
{
    size_t sz=0;
    if (!started)
    {
        sz += startLine();
        started = true;
    }

    sz += Serial.print(str);

    if (end)
    {
        sz += endline();
    }

    return sz;
}





size_t MngCommunication::LogLine(const unsigned char n, boolean end)
{
    return LogLine((unsigned long) n, end);
}


size_t MngCommunication::LogLine(const int n, boolean end)
{
    return LogLine((long) n, end);
}

size_t MngCommunication::LogLine(const unsigned int n, boolean end)
{
    return LogLine((unsigned long) n, end);
}


size_t MngCommunication::LogLine(const long n, boolean end)
{
    size_t sz=0;
    if (!started)
    {
        sz += startLine();
        started = true;
    }

    sz += Serial.print(n);

    if (end)
    {
        sz += endline();
    }

    return sz;
 }

 size_t MngCommunication::LogLine(const unsigned long n, boolean end)
 {
    size_t sz=0;
    if (!started)
    {
        sz += startLine();
        started = true;
    }

    sz += Serial.print(n);

    if (end)
    {
        sz += endline();
    }

    return sz;
}



size_t MngCommunication::LogLine(const double n, boolean end)
{
    size_t sz=0;
    if (!started)
    {
        sz += startLine();
        started = true;
     }

     sz += Serial.print(n);

    if (end)
    {
         sz += endline();
    }

    return sz;
}

size_t MngCommunication::LogLine(const Printable&str, boolean end)
{
    size_t sz=0;
    if (!started)
    {
        sz += startLine();
        started = true;
    }

    sz += Serial.print(str);

    if (end)
    {
        sz += endline();
    }

    return sz;
}

如果我对字符串或字符缓冲区使用LogLine()函数,则一切正常。 e.g:

MngCommunication* Comm;

Comm = MngCommunication::getInstance();
Comm.LogLine("this ");
Comm.LogLine("is ");
Comm.LogLine("a ");
Comm.LogLine("test!!! ", true);

奇怪的是号码来电: 如果我用数字来呼叫LogLine(),例如:

MngCommunication* Comm;

Comm = MngCommunication::getInstance();
Comm.LogLine(millis());

代码执行没问题。但是第二次调用,使用数字或字符串,重新启动Arduino。同样的,如果我先用字符串然后用数字调用函数。 e.g:

MngCommunication* Comm;

Comm = MngCommunication::getInstance();
Comm.LogLine("this ");
Comm.LogLine("is ");
Comm.LogLine("a ");
Comm.LogLine("test!!! ");

Comm.LogLine("time= ");
Comm.LogLine(mills());

1 个答案:

答案 0 :(得分:1)

static char timebuffer[8];
sprintf(timebuffer,"%08lu", millis());

首先,这个缓冲区太小了。字符串终结符必须有空格(零字节),因此您有一个缓冲区溢出,可能会导致各种奇怪的效果。

此缓冲区的长度至少应为11个字节。最大数字(32位)为10个字节,字符串终止符为1个字节。

我认为mills()是一个错字?

使用常量值时是否存在同样的问题,例如Comm.LogLine(123);

如果你打电话怎么办:

Comm.LogLine(123, true);
Comm.LogLine(456);