Kenneth M. Cruikshank
Simple Powershell script to redirect input

Kenneth M. Cruikshank
Kinect Projects

This page documents some projects using the Arduino prototype board.

Introduction

One way in which the simple Arduino boards (i.e., no attached SD card or network port) send informaton to the PC is with the console. The terminal sees the input stream on one of the computers COM ports (aka serial port).

Sometimes it is nice to take this data and stream it to a host computer and either save it, or perhaps also do some processing.

  1. Configure COM port
  2. Load Arduino IDE, and launch your program on the Arduino
  3. Exit the development evironment
  4. Run Powershell
  5. Move to the directory where the script is contained
  6. Run the script

The data stream will be re-directed from the COM port to a file and a stream. Note, you can also have data streamed directly to a database. The action section of the script can be replaces with almost anything your want.

The script

# Script assumes COM3 ... the first command lists the available ports           

# It also assumes a baud rate of 9600 bps, no parity, 8 bits, 1 stop bit

$portList = [System.IO.Ports.SerialPort]::getportnames()
$portList
$port= new-Object System.IO.Ports.SerialPort COM3,9600,None,8,one
$port.open()
Start-Sleep 2 # wait 2 seconds until Arduino is ready
$port.Write("93c") # writes your content to the serial connection
$myFile = New-Item "C:\Users\i1kc\Documents\ArduinoLog.txt"

try {
while($myinput = $port.ReadLine()) {
# Write the line to the console and the file ...
$myinput

# You could do some calculations here, or call other programs
$myinput | Write-File $myFile -Append
}
}

catch [TimeoutException] {
# Error handling code here
}

finally {
# Any cleanup code goes here
}


$port.Close() #closes serial connection

 

Geology Department
http://www.pdx.edu/geology

Copyright © 1994-2015 · K.M. Cruikshank ·
http://geomechanics.research.pdx.edu