完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我一直在苦苦思索如何将一个计时器添加到一个循环中,它会在离散的时间(比如10秒)之后退出。
我要做的是收集RS232接口的输入来填充一个字符串(包括结尾的校验和)。 当接收到校验和时,循环退出。我还想退出,如果我没有收到它在离散的时间量(10秒以上的价值)。 我试图用一个8位计数器来实现它,它被1秒的时钟计时,结果是计数器值为10,退出,但永远不能将计数器重置为下一次的0(保持每10秒退出一次)。 做这类事情的人是如何实现一个时间的,这样你就不会永远在串行处理循环中停留在字符串缓冲区中的垃圾中了吗? 以上来自于百度翻译 以下为原文 I've been struggleing with how to add a timer to a loop that will exit after a discrete amount of time (say 10 seconds). What I have going on is I'm collecting input from an RS232 interface to fill up a string (that includes a checksum at the end). The loop exits when the checksum is received. I also want to exit if I don't receive it after a discrete amount of time (that 10 second value above). I tried to implement it using a 8 bit counter that was clocked by a 1 seconds clock with the result that I could get the counter value to 10, exit, but never able to reset the counter back to 0 for the next pass (kept exiting every 10 seconds How do folks that do this type of thing implement implement a time so your not stuck forever in the serial processing loop forever with garbage in the string buffer? |
|
相关推荐
2个回答
|
|
为了在串行接口上接收数据,不阻塞最好的解决方案是RX驱动中断和循环缓冲器的组合。
如果您愿意,另一种方式(更容易)是为组件定义一个足够大的缓冲区,并让INT通过内部中断来处理,这将自动启用。你只需要查询包含所需字符量的缓冲区。 如果你仍然处于退出循环中:设置一个定时器,在进入例程时启动它,在循环RX和定时器状态下进行轮询,并在停止定时器后发生任何一个退出,如果RX有一个字符获取并返回值,则返回一个没有字符的指示。 快乐编码 鲍勃 以上来自于百度翻译 以下为原文 For receiving data over a serial interface non-blocking the best solution is a combination of Rx driven interrupt and a circular buffer. If you prefer, another way (easier) is to define for the component a buffer large enough and let int be handled by the internal interrupt which will be enabled automatically. You just have to poll for the buffer containing the required amount of chars. If you still are interrested in exiting a loop: Set up a timer, start it when entering the routine, poll in a loop Rx and timer status and exit when any of them occurs after stopping the timer and if Rx had a char get it and return the value, else return an indication for no char. Happy coding Bob |
|
|
|
如果你仍然处于退出循环中:设置一个定时器,在进入例程时启动它,在循环RX和定时器状态下进行轮询,并在停止定时器后发生任何一个退出,如果RX有一个字符获取并返回值,则返回一个没有字符的指示。
快乐编码 鲍勃 实际上,这就是我用计数器和1Hz时钟所做的。我投票给选民超过10秒,明确的事情,并返回主。不幸的是,我不能清除计数器并停止它,所以每10秒它就继续退出,因为计数器计数为10。我漫游了这些例子,并没有清楚地理解它为什么用各种各样的方法来处理和发挥作用。 以上来自于百度翻译 以下为原文 If you still are interrested in exiting a loop: Set up a timer, start it when entering the routine, poll in a loop Rx and timer status and exit when any of them occurs after stopping the timer and if Rx had a char get it and return the value, else return an indication for no char. Happy coding Bob Actually, that is what I did using a counter and a 1HZ clock. I poll the counter for being greater than 10 seconds, clear things and return to main. Unfortunately, I can not clear the counter and stop it, so every 10 seconds it continues to exit because the counter counts to 10. I've roamed the examples and do not have a clear understanding of why it behaves and played with all kinds of library methods to no avail. |
|
|
|
只有小组成员才能发言,加入小组>>
752个成员聚集在这个小组
加入小组2070 浏览 1 评论
1826 浏览 1 评论
3639 浏览 1 评论
请问可以直接使用来自FX2LP固件的端点向主机FIFO写入数据吗?
1761 浏览 6 评论
1513 浏览 1 评论
CY8C4025LQI在程序中调用函数,通过示波器观察SCL引脚波形,无法将pin0.4(SCL)下拉是什么原因导致?
510浏览 2评论
CYUSB3065焊接到USB3.0 TYPE-B口的焊接触点就无法使用是什么原因导致的?
360浏览 2评论
CX3连接Camera修改分辨率之后,播放器无法播出camera的画面怎么解决?
410浏览 2评论
357浏览 2评论
使用stm32+cyw43438 wifi驱动whd,WHD驱动固件加载失败的原因?
857浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-24 00:19 , Processed in 0.919910 second(s), Total 79, Slave 62 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号