当我们通过串口c#发送文本文件时,我没有收到完整的数据。 [英] I am not receiving the complete data when we send a textfile through serial port c#..?

查看:193
本文介绍了当我们通过串口c#发送文本文件时,我没有收到完整的数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙伴们

我通过串口发送文本文件并创建一个文本框来显示传入的数据,但是当文本框中的数据显示文本文件中缺少某些行时。我怎样才能收到全部数据



这里是我的代码

Hi guys
i send a text file through serial port and create a textbox to display the incoming data, but when the data display on textbox some lines are missing from text file .?? how can i receive the entire data

here is my code

private void butSetFile_Click(object sender, EventArgs e)
{
    this.openFileDialog1.ShowDialog();

    this.txtfile.Text = this.openFileDialog1.FileName;
}

private void butSendFile_Click(object sender, EventArgs e)
{
    if (serialPort1.IsOpen == false)
    {
        this.serialPort1.Open();
    }
    serialPort1.Write(System.IO.File.ReadAllText(this.txtfile.Text));
}



private void save_Click(object sender, EventArgs e)
{

    this.saveFileDialog1.ShowDialog();
    System.IO.File.WriteAllText(this.saveFileDialog1.FileName, this.txtData1.Text);
}





我尝试过:



串口连接为环回



What I have tried:

serial port is connected as loop back

推荐答案

正如我昨天解释的那样:

As I explained yesterday:
引用:

顺便说一句:串口不是瞬时的。传输数据需要时间,因此使用ReadExisting可能无法一次性获取整个消息!

And by the way: Serial ports are not instantaneous. It takes time to transfer the data, so using ReadExisting may not get you the entire message in one go!

同样适用于ReadAllText - 系统不知道文件何时开始和结束,它只是数据到串口。因此,您需要打包信息,以便您的发送和接收软件协同工作。你不能只是去发送,并假设它是通过魔法立即完成的! :笑:

The same applies to ReadAllText - the system does not "know" when a file starts and ends, it's just data to the Serial port. So you need to "package" the information so that your sending and receiving software are working together. You can't just go "send" and assume that it's all done instantly for your by magic! :laugh:


首先,你的代码示例是关于发送,而不是关于接收。



首先要做的是确保文件正确发送。为此,在接收端设置像PuTTY这样的终端工具,它可以让你看到你从串口获得的东西。

一旦你确定发送没问题,你知道问题出在接收方。



你需要在编程接收部分之前学习串口,因为存在很多缺陷。

- 串口很慢。你需要考虑到这一点。

- Windows在接收方设置一个缓冲区,但它不是无限的,可能是文件不适合缓冲区。

- 串口本身没什么可告诉你的,你收到了完整的文件。你必须设置一个方法来处理这个问题。
First of all, your code sample is about sending, not about receiving.

First thing to do is to make sure the file is sent correctly. For this, set a terminal tool like PuTTY on receiving side, it will allow you to see what you get from serial.
Once you are sure sending is ok, you know the problem is on receiving side.

You need to learn serial port before programming the receiving part because there is a lot od pitfalls.
- Serial port is slow. you need toke this into account.
- Windows set a buffer on receiving side, but it is not infinite, chances are that the file do not fit in buffer.
- Serial port by itself have nothing to tell you that you received the full file. you have to device a method to handle this.


这篇关于当我们通过串口c#发送文本文件时,我没有收到完整的数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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