将服务器时间以十进制格式写入串行端口 [英] write server time in decimal format to serial port

查看:71
本文介绍了将服务器时间以十进制格式写入串行端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个代码来将服务器时间发送到串行端口.在这里,我已经以字符串格式将时间发送到串行端口,并且可以正常工作.
但是我需要将服务器时间以十进制格式发送到串行端口.当我在程序中使用带注释的行时,出现错误,提示其格式不正确.如何将服务器时间以十进制格式发送到串行端口?

任何帮助都将得到应用.

谢谢

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);
         }

     }


    }
}

推荐答案

您的注释代码不起作用,因为SerialPort.Write方法不接受十进制"类型的参数.但是,即使它接受这种类型的数据,串行连接另一端的设备也将收到与发送字符串时相同的值.您应该找出串行端口另一侧的设备真正需要的数据时间格式.
代替使用净时间",也许可以使用"DateTime.ToString()"-在这里,您至少有机会正确格式化字符串.
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.


好,

如果您真的需要十进制数,则可以使用以下内容.

Ok,

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

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;
}



该代码将时间字符串从服务器转换为DateTime对象,将DateTime转换为双精度表示形式,然后将双精度转换为可以使用串行端口发送的字符串.



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.


感谢迅速的答案.

在这里,我使用了净时间"以便从服务器中提取时间.

另一端的设备(某些微控制器)仅准备接受十进制格式的时间.(由于此微控制器连接到串行端口的内存不足,无法进行转换)

有什么方法可以将十进制时间发送到串行端口?

谢谢
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


这篇关于将服务器时间以十进制格式写入串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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