Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a code to send server time to serial port.here I hav sent the time in string format to serial port and its working fine.
But I need to send the server time in decimal format to the serial port. When I use the commented lines in program it gives an error saying its incorrect format. how can i send server time in decimal format to serial port?

Any help would be appriciated.

Thanks

public partial class Form1 : Form
    {
        string output;
        //decimal outtime;
        SerialPort _serialPort = new SerialPort("COM1", 9600,  Parity.None, 8, StopBits.One);

        public Form1()
        {

            InitializeComponent();
             timer1.Enabled = true;
             _serialPort.Open();
        }

    public string getnettime()
    {
    System.Diagnostics.Process p = new System.Diagnostics.Process();p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.FileName = "net";
    p.StartInfo.Arguments = "time";
    p.Start();
    p.WaitForExit();
    output = p.StandardOutput.ReadToEnd();
    output=(output.Remove(0, 40));
    output =(output.Remove(5, 42));

    return output;
   }
     private void timer1_tick(object sender, EventArgs e)
     {
         //decimal dectime=0;
         try
         {
            getnettime();
            textBox1.Text = output;
            //dectime = System.Convert.ToDecimal(textBox1.Text);
            _serialPort.Write(output);
           // _serialPort.Write("dectime");
         }
         catch (Exception ex)
         {
             MessageBox.Show("An error occurred : " +
                    ex.Message, "Data transfer", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }

     }


    }
}
Posted

Your commented code is not working since the SerialPort.Write method does not accept a parameter of type "decimal". But even if it would accept this type of data, the device on the other side of your serial connection would receive the same value as when sending the string. You should find out which datatime format the device on the other side of the serial port really needs.
Instead of using "net time", maybe you could use "DateTime.ToString()" - Here you have at least the chance to format the string appropriately.
 
Share this answer
 
Comments
Espen Harlinn 15-Jan-11 10:58am    
5+ Good explaination
Ok,

if you really need it in decimal you could use the following.

C#
public string getnettime()
{
  System.Diagnostics.Process p = new System.Diagnostics.Process();
  p.StartInfo.UseShellExecute = false;
  p.StartInfo.RedirectStandardOutput = true;
  p.StartInfo.FileName = "net";
  p.StartInfo.Arguments = "time";
  p.Start();
  p.WaitForExit();
  output = p.StandardOutput.ReadToEnd();
  output=(output.Remove(0, 40));
  output =(output.Remove(5, 42));
  DateTime time = Convert.ToDateTime(output);
  double dtime = time.Hour + (time.Minute / 60.0);
  output = dtime.ToString();
  return output;
}


This code converts the time string from the server to a DateTime object, converts the DateTime to a double representation and then the double to a string which can be sent using the serial port.
 
Share this answer
 
v2
Comments
Espen Harlinn 15-Jan-11 10:57am    
5+ Good effort
Thanks for the prompt answer.

here i have used "net time" in order to extract the time from the server.

Device on the other side( some micro controller ) is only ready to accept time in decimal format.(since this micro controller connected to the serial port does not have enough memory to carry out the conversion)

Is there any way to send time in decimal to serial port ?

Thanks
 
Share this answer
 

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