I am using a PC (Windows10) to send data to Arduino over USB (serial port) and it runs correctly. So I wrote a powershell script like below:
$serial = new-Object System.IO.Ports.SerialPort COM8,9600,None,8,one
$serial.open()
$serial.WriteLine("1")
$serial.close()
The problem is when I restart my PC because after restarting a few times the Arduino gets a new serial port and my script is set to COM8, then it does not work!
So using powershell, how can I know which serial port the Arduino is connected after restarting the PC?
How to change this powershell script to automatically identify the serial port that the Arduino is connected? Thanks
Try this. Note, I don't actually have something connected to a serial port to test.
You will need to look through the $SerialPorts
to see if there is something to identify the proper port and then tweak the Where-Object -Property Description -eq 'Arduino stuff'
so it match what you identified.
$SerialPorts = Get-CimInstance -Class Win32_SerialPort | Select-Object Name, Description, DeviceID
# This is the part where you need to tweak the Where-Object statement to match what you are looking for
$ArduinoPort = $SerialPorts | Where-Object -Property Description -eq 'Arduino stuff' | Select -ExpandProperty DeviceID
$serial = new-Object System.IO.Ports.SerialPort $ArduinoPort, 9600, None, 8, one
$serial.open()
Additional note
You can remove the | Select-Object Name, Description, DeviceID
if you don't find any information that can help you identify which is the arduino port so that you get all the properties instead (and check from there if anything can be used to pinpoint which serial port to use)