C - While 和 IF 语句 - 尝试在 X 时间后超时
我在使用下面的代码时遇到困难;
int i = 0;
int x = 0;
int ch ;
int n;
while((i < sizeof(buffer) - 1) && (x < (TIMER_FREQ*30)))
{
//getkey_serial0 returns either a (int)character or 0 if nothing on
//UART0
if((ch = getkey_serial0) == 0)
{
x++; //Increment X as there is nothing being received.
}
else
{
if(ch == '\n')
{
n++;
}
if(n < 8){ //Yes I can simplify this but for some reason
} //I only just noticed this :/ Anyway, it is
else{ //just here to avoid saving info I don't need
buffer[i] = ch ;
i++;
}
}
}
由于它读取的输入是无线扫描的结果,因此扫描的条目数量可能会有很大差异,因此我需要能够避免无限循环。 最初我只读取了 11 个 \n,但这很垃圾,因为我一直缺少我需要的 SSID,所以我决定需要某种计时器或方法来帮助我在 X 时间后中断。
TIMER_FREQ 被定义为 10。
显然我正在做一些愚蠢的事情,所以任何建议或提示将不胜感激。 我通常更喜欢建议来帮助我尝试和思考问题,而不是固定的代码帖子:)尽管我尽了最大的努力,但我似乎总是错过一些简单的东西!
谢谢
编辑:我应该提到,这是在嵌入式系统(ARM7)上
I am having difficulties with the below code;
int i = 0;
int x = 0;
int ch ;
int n;
while((i < sizeof(buffer) - 1) && (x < (TIMER_FREQ*30)))
{
//getkey_serial0 returns either a (int)character or 0 if nothing on
//UART0
if((ch = getkey_serial0) == 0)
{
x++; //Increment X as there is nothing being received.
}
else
{
if(ch == '\n')
{
n++;
}
if(n < 8){ //Yes I can simplify this but for some reason
} //I only just noticed this :/ Anyway, it is
else{ //just here to avoid saving info I don't need
buffer[i] = ch ;
i++;
}
}
}
As the input it is reading in is the results of a wireless scan the number of entries scanned can vary greatly, and so I need to be able to avoid infinitely looping.
Originally I just read up to 11 \n's but this was rubbish as I kept missing SSID's which I needed, so I decided I needed some sort of timer or method to help me break after X amount of time.
TIMER_FREQ is defined as 10.
Clearly I am doing something stupid so any suggestions or tips would be greatly appreciated.
I generally prefer suggestions to help me try and think out the problem as opposed to fixed code posts :) I always seem to miss something simple despite my best efforts!
Thanks
EDIT: I should mention, this is on an embedded system (ARM7)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够访问通用定时器中断——通常称为 sys_tick()。
此类“裸机”应用程序的一般做法是将中断配置为每 n 毫秒触发一次(我的 Cortex M3 上经常使用 10 毫秒)。然后,让 ISR 更新计数器。您需要确保计数器更新是原子的,因此请使用 32 位、正确对齐的变量。 (我假设你的处理器是 32 位的,我记不清了)。然后您的“应用程序”代码可以根据需要轮询经过的时间。
但是 - 这个计时器讨论可能没有实际意义。在我的 ARM9 应用程序中,我们将中断与 UART 的接收缓冲区联系起来。关联的 ISR 捕获击键,然后执行任何缓冲区管理。这是你的选择吗?
You should have access to a general purpose timer interrupt -- commonly called sys_tick().
The general practice in such "bare metal" applications is to configure the interrupt to fire every n milliseconds (10 ms is frequently used on my Cortex M3). Then, have the ISR update a counter. You'll want to ensure the counter update is atomic, so use a 32-bit, properly-aligned variable. (I'm assuming your processor is 32-bit, I can't recall for certain). Then your "application" code can poll the elapsed time as needed.
BUT - this timer discussion might be moot. In my ARM9 applications, we tie an interrupt to the UART's receive buffer. The associated ISR captures the keystroke and then performs any buffer management. Is that an option for you?
你真正的意思是:
还是你实际上的意思是:
如果是后者,这就是为什么你的程序在给它一个函数指针时永远不会返回零。您的程序在构建时是否有很多警告?
Do you really mean:
Or do you actually mean:
If the latter, this is why your program never returns zero as you are giving it a function pointer. Does your program have many warnings at build?
如果你想计时,请查看 time()。它会让您看到系统的挂钟,这样您就可以确定是否已经过去了太多秒。
If you want to time things, look into time(). It will let you see the system's wall clock, so you can determine if too many seconds have elapsed.