如何在C#中通过串行端口从文本文件接收的文本框中显示数据 [英] how to display the data in a textbox received from a text file through serial port in c#

查看:361
本文介绍了如何在C#中通过串行端口从文本文件接收的文本框中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用2种方式来发送数据  1)通过文本文件2)直接文本消息

i am using 2 ways to send the data  1) through a text file 2) direct text message 

当我以直接消息的形式发送数据时,我收到的文本没有任何问题..但是问题是当我在文本文件中发送数据时,它发送成功,但是无法读回并且无法显示文本框中的数据 有人建议我如何解决问题

when i send the data  as direct message  i received the text without any problem.. but the problem is when i send the data in text file it sent successfully , but  cant  read  back and unable to dispaly the data in textbox  can anyone suggest me how to  solve the problem

 private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string s= sp.ReadExisting();
        
    }

 感谢您

推荐答案

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

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


这篇关于如何在C#中通过串行端口从文本文件接收的文本框中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆