SerialPort.BytesToWrite 的用法
如果 SerialPort.Write() 是阻塞操作(或者不是?),那么需要 BytesToWrite() 方法。它总是评估为零,导致最后的写入操作要么成功写入所有数据,要么失败,在任何一种情况下,要写入的字节都将为零。
也许,事情比我所描述的还要多。
If the SerialPort.Write() is a blocking operation( or is it not?), what would be the need for the BytesToWrite() method. It would always evaluate to zero, cause the last Write operation either succeeded in writing all data or failed, in either case the bytes to be written would be come zero.
Perhaps, there is more to it then what I have described.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SerialPort.Write
是一个阻塞操作,是的。然而,有两个缓冲区需要考虑:串行设备和SerialPort
缓冲区。如果将
SerialPort
对象配置为缓冲,则仅当该缓冲区没有足够的空间时才会阻止写入。只要缓冲区变空足以容纳新数据,它就会阻塞。否则它会填充缓冲区并返回。如果
SerialPort
对象不缓冲,则写入操作仅在将数据传输到串行设备时阻塞。该设备有自己的缓冲区(*),因此该块所花费的时间可能比发送数据所花费的时间少得多。SerialPort.BytesToWrite
包括设备缓冲区中的数据和SerialPort
对象缓冲区中的数据。(*) 较旧的 UART 没有缓冲区,较新的 UART 有缓冲区,但可以配置为不缓冲区。
SerialPort.Write
is a blocking operation, yes. However, there are two buffers to be considered: The serial device, and theSerialPort
buffers.If the
SerialPort
object is configured to buffer, the write only blocks if there isn't enough room in that buffer. It will block for as long as it takes the buffer to empty enough to fit the new data. Otherwise it fills the buffer and returns.If the
SerialPort
object does not buffer, the Write operation blocks only for as long as it takes to transfer the data to the serial device. That device has its own buffer(*), so the block may take far less time than the time it will take to send the data.SerialPort.BytesToWrite
includes both data in the device's buffer and data in theSerialPort
object's buffer.(*) Older UARTs did not have buffers and newer ones do but can be configured to not buffer.
串行端口是非常慢的 I/O 设备,其历史可以追溯到计算的石器时代。在 9600 位/秒的常见波特率下,它每秒只能传输不到千字节。与硬盘相比,每秒可实现 60,000,000 字节的突发速度。或者更具可比性,网卡每秒可以传输 125,000,000 字节。
因此,串行端口驱动程序会保留一个缓冲区来存储您写入的字节,并在 UART 芯片写入这些字节时慢慢清空它。该缓冲区允许 Write() 调用快速返回。鉴于它是如此缓慢,您可能需要了解该缓冲区有多满,以便避免阻塞 Write() 调用。 WriteBufferSize - BytesToWrite 告诉您该缓冲区中有多少可用空间。
Serial ports are very slow I/O devices that date from the stone age of computing. At a common baudrate of 9600 bits per second, it can only transmit a bit less than thousand bytes per second. Compare to a hard disk, a burst speed of 60,000,000 bytes per second is possible. Or more comparable, a network card can transmit 125,000,000 bytes per second.
So the serial port driver keeps a buffer that stores the bytes you write and slowly empties it while they are written by the UART chip. That buffer allows the Write() call to quickly return. Given that it is so slow, you might want to be aware of how full that buffer is so that you can avoid blocking on the Write() call. WriteBufferSize - BytesToWrite tells you how much space is available in that buffer.
SerialPort 属性 BytesToWrite 获取编号发送缓冲区中数据的字节数。
另一方面,SerialPort 方法 Write(string text) 写入指定的字符串到串行端口。
您知道串口是如何工作的吗?串口每秒发送一定数量的字节,具体取决于所使用的波特率。
The SerialPort property BytesToWrite Gets the number of bytes of data in the send buffer.
On the other hand the SerialPort method Write(string text) Writes the specified string to the serial port.
You do know how a serial port works right? A serial sends a certain amount of bytes every second depending on the baud used.