如何从串口C#接收文本文件..? [英] How do I receive the text file from serial port C# ..?
问题描述
hi guys
我正在创建一个用于通过串口发送和接收数据的小gui。我给出了两种发送数据的方法:1)文本文件2)直接在文本框中输入文本。
如果我在文本框中发送数据,
i成功获得响应,但问题是如何接收文本文件
文本文件发送成功通过串口但无法接收文本文件任何人都可以请我接收文本文件.. ??
我尝试过:
hi guys
i am creating a small gui for sending and receiving data through serial port. i gave two ways for sending the data 1) text file 2) direct text entering in textbox.
i successfully get response if i send a data in textbox , but the problem is how can i receive text files
the text file is send successfully through serial port but unable to receive the text file can anyone please me who to receive text files ..??
What I have tried:
private void button5_Click(object sender, EventArgs e)
{
try
{
if (serialPort1.IsOpen)
{
txtReceive.Text = serialPort1.ReadExisting();
txtReceive.Clear();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
i使用此代码,但它会收到我们在文本框中输入的数据不是来自文本文件
i use this code but it receive the data what we enter in textbox not from the text file
推荐答案
当数据来自串口时,它没有任何关于它来自何处的指示 - 它只是一个字节流 - 或者在你的情况下是组成字符串的字符流。
如果你想发送文本文件,那么你需要为接收软件提供一种方法来将其识别为文本文件而不是消息 - 通过提供无法输入的标题,或通过在发送者和接收者之间实现协议,这意味着他们总是知道他们得到了什么。
没有 - 需要更改发送软件和接收 - 您可以;告诉数据来自何处。
顺便说一句:串口不是瞬时的。传输数据需要时间,因此使用ReadExisting可能无法一次性获取整个消息!
When data comes from a serial port, it doesn't have any indications as to where it came from - it's just a stream of bytes - or in your case a stream of characters which make up a string.
If you want to send a text file, then you need to provide a way for the receiving software to identify it as a text file instead of a message - either by providing a "header" which can't be typed, or by implementing a protocol between the sender and the receiver which means they always know what they are getting.
Without that - which needs changes in the send software as well as the receive - you can;t tell where data came from.
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!
您没有收到文件。你收到数据。如何解释这些数据取决于您的规格。如果数据是文本字符串,则可以在文本框中显示它们或将它们保存到文件中。但这是某种后期处理,与串行通信无关。
这没有意义:
You did not receive files. You receive data. How these data have to be interpreted depends on your specifications. If the data are text strings, you can show them in a text box or save them to a file. But that is some kind of post processing and is not related to the serial communication.
This makes no sense:
txtReceive.Text = serialPort1.ReadExisting();
txtReceive.Clear();
您正在设置文本框内容并在此后立即清除。
You are setting the text box content and clear it immediately thereafter.
这篇关于如何从串口C#接收文本文件..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!