在队列中存储字符数组?

时间:2019-04-03 15:25:32

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

由于我不确定如何解决我想做的事情,因此我无法完全搜索到我想要的东西,因此我将仅对其进行描述。我认为关于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())一样。这样会更容易将其存储在向量类型队列中吗?

1 个答案:

答案 0 :(得分:0)

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

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