Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all

I am sending and receiving text files over serial port in c# without any problem . But i want to send data according the memory size in text file "like sending 1 kb in 1 sec , next 1 kb in next second like that ".
I am working on this from long back didn't get the perfect solution till now

Here is my code
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)
     {

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

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

         }


What I have tried:

I tried StremReader function and Buffers also. But didn't get the result what i need can any one help me

i used this code also
List<string> list = new List<string>();
          using (StreamReader reader = new StreamReader(this.txtfile2.Text))
          {
              string line;
              while ((line = reader.ReadLine()) != null)
              {
                  list.Add(line); // Add to list.

}
}
Posted
Updated 8-Mar-17 0:55am

1 solution

Just read the file into a string. Then you can send 1 KB portions by using SubString(blockNumber * 1024, 1024) where blockNumber is incremented after writing each block until all blocks has been send.

Program flow:

Global objects:
C#
string buffer;
int blockNumber = -1;

Set File / click send file:
C#
buffer = System.IO.File.ReadAllText(this.openFileDialog1.FileName);
if (!buffer.IsNullOrEmpty())
{
    if (serialPort1.IsOpen == false)
    {
        this.serialPort1.Open();
    }
    blockNumber = 0;
    // start timer
}

Timer event function:
C#
if (blockNumber >= 0)
{
    string blockString = buffer.SubString(blockNumber * 1024, 1024);
    // Empty if all data has been send
    if (!blockString.IsNullOrEmpty())
    {
        serialPort1.Write(blockString);
        blockNumber++;
    }
    else
    //OR: if (blockString.IsNullOrEmpty() || blockString.Length() < 1024)
    {
        blockNumber = -1;
        buffer = "";
        // stop timer
    }
}
 
Share this answer
 
Comments
Member 12941572 8-Mar-17 7:36am    
i use your code inside the btnSendFile but i got an error at this place
 if (!buffer.IsNullOrEmpty()) 
and where should i write the code for timer event function ? inside any button or in main function ??
Jochen Arndt 8-Mar-17 7:50am    
My solution is just a skeleton / program flow and not ready for usage.
You have to implement it yourself using a timer (see https://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx).

It must be all wrapped into a class where the timer and the global objects are class members.

The code block "Set File / click send file" can be put into your butSetFile_Click() function as it is or into your butSendFile_Click() function when using the stored filename.

The timer event handler can be an OnTimedEvent() function (see the example from the above timer link).

See the example also for initialising, starting, and stopping the timer.

This is Quick Answer and I have no time to write and test all the code. We are unpaid volunteers here doing our normal job too.

Just try to do it yourself. You will also learn better when doing so. If you get stuck somewhere you can always raise a new question.

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