Hey Gerry,
This kicked my butt for quite a while too, till Dezsoe showed me the way (
http://www.forum.cncdrive.com/viewtopic.php?f=14&t=84&p=15209&hilit=codesync#p15213). The quick and dirty answer is to use 'CodeSync', but you have to call it from new threads, and use a 'is moving' routine which is also threaded. If you call CodeSync from the form directly, it will lock up the program. Doing some of this stuff from macros was far easier than plugins...
Here is an example block of a 'move to 0' code I have in ProbeIt. It probably is sloppy and can be done much cleaner/better, but it works
This references a number of check boxes and other stuff, but you should be able to weed out what you need from this. You'll also note I leave in plenty of comments on things I tried that didn't work, etc, so I don't accidentally go back to what I did incorrectly at a later time...
First, this is run by the button on the form, to start the routine:
- Code: Select all
Private Sub btnGoToZero_Click(sender As Object, e As EventArgs) Handles btnGoToZero.Click
If Not cbGoTORot.Checked AndAlso Not cbGoToX.Checked AndAlso Not cbGoToY.Checked AndAlso Not cbGoToZ.Checked Then
Using New Centered_MessageBox(Me)
MessageBox.Show(Me, "Appears no axis is set for moving, no move To make!")
End Using
Return
Else
Try
If thrMyProc Is Nothing Then
abortflag = False
thrMyProc = New Thread(AddressOf GoToZeroThread)
thrMyProc.CurrentCulture = Thread.CurrentThread.CurrentCulture ' Stay in English
thrMyProc.Start()
' Me.btnProbeGrid.Text = "STOP !"
Else
'stop UCCNC first
UC.Stop()
abortflag = True
'thrMyProc.Abort() 'DONT DO THIS
thrMyProc = Nothing
End If
Catch ex As Exception
messagebox.show(Me, "Didn't work....")
End Try
End If
end sub
The above starts a new thread, pointed at running the 'move' routine, which is full of codesync calls:
- Code: Select all
Public Sub GoToZeroThread()
Dim zstart, xstart, ystart, rotstart, feed, safe, zretract, zend, xend, yend, rotend, xpoint, ypoint, zpoint, rotpoint As Double
Dim strMoveCode As String
' ABORT if needed to
If UC.GetLED(25) Or UC.GetLED(36) Or UC.GetLED(217) Or abortflag Then ' Check for feedhold and cycle stop and reset
Try
Using New Centered_MessageBox(Me)
MessageBox.Show(Me, "Aborting offset move, UCCNC in reset!")
End Using
UC.AddStatusmessage("Aborting offset move!")
thrMyProc = Nothing
Return
'thrMyProc.Abort()
Exit Sub
Catch ex As Exception
End Try
End If
'End abort block
xstart = UC.GetXpos
ystart = UC.GetYpos
zstart = UC.GetZpos
Try
If cbGoToX.Checked Then
xpoint = 0
End If
If cbGoToY.Checked Then
ypoint = 0
End If
If cbGoToZ.Checked Then
zpoint = 0
End If
If cbGoTORot.Checked Then
rotpoint = 0
End If
feed = nudGoTOVel.Value
Catch ex As Exception
End Try
strMoveCode = "F" & feed
'messagebox.show(me, strMoveCode)
UC.Codesync(strMoveCode)
While IsMoving()
Thread.Sleep(5)
End While
' Lift Z to safe height
If cbZSafeLift.Checked Then
GoToSafeZ()
Else
End If
'Z First:
If cbGoToZ.Checked AndAlso (zpoint > UC.GetZpos) Then
If feed <> 0 Then
strMoveCode = "G1 "
Else
strMoveCode = "G0 "
End If
strMoveCode = strMoveCode & " Z" & fn(zpoint)
UC.Codesync(strMoveCode)
While IsMoving()
Thread.Sleep(5)
End While
End If
'Move to location
If feed <> 0 Then
strMoveCode = "G1 "
Else
strMoveCode = "G0 "
End If
If cbGoToX.Checked Then
strMoveCode = strMoveCode & " X" & fn(xpoint)
End If
If cbGoToY.Checked Then
strMoveCode = strMoveCode & " Y" & fn(ypoint)
End If
'If cbGoToZ.Checked Then
' strMoveCode = strMoveCode
'End If
If cbGoTORot.Checked Then
If comboRotoraySelected.Text = "A" Then
strMoveCode = strMoveCode & " A" & fn(rotpoint)
ElseIf comboRotoraySelected.Text = "B" Then
strMoveCode = strMoveCode & " B" & fn(rotpoint)
ElseIf comboRotoraySelected.Text = "C" Then
strMoveCode = strMoveCode & " C" & fn(rotpoint)
End If
End If
'strMoveCode = strMoveCode & " F" & fn(feed)
UC.Codesync(strMoveCode)
While IsMoving()
Thread.Sleep(5)
End While
'strMoveCode = "G1 X" & fn(xpoint) & " Y" & fn(ypoint) & " F" & fn(feed)
If (cbZSafeLift.Checked AndAlso cbZSafeReturn.Checked) Or (Not cbZSafeLift.Checked AndAlso Not cbZSafeReturn.Checked) Then
If feed <> 0 Then
If cbGoToZ.Checked Then
strMoveCode = "G31 z" & fn(zpoint) & " F" & fn(feed)
Else
strMoveCode = "G31 z" & fn(zstart) & " F" & fn(feed)
End If
Else
If cbGoToZ.Checked Then
strMoveCode = "G0 z" & fn(zpoint)
Else
strMoveCode = "G0 z" & fn(zstart)
End If
End If
UC.Codesync(strMoveCode)
While IsMoving()
Thread.Sleep(5)
End While
End If
thrMyProc = Nothing
End Sub
Sub GoToSafeZ()
strMoveCode = "m216"
UC.Codesync(strMoveCode)
'UC.Callbutton(216) ' skips ahead..., don't use...
While IsMoving()
Thread.Sleep(5)
End While
End Sub
The above code also makes calls to a version of 'is moving' that is also threaded:
- Code: Select all
Private Function IsMoving() As Boolean
Dim thrIsMoving As New Thread(Sub() CheckIsMoving())
thrIsMoving.CurrentCulture = Thread.CurrentThread.CurrentCulture
thrIsMoving.Start()
While thrIsMoving.IsAlive
Thread.Sleep(1)
End While
Return IsMovingFlag
End Function
Private Sub CheckIsMoving()
IsMovingFlag = UC.IsMoving
End Sub
Note, I use thrMyProc as the name of the thread for any code that does a moving routine, and you'll notice I check to make sure it is 'nothing' before starting a new thread, and at the end of the routine, I kill thrMyProc and set it to nothing (or if an abort is needed, etc).
Let me know if that helps or any questions.
I'm not sure if codelist has a 'sync' version or not, ive not used the codelist. You may have to iterate over each item in the list and do a 'codesyn' on it instead for a plugin. something similar to:
- Code: Select all
for each str as string in gcodelist
codesync(str)
While IsMoving()
Thread.Sleep(5)
End While
next
Good luck!
regards,
Eric