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

I have write a program to send text files over serial port in c# , and add a timer to calculate time.
When i start sending file the timer is on. But it won't stop when the transfer is completed can anyone tell me how to stop/pause the timer when file transferring is completed

private void butSendFileEle_Click(object sender, EventArgs e)
       {
           if (serialPort1.IsOpen == false)
           {
               try
               {
                   this.serialPort1.Open();
               }
               catch
               {
               }
           }
           timer1.Start();
           serialPort1.Write(System.IO.File.ReadAllText(this.txtfilePo.Text));

       }


private void timer1_Tick(object sender, EventArgs e)
       {
           label2.Text = min + ":" + sec + ":" + ms + ":" + us.ToString();
           ms++;
           if (us > 10)
           {
               ms++;
               us = 0;
           }
           else
           {
               us++;
           }
           if (ms > 100)
           {
               sec++;
               ms = 0;
           }

       }


This is my code

What I have tried:

Is this is the correct way ?? if not can anyone suggest me the correct way to calculate time duration

Thanks in advance
Posted
Updated 18-Aug-21 21:54pm
Comments
Dave Kreskowiak 17-Feb-17 10:10am    
Keep in mind that there is no such thing as something happening "automatically" for you. YOU Have to write the code to make these things happen.
Member 12990959 18-Feb-17 0:23am    
yeah I know no such thing happening automatically for me , and i know i have to write code there, that's what i am asking how to write and any suggestions from experts ..

Thank you
Philippe Mori 18-Feb-17 9:39am    
That code does not make much sense. Timer are not intended for timing purpose but to raise an event after some time is elapsed. Also timer are not very precise. If the system is busy, events will be late and your computed time would be shorter than expected. It might be a good idea to read documentation before writing code.

Stopwatch is so much better than any timer for this
here is an example which can make understand when to call stopwatch and how and stop it and print the time.
using System.Threading;
 public class Program
    {
        public static void Main(string[] args)
        {
               
            var stopwatch = new System.Diagnostics.Stopwatch();
                stopwatch.Start();
                Thread.Sleep(100);
            Console.WriteLine("Hello, world!");
            // Your code here.
                int cnt=0;
                for(int i=1; i<=100000;i++)
                {
                    cnt+=1;
                }
            stopwatch.Stop();
            TimeSpan ts = stopwatch.Elapsed;
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
            Console.WriteLine("RunTime " + elapsedTime);
            
        }            
            
        }
 
Share this answer
 
v2
Comments
Member 12990959 17-Feb-17 5:51am    
Thank you for Response Here i am sending text file not a direct messages how should i try this code and its a windows application not a console application
Ralf Meier 17-Feb-17 7:07am    
I don't understand you - the sample is quiet good.
Everything between Stopwatch.Start and Stopwatch.Stop is an Example to Show how it works. In this place you should enter your Data-Transfer-Method or it's Content ...
Deepak Geriani 17-Feb-17 8:00am    
Exactly what i Had try to explain is to write you logical code between the stopwatch start and stop and also comment that write "your code here" if you can see it properly
Member 12990959 18-Feb-17 0:33am    
yeah ..


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

if we use stopwatch start and stop in the same loop , it doesn't even start
Call:
C#
timer1.Stop();
When the transfer is complete - I.e. you have run out of data to send. Check the SerialPort.BytesToWrite Property (System.IO.Ports)[^] and it'll give you an estimate - not all the bytes will have arrived at the desitination yet, becasue the port driver will also have it's own buffer, and the port hardware chips generally have a small (8 bytes or so) buffer as well but you can't query those directly to get an accurate indication.
 
Share this answer
 
Comments
Member 12990959 17-Feb-17 5:31am    
yeah i write timer1.stop(); after 1st code then the timer doesn't even running
OriginalGriff 17-Feb-17 5:43am    
:sigh:
That's why I mentioned BytesToWrite - serial port data is buffered three times (four if you include the bit emitter itself) - so unless you check how much there is left to write, you won't know...
Another (very simple) possibility :
- bevor you write your data to the Serialport you read Now into a Variable (type Date)
- when you have finished the Serialport.write you build the difference (as timeSpan) between your Variable and Now - this Shows you also the elapsed time (in Seconds or Milliseconds - like you want)

That could look like this :
C#
System.DateTime myStart = System.DateTime.Now;

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

System.TimeSpan myTimespan = System.DateTime.Now.Subtract(myStart);
Int myElapsedTime = myTimespan.Seconds * 1000 + myTimespan.Milliseconds;
 
Share this answer
 
v4
Comments
Member 12990959 18-Feb-17 0:43am    
thank you for response

private void butSendFile_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen == false)
{
this.serialPort1.Open();
}
System.DateTime myStart = Now;

serialPort1.Write(System.IO.File.ReadAllText(this.txtfile2.Text));
TimeSpan myTimespan = Now.Subtract(myStart);
Int myElapsedTime = myTimespan.Seconds * 1000 + myTimespan.Milliseconds;

}

if i write code like this got 3 errors at
1) "now" doesnt exit in the current context
2) "int" could not be found

what should i do to remove those errors
Ralf Meier 18-Feb-17 4:27am    
I'm sorry ... that comes when writing code blind ...
Now comes out of the Namespace System - you should either use it (Using System ;) of write it before that instruction (I modified my Solution)
Member 12990959 18-Feb-17 4:40am    
Again it shows the same errors sir.
now and integer
Even i use (using.System;)
Ralf Meier 18-Feb-17 5:14am    
I'm sorry again,
in my code I'm sparely using that parts ...
It must be "System.DateTime.Now" - and INT must be right - see my code-changes
Member 12990959 18-Feb-17 6:06am    
It doesn't work This time no errors, But the port is not opened if i insert your code and communication is not processing

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