我想通过串口C#逐行发送文本文件 [英] I want to send a text file line by line through serial port C#

查看:321
本文介绍了我想通过串口C#逐行发送文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家

我在c#中的Windows窗体应用程序中创建了一个GUI,用于通过串口发送和接收文本文件。并且它是成功我发送和接收没有任何问题



但我想通过串口逐行发送,以便我将逐行接收任何一个请帮助我



这里我发送文本文件的代码



Hi Guys
i created a GUI in Windows forms application in c# for sending and receiving Text files over serial port. And it is success i send and receive with out any problem

But i want to send line by line over serial port so that i will receive line by line can any one help me please

Here my code for sending text file

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

          this.txtfile2.Text = this.openFileDialog1.FileName;

      }


      private void butSendFile_Click(object sender, EventArgs e)
      {
          try  //sending files through serial port
          {

              if (serialPort1.IsOpen == false)
              {
                  this.serialPort1.Open();
              }

              serialPort1.Write(System.IO.File.ReadAllText(this.txtfile2.Text));

          }

      }





我是什么尝试过:



i通过使用代码解决了控制台应用程序中的问题,但我不知道它是如何在c#中为Windows窗体应用程序编写的br $>




What I have tried:

i solved the issue in console application by using the code ,but i don't know how it write for windows forms application in c#

List<string> list = new List<string>();
           using (StreamReader reader = new StreamReader(@"E:Text.txt"))
           {
               string line;
               while ((line = reader.ReadLine()) != null)
               {
                   list.Add(line); // Add to list.
                   Console.WriteLine(line); // Write to console.
               }
           }

推荐答案

引用:

while((line = reader.ReadLine())!= null)

{

while ((line = reader.ReadLine()) != null)
{

您可以使用相同的循环将行写入串口。



但是,从接收器的角度看,没有任何变化。它必须根据行终止符来区分不同的行。

You may use the same loop to write the lines to the serial port.

However, from the poin of view of the receiver, nothing changes. It has to discriminate the different lines based on the line terminator character(s).


这个问题让我回到了20世纪90年代初...



通过串行端口发送数据与TCP / IP不同。你需要添加一些握手。



这意味着当发送器向接收器发送数据包时,接收器需要回复ACK或NAK响应。如果发送器收到ACK,则发送下一个块;如果收到NAK,则重新发送。



此外,RS232上的数据可能会损坏。 TCP / IP也是如此,但TCP / IP自动重试中的传输层,而不是RS232。因此,最好用标题包装数据块(描述数据 - 例如:序列ID,数据类型等)和尾随校验和。如果校验和在接收端有效,则接收器响应,如果不是,则响应,然后是NAK。当接收方发送响应时,建议添加数据包ID和校验和标头/尾部包装器。这将使发送器更容易保持同步或识别是否收到损坏的数据。
This question takes me back to the early 1990s...

Sending data over serial ports is not the same as TCP/IP. You need to add some handshaking.

This means that when the transmitter sends a packet of data to the receiver, the receiver needs to reply with an ACK or NAK response. If an ACK was received by the transmitter, then send the next block; if a NAK was received, then resend.

Also, data over RS232 can get corrupted. Same happens with TCP/IP but the transport layer in TCP/IP auto retries, not RS232. So it is a good idea wrap the block of data with a header (describes the data - eg: sequence id, data type, etc...) and a trailing checksum. If the Checksum is valid at the receiving end, then the receiver responds and with an ACK, if not, then NAK. When the receiver sends the response, it is advisable to add the packet id and a checksum header/tail wrapper as well. This will make it easier for the transmitter to stay in sync or identify if corrupt data was received back.


你好,



您可以尝试此代码。我认为这将解决您的问题。

Hello,

You can try this code. I think it will solve your problem.
if (serialPort1.IsOpen == false)
{
    this.serialPort1.Open();
}    

using (StreamReader reader = new StreamReader(@"E:Text.txt"))
{
     string line;
     while ((line = reader.ReadLine()) != null)
     {
           //list.Add(line); // Add to list.
           serialPort1.Write(line + Environment.NewLine); 
     }
}



我认为此代码将满足您的目的。


I think this code will serve your purpose.


这篇关于我想通过串口C#逐行发送文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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