I'm making a program that connects to a 3D printer, and sends/receives commands from it.
I'm trying to figure out how to take the output from $port.ReadLine() and send it to a TextBox (ReadOnly output in my GUI).
I have since edited this OP to reflect that I now am able to hit the TEST button, and the script spits out several lines of output to the host terminal. It's still missing some lines in between some of those output lines.
Also, I'd like this to happen in the background, not from a button, so that I can send other data to the port, and use the rest of the program at the same time.
I've looked at Start-Job quite a bit, but have yet to get it to work correctly, if at all.
My code:
Clear-Host
$port= new-Object System.IO.Ports.SerialPort COM6,115200,None,8,one
$port.Close()
$port.Open()
Start-Sleep -Milliseconds 1000
$port.ReadTimeout = 1000
function guibutton ($window,$todo,[string]$name="Butty",[string]$text="Butty",
[int]$x=300,[int]$y=200,[int]$w=80,[int]$h=30) {
$Button = New-Object System.Windows.Forms.Button
$Button = New-Object System.Windows.Forms.TextBox
$Button.Location = New-Object System.Drawing.Size($x,$y)
$Button.Size = New-Object System.Drawing.Size($w,$h)
#$Button.Name = $name
$Button.Text = $text
$Button.Add_Click($todo)
$window.Controls.Add($Button)
}
#Generated Form Function
function GenerateForm {
#region Import the Assemblies
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
#region Generated Form Objects
$MainMenu = New-Object System.Windows.Forms.Form
$MainMenu.FormBorderStyle = 'FixedDialog'
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#Set window size
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 140
$System_Drawing_Size.Width = 650
#Buttons
$TEST = New-Object System.Windows.Forms.Button
$EXIT = New-Object System.Windows.Forms.Button
$Connect = New-Object System.Windows.Forms.Button
$Disconnect = New-Object System.Windows.Forms.Button
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
#Unknown
#$handler_MainMenu_Load =
$OnLoadForm_StateCorrection= { $MainMenu.WindowState = $InitialFormWindowState }
#----------------------------------------------
#Buttons
#----------------------------------------------
$TEST_OnClick={
Write-Host "Port: " + $portnumber
Write-Host "Speed: " + $baudrate
Write-Host "Getting values from COM port..."
read-com
}
$Disconnect_OnClick={
Write-Host "Closing COM port..."
$port.Close()
}
$Connect_OnClick={
$port.Open()
}
$EXIT_OnClick={
$port.Close()
Write-Host "Closing..."
$MainMenu.close()
}
# SET PROGRAM-WIDE FONT HERE
$MainMenu.ClientSize = $System_Drawing_Size
$MainMenu.DataBindings.DefaultDataSourceUpdateMode = 0
$MainMenu.Text = "NAME"
$MainMenu.TopMost = $True
$MainMenu.add_Load($handler_form1_Load)
# == TEST== #
$TEST.Text = "TEST"
$TEST.UseVisualStyleBackColor = $True
$TEST.add_Click($TEST_OnClick)
$TEST.Location = New-Object System.Drawing.Point(300,100)
$TEST.Size = New-Object System.Drawing.Size(80, 30)
# == EXIT Button == #
$EXIT.Text = "EXIT"
$EXIT.UseVisualStyleBackColor = $True
$EXIT.add_Click($EXIT_OnClick)
$EXIT.Location = New-Object System.Drawing.Point(10,100)
$EXIT.Size = New-Object System.Drawing.Size(100, 30)
# == CONNECT Button == #
$Connect.Location = New-Object System.Drawing.Point(10, 35)
$Connect.Size = New-Object System.Drawing.Size(83, 30)
$Connect.Text = "Connect"
$Connect.add_Click($Connect_OnClick)
# == DISCONNECT Button == #
$Disconnect.Text = "Disconnect"
$Disconnect.UseVisualStyleBackColor = $True
$Disconnect.add_Click($Disconnect_OnClick)
$Disconnect.Location = New-Object System.Drawing.Point(98, 35)
$Disconnect.Size = New-Object System.Drawing.Size(83, 30)
# == COM list window NEW STYLE == #
$comlist = New-Object Windows.Forms.ComboBox
$comlist.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
$comlist.Items.AddRange([System.IO.Ports.SerialPort]::GetPortNames())
$comlist.location = new-object System.Drawing.Point(10,10)
$comlist.Size = New-Object System.Drawing.Size(70, 10)
$comlist.Add_SelectedValueChanged({
$script:portnumber = $comlist.SelectedItem
Write-Host $portnumber
})
# == Baudrate Selection Box == #
$comboBox1 = New-Object System.Windows.Forms.ComboBox
$comboBox1.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
$comboBox1.Location = New-Object System.Drawing.Point(85, 10)
$comboBox1.Size = New-Object System.Drawing.Size(98, 10)
$comboBox1.Items.AddRange(("250000","115200"))
$comboBox1.Add_SelectedValueChanged({
$script:baudrate = $comboBox1.SelectedItem
})
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(10,200)
$outputBox.Size = New-Object System.Drawing.Size(200,200)
$outputBox.MultiLine = $True
$outputBox.ScrollBars = "Vertical"
$MainMenu.Controls.Add($outputBox)
$MainMenu.Controls.Add($TEST)
$MainMenu.Controls.Add($EXIT)
$MainMenu.Controls.Add($Connect)
$MainMenu.Controls.Add($Disconnect)
$MainMenu.controls.Add($comlist)
$MainMenu.controls.Add($comboBox1)
# == Save and Open Form == #
$InitialFormWindowState = $MainMenu.WindowState
$MainMenu.add_Load($OnLoadForm_StateCorrection)
$MainMenu.ShowDialog()| Out-Null
} #End Function
function read-com {
$port.WriteLine("M503")
do {
Write-Host $port.ReadLine()
Start-Sleep -Milliseconds 10
} Until ($port.ReadLine() -eq "ok")
}
#Call the Function
GenerateForm
My read-com function is what's getting the info from the COM port.
Here is my output:
Getting values from COM port...
echo: G21 ; Units in mm (mm)
echo: M200 D1.75
echo:; Steps per unit:
echo:; Maximum feedrates (units/s):
echo:; Maximum Acceleration (units/s2):
echo:; Acceleration (units/s2): P<print_accel> R<retract_accel> T<travel_accel>
echo:; Advanced: B<min_segment_time_us> S<min_feedrate> T<min_travel_feedrate> J<junc_dev>
echo:; Home offset:
echo:; Auto Bed Leveling:
echo:; Material heatup parameters:
echo: M145 S1 H240 B90 F0
echo: M301 P21.00 I1.25 D86.00
echo: M851 X29.00 Y0.00 Z0.00
Exception calling "ReadLine" with "0" argument(s): "The operation has timed out."
At C:\Users\chevy\Desktop\GUI PROGRAM\TEST OUTPUT - Method 2.ps1:154 char:7
+ Write-Host $port.ReadLine()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : TimeoutException
Exception calling "ReadLine" with "0" argument(s): "The operation has timed out."
At C:\Users\chevy\Desktop\GUI PROGRAM\TEST OUTPUT - Method 2.ps1:155 char:7
+ Start-Sleep -Milliseconds 10
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : TimeoutException
Output SHOULD look like this:
Marlin 2.0.5.3
echo: Last Updated: 2020-03-31 | Author: (Bob Kuhn, Anet config)
echo:Compiled: May 13 2020
echo: Free Memory: 11848 PlannerBufferBytes: 1456
echo:V76 stored settings retrieved (640 bytes; crc 30778)
echo: G21 ; Units in mm (mm)
echo: M149 C ; Units in Celsius
echo:; Filament settings: Disabled
echo: M200 D1.75
echo: M200 D0
echo:; Steps per unit:
echo: M92 X100.00 Y100.00 Z401.00 E439.00
echo:; Maximum feedrates (units/s):
echo: M203 X400.00 Y400.00 Z8.00 E50.00
echo:; Maximum Acceleration (units/s2):
echo: M201 X1500.00 Y1500.00 Z100.00 E5000.00
echo:; Acceleration (units/s2): P<print_accel> R<retract_accel> T<travel_accel>
echo: M204 P500.00 R1000.00 T500.00
echo:; Advanced: B<min_segment_time_us> S<min_feedrate> T<min_travel_feedrate> J<junc_dev>
echo: M205 B20000.00 S0.00 T0.00 J0.10
echo:; Home offset:
echo: M206 X0.00 Y0.00 Z0.00
echo:; Auto Bed Leveling:
echo: M420 S0 Z0.00
echo:; Material heatup parameters:
echo: M145 S0 H190 B60 F0
echo: M145 S1 H240 B90 F0
echo:; PID settings:
echo: M301 P21.00 I1.25 D86.00
echo:; Z-Probe Offset (mm):
echo: M851 X29.00 Y0.00 Z0.00
echo:SD init fail