且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中通过串行端口从文本文件接收的文本框中显示数据

更新时间:2023-11-03 15:01:58

您无法发送文本文件",通过串口.该端口发送字符流或字节流,具体取决于您如何对其进行配置.要发送文件,您必须打开文件并流式传输其内容.在那个 指出它的工作原理与您直接编写字符串消息完全相同.请张贴您用来编写文件的代码.

You cannot send "text files" through the serial port. The port sends either a stream of characters or a stream of bytes depending upon how you have it configured. To send a file you'd have to open the file and stream its contents. At that point it works exactly like you had written a string message directly. Please post the code you're using to write the file.

还请注意,您的处理程序并不能在所有情况下都能正常工作. ReadExisting返回接收到数据时缓冲区中的可用内容.这可能是也可能不是所有已发送的数据.对于小消息,可能会,但对于大消息 消息只会是当时收到的消息.该事件将多次引发,直到接收到所有数据.在接收到所有数据之前,您必须构建要接收的字符串(可能在字段或其他内容中).

Also note that your handler isn't going to work in all cases. ReadExisting returns what is available in the buffer at the point the data is received. This may or may not be all the data that was sent. For small messages it probably will be but for large messages it will only be what has been received at that time. The event will be raised multiple times until all the data is received. Until all the data is received you'll have to build up the string being received (probably in a field or something).

最大的挑战是知道何时拥有一切.这完全取决于您所定义的协议.在某些协议中,每个消息"都可以被发送.前面带有某种报头,该报头除其他外指定了数据的长度 被接收.在其他协议中,在消息末尾发送定义明确的值.您需要确定如何在协议中进行操作.

The biggest challenge is knowing when you've got everything. That is completely up to the protocol that you've defined. In some protocols each "message" is preceded by some sort of header that specifies, amongst other things, the length of data to be received. In other protocols a well-defined value is sent at the end of the message. You need to decide how you'll do it in your protocol.

迈克尔·泰勒
http://www.michaeltaylorp3.net

Michael Taylor
http://www.michaeltaylorp3.net