Click here to Skip to main content
15,392,858 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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));

          }

      }


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.
               }
           }
Posted
Updated 12-Jan-22 23:32pm
Comments
[no name] 3-Mar-17 9:15am
   
Why do you think that your "console" code would somehow not work in your Winforms code? take out the Console.WriteLine and the code would work just the same. There isn't a C# code for console applications and C# for Winforms, it's all C#.
Member 12941572 3-Mar-17 23:08pm
   
there is buttons in winforms as i mentioned code in the question. where do i insert the code ??
englebart 23-Jan-22 13:41pm
   
I think the Q is more about how to tie a form button event to the code that is already working as a Console app.

Next clarification might be instead of reading from a file, should the comm port output be calculated or come from a text control on the form.

Quote:
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).
   
Comments
Member 12941572 3-Mar-17 23:14pm
   
I think you didn't understand my question, i have to read the text file line by line and it is success in console application. What i need is the same operation will be done on winform application where do i insert the code
[no name] 4-Mar-17 7:38am
   
A 5, even OP Claims that you do not understand his question *lol*, but I'm pretty convinced you do.
CPallini 4-Mar-17 10:41am
   
:-) Thank you very much.
Member 12941572 4-Mar-17 8:11am
   
i didn't get my solution even now.
[no name] 4-Mar-17 10:51am
   
What do you expect exactly?
Graeme_Grant 4-Mar-17 20:24pm
   
Are you wanting us to write your code for you?
Member 12941572 4-Mar-17 23:54pm
   
it doesn't mean i am expecting code from you...
i tried what he said but the problem is not solved
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.
   
v2
Comments
Member 12941572 4-Mar-17 6:51am
   
didn't get even a single word :-)
[no name] 4-Mar-17 7:35am
   
He he, a 5 for mentioning the nostalgic part of the 90s and of course more important this handshaking stuff
Graeme_Grant 4-Mar-17 7:45am
   
Those were the black-box days... :)
Member 12941572 4-Mar-17 8:07am
   
Ha ha but i didn't get my answer till now please help me guys
[no name] 4-Mar-17 11:02am
   
Ha ha, and where you stuck?

It is simple stuff:
Sender:
Send...
Receiver:
Receive

...and mentioned a lot enough here: Receiver Needs to inform Sender when it is ready or not ready to receive. Either by Hardware handshaking or by Software handshaking xon/xoff
Member 12941572 4-Mar-17 23:56pm
   
yeah when i click on on send the total text file is transfered but i want to send line by line can you you please suggest me how it is ..?? i tried with stream Reader but didnt get the solution
Graeme_Grant 5-Mar-17 0:12am
   
Does the have chunks separated by a new line marker (CR, CR/LF, or LF)? If yes, simply use the string.Split method.
Member 12941572 5-Mar-17 0:13am
   
inside sending block or inside select file block
Graeme_Grant 5-Mar-17 0:16am
   
1. load file
2. split lines
3. iterate lines & send in a loop - foreach(...), for(...;...;...), etc...
Hello,

You can try this code. I think it will solve your problem.
C#
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.
   
v3
Comments
Member 12941572 4-Mar-17 5:30am
   
actually i didnt write any data while executing the program , i have to send a text file which have 400 lines i have to read one by one hope you understand what i said

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900