windows 每次从串口读取数据后,我可以清除串口吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2948193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can i clear the serial port every time after reading the data from it?
提问by Badr
i need to clear the data on serial port when i have read the data from it before i read the data again? i m using c/c++ on windows xp
当我从串口读取数据时,我需要在再次读取数据之前清除串口上的数据吗?我在 windows xp 上使用 c/c++
how can i do so ?
我该怎么做?
thanx in advance.
提前谢谢。
采纳答案by Max Lybbert
The C++ standard has interfaces for writing to files, to the screen and to a log. It also has interfaces for reading from files and reading from "standard input." There is no standard way to interact with serial ports, network connections, etc.
C++ 标准具有用于写入文件、屏幕和日志的接口。它还具有从文件读取和从“标准输入”读取的接口。没有与串行端口、网络连接等交互的标准方式。
Luckily your operating system or platform will have an interface for this. But (1) what you have to do to read from a serial connection, and (2) what you need to do between consecutive reads, and (3) how to do it are all platform dependent.
幸运的是,您的操作系统或平台将为此提供一个接口。但是(1)从串行连接读取必须做什么,(2)在连续读取之间需要做什么,以及(3)如何做都取决于平台。
Looking at some Microsoft Documentation, you don't have to "clear the port" at all. But when a flag is set to signal something -- for instance that an error occurred -- then you need to reset the flag before continuing. Of course you reset the flag afterhandling whatever the flag was meant to signal.
查看一些 Microsoft 文档,您根本不必“清除端口”。但是当一个标志被设置为发出信号时——例如发生了错误——那么您需要在继续之前重置该标志。当然,您在处理完标志要发出的信号后重置标志。
回答by Hans Passant
Purging the receive buffer is almost always wrong. Serial port communications are asynchronous by nature, you'll risk deleting good data. Only if you use a master-slave protocol (the device only ever transmits when queried by the host) would allow purging. But then, if the receive buffer actually has data to purge then you're ignoring a protocol violation, something you neverwant to just ignore.
清除接收缓冲区几乎总是错误的。串行端口通信本质上是异步的,您将面临删除好的数据的风险。仅当您使用主从协议(设备仅在主机查询时才进行传输)时才允许清除。但是,如果接收缓冲区实际上有数据要清除,那么您就忽略了协议违规,这是您永远不想忽略的。
Reliable serial port communication requires a protocol. A checksum to verify message integrity and ACK/NAK handshaking to recover from data corruption. Check out the RATP protocol, described in RFC 916. Widely ignored btw but I've used it in the past. Its only weakness is buffered connection attempts.
可靠的串口通信需要一个协议。用于验证消息完整性和 ACK/NAK 握手以从数据损坏中恢复的校验和。查看 RFC 916 中描述的 RATP 协议。顺便说一句,被广泛忽略,但我过去曾使用过它。它唯一的弱点是缓冲连接尝试。
回答by Mohamed Tahir
The following solution is not the best, but it is suitable for many cases:
以下解决方案不是最好的,但适用于许多情况:
Read a large number of bytes from the serial port rendering it empty. For example:
从串行端口读取大量字节使其为空。例如:
ReadFile(Port,TMP,4096,&ioread,NULL);
ReadFile(Port,TMP,4096,&ioread,NULL);
tmp[ ]: is the variable that will hold the data that you don't want (flush data) ioread: Number of bytes read
tmp[ ]: 是保存你不想要的数据的变量 (flush data) ioread: 读取的字节数
So the above will read 4096 bytes from the serial port. You can increase the number to make sure you end up with a clean port after the statement.
所以上面将从串口读取4096个字节。您可以增加该数字以确保在语句后得到一个干净的端口。
P.S. I had used this with Visual Studio 2010, C++, Windows 7 - 64 bit
PS 我曾在 Visual Studio 2010、C++、Windows 7 - 64 位上使用过它