Machsupport Forum
Mach Discussion => VB and the development of wizards => Topic started by: mecky on November 04, 2009, 09:08:32 PM
-
hey,
i have got a probably simple problem to solve in VB.
i got my m6start tool change macro to work but it appears that if i need to interrupt the tool change for whatever reason, by pressing esc., the macro continues a couple of lines down or in the second subroutine called. i am not quite sure about this.
is there any way to build an interrupt or escape loop/subroutine or something similar into a macro that is going to take care of this problem?
thanks
_mecky
here is the toolchange macro:
Sub Main()
oldtool = GetCurrentTool()
newtool = GetSelectedTool()
'newtool = 1
x = GetToolChangeStart( 0 )
y = GetToolChangeStart( 1 )
z = GetToolChangeStart( 2 )
maxtoolnum = 3
toolup = 0.0
If newtool = oldtool Then
Exit Sub
End If
While newtool > maxtoolnum
newtool = Question (" ey! there are only " & maxtoolnum)
Wend
code "G0 G53 Z" & toolup 'first operation, moving the tool up
While IsMoving()
Wend
Call ReturnTool(oldtool) 'return old tool
While IsMoving()
Wend
Call GrabTool(newtool) 'grab new tool
While IsMoving()
Wend
SetCurrentTool(newtool)
Code "G00 X" & x & "Y" & y
End Sub
Sub ReturnTool (ByVal ToolNumber As Integer)
Xclear = 141.5
toolup = 0.0
Select Case ToolNumber
Case = 1
Xpos = 144.6
Ypos = 2
Zpos = -10.68
Case = 2
Xpos = 144.6
Ypos = 4
Zpos = -10.68
Case = 3
Xpos = 144.7
Ypos = 6
Zpos = -10.64
End Select
Code "G53 G00 X" & Xclear & "Y" & YPos
While IsMoving()
Wend
Code "G53 G01 F20 Z" & Zpos
While IsMoving()
Wend
Code "G53 G01 F20 X" & Xpos
While IsMoving()
Wend
ActivateSignal(output3)
code "G04P2000"
While IsMoving()
Wend
Code "G53 G01 F20 Z" & toolup
While IsMoving()
Wend
End Sub
Sub GrabTool (ByVal ToolNumber As Integer)
Xclear = 141.5
toolup = 0.0
Select Case ToolNumber
Case = 1
Xpos = 144.6
Ypos = 2
Zpos = -10.68
Case = 2
Xpos = 144.6
Ypos = 4
Zpos = -10.68
Case = 3
Xpos = 144.7
Ypos = 6
Zpos = -10.64
End Select
Code "G53 X" & Xpos & "Y" & YPos
While IsMoving()
Wend
Code "G53 G01 F20 Z" & Zpos
While IsMoving()
Wend
code "G04P2000"
DeactivateSignal(output3)
code "G04P2000"
While IsMoving()
Wend
Code "G53 G01 F20 X" & Xclear
While IsMoving()
Wend
Code "G53 G01 F20 Z" & toolup
While IsMoving()
Wend
End Sub
Main
-
Hi
At first I thought, 'this should be quite simple' , but after scouring the documentation etc. I can see why you asked.
Despite providing a SetKeys() function to stuff the keyboard buffer and feed keys to the window with focus, the Cypress VBS does not have a corresponding GetKeys() function.
Historically, I would have used Int 9 in assembler to interrogate the keyboard buffer, when I used to program in Windows years ago.
Not much use in VBS though.
The main problem in any windowing system, is that mouse and keyboard input is directed and read in terms of which window has focus.
Usually this will be by way of this sort of psuedo code
winptr = GetWindowWithFocus()
KeyPressed = winptr->keyboardEvent.GetKey()
You want a system wide remit, to see what is in the keyboard buffer and act on an Escape sequence.
So far as I can ascertain, the way to read the keystrokes at a system level in Windows using VB, is to access the functions in the user32.dll, namely
GetAsyncKeyState()
The below code should test the keyboard buffer for an Escape key press, by calling EscapeKeyPressed() from within your code.
'''''Determining if Escape has been pressed (vbKeyEscape)
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Function EscapeKeyPressed() As Boolean
Dim bResult As Boolean
'Check First Key (need to call twice, the first call clears
'the keyboard buffer then second checks it's still pressed)
If VarType(vKey1) = vbString Then
bResult = CBool(GetAsyncKeyState(Asc(vbKeyEscape)))
bResult = CBool(GetAsyncKeyState(Asc(vbKeyEscape)))
Else
bResult = CBool(GetAsyncKeyState(vbKeyEscape))
bResult = CBool(GetAsyncKeyState(vbKeyEscape))
End If
EscapeKeyPressed = bResult
Exit Function
End Function
One possible place to put this test is inside your
While IsMoving() loops
You have a lot of these, and should in any case have something like sleep(100) inside them to allow processor time back to the system whilst waiting on the completion of a move.
Something like this might be a starting point
Dim abort As Boolean
abort = false
.
.
.
While IsMoving()
abort = EscapeKeyPressed()
If abort = true Then
'''Initiate whatever needs doing here
End If
sleep(100)
Wend
Alternately, you could simply periodically call EscapeKeyPressed() thoughout the code.
I am no expert on VB or VBS, but I have seen other code where the functions from system .dll s are declared and then the function called, so it should work.
Hopefully someone else can assist with the exact coding required to get it to work in VBS if you have problems.
regards
Melee
PS
I seem to remember reading that the Escape key was not actually a good key to use for anything in macros, because it would likely conflict with the hard coded keybindings in Mach.
You might want to use the Space Bar, commonly used as a pause signal in various apps, so vbKeySpace or Chr(0x20) maybe.
-
Hi Again,
I have now been able to do some experimentation and have come up with some much simplified code which works with Cypress VBS.
I named the code M989.m1s and ran from the MDI line and it works. It permanently loops displaying "Still Looping" on the message line.
When the space bar is pressed, it exits and displays "Loop Aborted".
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Const VK_SPACE = &H20
Sub Main()
Dim abort As Boolean
Message("")
Do
abort = HaltKeyPressed()
If abort = true Then
Message "Loop Aborted"
Exit Do
End If
Message "Still Looping"
Sleep(100)
Loop
End Sub
Main
Sub HaltKeyPressed()
Dim bResult As Boolean
If GetAsyncKeyState(VK_SPACE) < 0 Then
bresult = true
Else
bresult = false
End If
HaltKeyPressed = bResult
End Sub
You will note that I have specifically declared a constant of value 0x20h for the space key. Whilst VBS does not throw an error if you use vbKeySpace, I had no way of knowing if all those constants were accessible to VBS and when I ran the script in debug it did not seem to pick up the key press.
You should now be able to cut and paste the HaltKeyPressed() function into your macro and then use the return from it in your code to determine if the macro should halt.
Don't forget to cut and paste the declaration from user32.dll and the Const declaration too at the start of your code.
Hope this does what you want
regards
Melee
-
thanks a lot for this. i have not been able to implement your suggestions yet but will let you know some time this week how it turns out.
best
_mecky