在队列中存储一组字符?

问题描述 投票:1回答:1

我无法完全搜索我想要的东西,因为我不知道如何解决我想做的事情,所以我只是要描述一下。我认为关于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个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.