gpt4 book ai didi

c++ - 在队列中存储一组字符?

转载 作者:太空宇宙 更新时间:2023-11-04 12:38:05 26 4
gpt4 key购买 nike

我无法完全搜索我想要的东西,因为我不确定如何解决我想做的事情,所以我只想描述它。我认为关于 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
}

我需要什么样的队列?我在想也许我可以有一个全局的 vector 容器,它存储这些数组,然后主程序获取最旧的一个,但我不确定该怎么做?

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

最佳答案

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

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

关于c++ - 在队列中存储一组字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55498820/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com