Machsupport Forum

Mach Discussion => General Mach Discussion => Topic started by: M250cnc on August 27, 2010, 12:00:10 PM

Title: VB Code On A Button Help Required
Post by: M250cnc on August 27, 2010, 12:00:10 PM
Mach version 3.42 on a lathe

I think these newer Mach versions are all Boolean and Double Bubble Trouble  ;D

I think anyone who knows what they are doing "Unlike Me" should be able to sort this mess out.

When i press the BUTTON nothing happens

X_Distance = GetUserDRO ( 2011 )
Code "G04 P.25"
Code "G01 X_Distance F.10"

Pretty Please
Title: Re: VB Code On A Button Help Required
Post by: ger21 on August 27, 2010, 12:18:20 PM
There may be two issues.
1) there's no Axis letter (X?)
2) I think the X_Dist needs to be converted into a string and combined into another string which would be the complete line.
Something like this, but I don't know the exact syntax, as I've never done this in Mach.

Movement = "G01 X" + cstr(X_Distance) + " F.10"
Code Movement
Title: Re: VB Code On A Button Help Required
Post by: M250cnc on August 27, 2010, 12:55:59 PM
Thanks Gerry

Yes i can see how not having the axis letter could be a problem  ::)

I just tried fixing that and i got nothing

I tried all of your suggestion, but still nothing  :'(
Title: Re: VB Code On A Button Help Required
Post by: ger21 on August 27, 2010, 01:15:41 PM
I'm not 100% sure you can do it like I describe in Mach3.

Here's some info from the VB Manual. Looks like it should work. Maybe the CStr is case sensitive??

Quote
CStr(expression)
Converts any valid expression to a String.
Example:
Sub Main
Dim Y As Integer
Y = 25
Print Y
If VarType(Y) = 2 Then
X = CStr(Y) 'converts Y To a Str
X = X + "hello" 'It is now possible to combine Y with strings
Print X
End If
End Sub
Title: Re: VB Code On A Button Help Required
Post by: ger21 on August 27, 2010, 01:37:03 PM
Try:

 Code (Movement)
Title: Re: VB Code On A Button Help Required
Post by: DaveCVI on August 27, 2010, 02:03:26 PM
HI,
the string concatenation operator in basic is the "&"

instead of
Code "G01 X_Distance F.10"

you want
Code "G01 X" &  X_Distance & " F.10"

But that is not yet quite good enough as X_Distance can turn into a string that has exponents etc. within it...

To solve that, I have a little helper routine that I use for this:
Code: [Select]
Function GCN(ByVal VBNum) As String
' GCodeNumber: a small util to take a double, format it and return it as a string
' this is needed so that all numbers put into strings that will be sent to Code will not
' have things of the form: X0.123456e-9   - which is not legal gcode format
GCN = Format(VBNum, "#########0.000000") ' 6 sig digits to right of decimal point should be enough
End Function ' GCN
so I would have written the initial line as
Code "G01 X" &  GCN(X_Distance) & " F.10"

Dave
Title: Re: VB Code On A Button Help Required
Post by: ger21 on August 27, 2010, 03:26:29 PM
Quote
HI,
the string concatenation operator in basic is the "&"
The Cypress manual shows that it's "+" ??

+ also works in VBA.
Title: Re: VB Code On A Button Help Required
Post by: DaveCVI on August 27, 2010, 04:20:01 PM
Hi,
Yes, there's more than one way to skin the cat -

I use '&' for concatenation in basic and never use '+'. 
It's enough of a habit that I had forgotten that '+' also works (usually) as given in the Cypress manual page 39:
'+' or '&' can be used for string concatenation

I scratched my head and eventualy remembered why I got into that habit; '+' as it is also an arithmetic operator.
By using '&' instead of '+',  I don't have to worry about cypress attempting to add two numbers together...  The '+', '-' and '&' operators all are shown with the same operator precedence (page 40).
Rather than test and determine cypress's  actions for odd corner cases, I found it simpler to avoid the issue -

From page 8:
"If a string and a number are concatenated the result is a string. To be sure your concatenation works regardless of the data type involved use the & operator. The & will not perform arithmetic on your numeric values it will simply concatenate them as if they were strings."

Dave

Title: Re: VB Code On A Button Help Required
Post by: M250cnc on August 27, 2010, 04:29:12 PM
Dave & Gerry

Thank you for you help, i will try it out tomorrow and I'm sure it will work and i will report back
Title: Re: VB Code On A Button Help Required
Post by: M250cnc on August 28, 2010, 11:21:41 AM

   GCN = Format(VBNum, "#########0.000000")      ' 6 sig digits to right of decimal point should be enough
Dave

Dave i tried with your helper but i got an error on the line in the quote

As i will be manually adding the number i will only have a max of two digits after the decimal

Here is the code

X_Distance = GetUserDRO ( 2011 )

Code "G04 P.25"

Code "G01 X" & (X_Distance)

I took away the feed rate in code so it runs at the the current feedrate in either G94 or G95 mode
Title: Re: VB Code On A Button Help Required
Post by: DaveCVI on August 28, 2010, 12:37:10 PM
Phil,

That's odd - below is a literal paste of a code fragment I just ran in mach to check -


Code: [Select]
Option Explicit
Dim X_Distance As Double

' entry point from mach
X_Distance = GetUserDRO (2011)

Code "G04 P.25"

MsgBox "hit OK to do " & Chr(13) &  "G01 X" & GCN(X_Distance)

Code "G01 X" & GCN(X_Distance)
Exit Sub


Function GCN(ByVal VBNum) As String
' GCodeNumber: a small util to take a VB double, format it and return it as a string
' this is needed so that all numbers ptu into strings that will be sent to CodeQ or CodeQW will not
' have things of the form: X0.123456e-9   - which is not legal gcode format
GCN = Format(VBNum, "#########0.00")
End Function ' GCN


Only difference was that I changed the format string to have only 2 digits to right of decimal as you requested.
I also added a debug msgbox just to see the string - looks fine, causes mach movement when the G01 is issued.

Note that I always use "option explicit" - that forces all variables to be declared before use.
This saves me from a typo creating a new variable of type variant on the fly. This should not make a difference to the code though.


Dave
Title: Re: VB Code On A Button Help Required
Post by: stirling on August 30, 2010, 07:10:30 AM
Guys - hope you don't mind me dropping in but there's no need for the conversions etc that you have going on. Exponent syntax is perfectly legal.

try this:

Code: [Select]
X_Distance = GetUserDRO ( 2011 )
Code "G04 P.25"
Code "G01 X" & X_Distance & " F.10"

Also you might want to take out thre Code "G04 P.25" as it serves no purpose so... try:

Code: [Select]
X_Distance = GetUserDRO ( 2011 )
Code "G01 X" & X_Distance & " F.10"

Of course depending on the following code (if any) you may need to add in a wait for the axis movement like:

Code: [Select]
X_Distance = GetUserDRO ( 2011 )
Code "G01 X" & X_Distance & " F.10"
while isMoving()
  sleep 100
wend

Ian