gpt4 book ai didi

C 和 Linux : Waiting for when a file has been written to

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:29:42 26 4
gpt4 key购买 nike

我目前正在从事一个项目,该项目将通过串行通信从微 Controller 读取数据。

截至目前,该程序(在我的计算机上)打开一个 /dev/tty* 文件并能够读/写它。微 Controller 会随时发送一个n字节的数据包。我想知道是否有任何方法可以判断何时所有字节都已写入文件?

我一直在查看 select()poll() 函数,但它们似乎只能告诉何时一个字节已准备就绪,但不是在每个字节都已写入时。

感谢任何帮助。谢谢!

最佳答案

如果你的 n 是硬编码的,你可以这样做(使用伪代码):

received_data = offset_from_last_round
while( received_data < n )
{
use select() for waiting data to arrive
read() all data you can, dont forget to check buffer oveflow here
received_data += how much data was red
}
full_message = buffer[ 0 ... N - 1 ]
offset_to_next_round = buffer[ N .. received_data ]

如果 n 不是硬编码的,你需要像@Golgauth 建议的那样做一些事情,或者在你的消息中添加一些“传输结束”序列/字节(如果你有二进制文件/要传输的数据)。简而言之:您需要某种协议(protocol)。

关于C 和 Linux : Waiting for when a file has been written to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17287753/

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