【问题标题】:Storing an array of chars in a queue?在队列中存储一个字符数组?
【发布时间】:2019-04-03 15:25:31
【问题描述】:

我无法完全搜索我想要的东西,因为我不确定如何解决我想要做的事情,所以我只是描述一下。我认为并不真正需要有关 mbed 的知识,因为我无法解释那部分。

我的 mbed 程序中有一个中断函数,每次从 pc 上的串行链路输入时都会执行该函数。

Serial pc(USBTX,USBRX); //makes a serial link with pc

int main() {
    pc.attach(&newCommand);    //everytime input from pc is detected, this interrupt will make scheduler go to function called 'newCommand'

    //here, fetch one array of chars to work with, the oldest in the queue and analyse the command. If there is none available, wait for one to become available
}


void newCommand() {
    int inputCount = 0;
    int inputBuff[64];
    while (pc.readable()) {    //check if there is anything to read
        if (inputCount  < 64) // check for buffer overflow, maximum 64 ints
          inputBuff[inputCount] = pc.getc(); 
        else
          pc.getc();   // ran out of space, just throw the data away.
        inputCount++;
    }

    //store this char array (inputBuff) in a sort of queue, with the oldest at the front   
}

我会寻找什么样的队列?我在想也许我可以有一个全局的向量容器,它存储这些数组,然后主程序获取最旧的一个,但我不确定如何做到这一点?

编辑:我想我也可以做一个向量而不是数组来存储字符,如 someVect.push_back(pc.getc())。这样会更容易存储在向量类型的队列中吗?

【问题讨论】:

    标签: c++ serial-port c++98 mbed


    【解决方案1】:

    向量向量确实是一种选择,但我认为一个基本问题是,当串行中断发生时,您希望完整的命令准备好。这不能保证。

    另一种方法是使用BufferedSerial 并在命令之间使用分隔符(例如\n)。然后只需按分隔符拆分缓冲区。缺点是您需要轮询,但将回调修补到库中会很简单。

    【讨论】:

      猜你喜欢
      • 2020-01-22
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 1970-01-01
      相关资源
      最近更新 更多