0

我有一个从串行端口获取数据的类,并且我收到 LDR 测量的开关数量仅为 1 和 0。

现在我想将它存储在一个类中,只要程序运行我如何使用托管变量来完成这个?

请注意,串行类每秒运行一次,因此当我创建打开一个我现在使用的类时 StoreClass Store

Store.Value = LDR_Value; // LDR_Value is the value from the serial bus

当我这样做时,总会创建一个 StoreClass 的副本,但这并不能解决问题。

请帮帮我。

4

2 回答 2

1

如果我理解正确,您需要一个容器来存储收到的所有值。如果您有一个名为 的类StoreClass,则可以创建一个vector成员并执行以下操作:

class StoreClass
{
  public:
    void AddValue(int v) { m_values.push_back(v); }

  private:
    std::vector<int> m_values; // Stores all values in order of arrival.
}

现在你只需要你的类的一个实例:

int main()
{
  StoreClass storage;
  while(StillSerialInput())
  {
    storage.AddValue(GetSerialValue());
  }
}
于 2012-09-12T05:30:38.937 回答
0

如果您想在容器中累积数据,请使用标准容器。我认为std::vector作为你工作的一个很好的容器就足够了:

#include <vector>

using namespace std;

int main()
{
   vector<DataType> dataReceived; //define data container

   DataType buffer; //type of your data
   while(recevingFromDevice) //loop that keeps running as long as you're receiving from your device
   {
      buffer = getDataFromDevice(); //buffer what you receive from the device
      dataReceived.push_back(buffer); //add this buffer to the stack of data
   }
   //now dataReceived is an array with all received data
}
于 2012-09-12T05:30:04.647 回答