#powershell #serial-port #cmdlet
#powershell #последовательный порт #powershell-командлет
Вопрос:
я пытаюсь создать командлет powershell, который отправлял бы шестнадцатеричную строку на COM-порт, используя в качестве входного аргумента строку. у меня есть небольшой uC на COM, который управляет 2 реле на основе входной шестнадцатеричной строки. я совершенно новичок в powershell, так что …
однако из командной строки powershell, вводящей команды вручную, это работает. это то, что я придумал:
using System.Management.Automation; // Windows PowerShell assembly.
using System.IO.Ports.SerialPort;
namespace ControlPort
{
// Declare the class as a cmdlet and specify the
// appropriate verb and noun for the cmdlet name.
[Cmdlet(VerbsCommunications.Send, "ControlCOM11", SupportsTransactions=true)]
public class SendControlCommand : Cmdlet
{
$port= new-Object System.IO.Ports.SerialPort COM11,9600,None,8,one;
$port.open();
// Declare the parameters for the cmdlet.
[Parameter(Mandatory=true)]
public string command
{
get { return command; }
set { command = value; }
}
private string command;
// Override the ProcessRecord method to process
// the supplied user name and write out a
// greeting to the user by calling the WriteObject
// method.
protected override void ProcessRecord(command)
{
if (TransactionAvailable())
{
using (CurrentPSTransaction)
{
if(command == "RELAY2ON")
{
const [Byte[]] $relay2on = 0x52, 0x45, 0x4C, 0x41, 0x59, 0x32, 0x4F, 0x4E, 0x3B;
$port.Write($relay2on, 0, $relay2on.Count);
}
if(command == "RELAY2OFF")
{
const [Byte[]] $relay2off = 0x52, 0x45, 0x4C, 0x41, 0x59, 0x32, 0x4F, 0x46, 0x46, 0x3B;
$port.Write($relay2off, 0, $relay2off.Count);
}
}
}
}
}
}
любая помощь приветствуется.
Комментарии:
1. Вы можете легко преобразовать этот код c # в скомпилированный командлет PowerShell. Выполните следующие действия: red-gate.com/simple-talk/dotnet/net-development/… Однако некоторые из ваших инструкций сначала необходимо преобразовать в синтаксис c #, а именно объявления переменных. В каждой строке должны быть небольшие изменения.
2. спасибо за большую помощь!