1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PowerShellでシリアルコンソール

Last updated at Posted at 2024-02-02
# 作:mori-oh
# コードは以下の記事をベースにした。
# https://qiita.com/yapg57kon/items/58d7f47022b3e405b5f3

# 制御文字の内、今回使う分だけ定義。

[char]$ETX = 0x03
[char]$LF  = 0x09
[char]$CR  = 0x0a
[char]$NAK = 0x15
[char]$ESC = 0x1b
[char]$DEL = 0x7F

# 入力からVT100シーケンスや制御文字に変換
function InputKeyParser([System.ConsoleKeyInfo]$key) {
	if($key.Modifiers -eq 'Ctrl' -and $key.Key -eq 'C' ) { $return = $ETX }
	elseif($key.Key -eq 'UpArrow')    { $return = "${ESC}[A" }
	elseif($key.Key -eq 'DownArrow')  { $return = "${ESC}[B" }
	elseif($key.Key -eq 'RightArrow') { $return = "${ESC}[C" }
	elseif($key.Key -eq 'LeftArrow')  { $return = "${ESC}[D" }
	elseif($key.Key -eq 'Home')       { $return = "${ESC}[H" }
	elseif($key.Key -eq 'End')        { $return = "${ESC}[F" }
	elseif($key.Key -eq 'Insert')     { $return = "${ESC}[2~" }
	elseif($key.Key -eq 'Delete')     { $return = "${ESC}[3~" }
	elseif($key.Key -eq 'PageUp')     { $return = "${ESC}[5~" }
	elseif($key.Key -eq 'PageDown')   { $return = "${ESC}[6~" }
	else { $return = $key.KeyChar }
	return $return
}

# 各個人の設定を入れる
$PORT = "COM5"
$BAUDOT = 115200
$PARITY = [System.IO.Ports.Parity]::None
$ENCORDING = "UTF-8"
$DTR = $false
$RTS = $false
$NEWLINE_TX = $CR
$NEWLINE_RX = $CR
$NEWLINE_DISP = $CR
$HANDSHAKE = [System.IO.Ports.Handshake]::None

# Ctrl+Cの無効化
[Console]::TreatControlCAsInput = $true

# 設定の適用
$SerialPort = New-Object System.IO.Ports.SerialPort ( $PORT, $BAUDOT, $PARITY )
$SerialPort.DtrEnable = $DTR
$SerialPort.RtsEnable = $RTS
$SerialPort.Handshake = $HANDSHAKE
$SerialPort.NewLine = $NEWLINE_DISP
$SerialPort.Encoding = [System.Text.Encoding]::GetEncoding($ENCORDING)
$NlConverter = { 
	param([System.IO.Ports.SerialPort]$sender, [System.EventArgs]$e)
	Write-Host -NoNewline ($sender.ReadExisting()).Replace( $NEWLINE_RX, $NEWLINE_DISP )
}
$DispEvent = Register-ObjectEvent -InputObject $SerialPort -EventName "DataReceived" -Action $NlConverter

# コンソールを開く
Write-Host -NoNewline "Enter Console. Exit Command is [Alt+Q]"
$SerialPort.Open()
$SerialPort.Write($NAK)
$SerialPort.Write($NEWLINE_TX)

# メインループ
while ($true) {
	if ( -not [Console]::KeyAvailable ) { continue }
	$keyInput = [Console]::ReadKey($true)
	if ( $keyInput.Modifiers -eq 'Alt' -and $keyInput.Key -eq 'Q' ) { break }
	$sendChar = InputKeyParser( $keyInput )
	$SerialPort.Write( $sendChar )
}

# 終了処理
$SerialPort.Write($NAK)
$SerialPort.Close()
Unregister-Event $DispEvent.Name
Remove-Job $DispEvent.id
Write-Host "`nExit Console."
[Console]::TreatControlCAsInput = $false

参考にしたページ

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?