【问题标题】:How to send Date/Time to COM-Port via Powershell?如何通过 Powershell 将日期/时间发送到 COM 端口?
【发布时间】:2013-10-09 21:25:45
【问题描述】:

我只是坐在 windows cmd shell 前面并通过 powershell 打开一个 COM 端口以将一些信息发送到外部设备。如果我在 shell 中输入以下代码,一切正常:

    powershell
    $port=new-Object System.IO.Ports.SerialPort COM3,9600,None,8,one
    $port.open(
    $port.WriteLine("Hello world")
    $port.Close()

但现在我想向端口发送日期和时间,所以我想我只需要更改第 5 行:

    $port.WriteLine(Get-Date)

    $port.WriteLine($Get-Date)

但这不起作用(外壳用红色字母告诉我一些奇怪的东西),到目前为止我不知道如何做到这一点...... 谁能帮帮我?

【问题讨论】:

    标签: powershell time send


    【解决方案1】:

    这行得通吗? “红色字母中的奇怪东西”是一个错误。它说什么?

    $date = Get-Date
    $port.WriteLine($date)
    

    【讨论】:

    • 那行得通,非常感谢,这并不难 0:-) 我会投赞成票,但我没有足够的声誉 ;-)
    • $port.WriteLine((Get-Date)) 也应该可以工作。在将返回值传递给WriteLine 方法之前,括号内的集合在子shell 中执行Get-Date
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多