Click here to Skip to main content
15,792,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a app that I use to communicate with a Fanuc CNC controller with a PC front end using the Fanuc Focas Libraries. The Fanuc CNC controller controls the operation of a machine tool that is used to cut metal parts. The Focas libraries has a series of different functions I can call to read/write different variables from the Fanuc CNC.

We just recently purchased another Fanuc CNC controller. The existing controller we were using is no longer available so we had to upgrade to a newer version. Over the last few days, I have been testing my app on the new controller to make sure everything works properly. Most everything works fine but I have found a few functions that do not work. Below you will find the function and the structure that doesn't work:

Public Const MAX_AXIS = 8

' read absolute axis position
    Declare Function cnc_absolute Lib "FWLIB32.DLL" _
       (ByVal FlibHndl As Integer, ByVal a As Short, ByVal b As Short, ByRef c As ODBAXIS) As Short

<StructLayout(LayoutKind.Sequential,Pack:=4)> _
    Public Structure ODBAXIS
        Public dummy As Short       ' dummy
        Public type As Short        ' axis number
        <MarshalAs(UnmanagedType.ByValArray,SizeConst:=MAX_AXIS)> _
        Public data As Integer()    ' data value
    End Structure 'ODBAXIS

Private Sub rd_abs_pos()
       'The subroutine reads Absolute Position

       Dim nLen, i As Short 'length for position call
       Dim nValid_fig As Integer 'max number of valid figures
       Dim nDec_fig_in(Focas1.MAX_AXIS) As Short 'number of decimal places input to CNC
       Dim nDec_fig_out(Focas1.MAX_AXIS) As Short 'number of decimal places output to CNC
       Dim dTmp As Double
       Dim ax_abs As Focas1.ODBAXIS 'Absolute position variable

       nLen = 4 + (4 * Focas1.MAX_AXIS) 'calculate length of axis variable (38)

       'Set to read path 1
       nRet = Focas1.cnc_setpath(FlibHndl, 1)
       ErrPath.Text = nRet.ToString

       'read absolute position Path 1
       nRet = Focas1.cnc_absolute(FlibHndl, Focas1.ALL_AXES, nLen, ax_abs)
       ErrRet.Text = CStr(nRet)

       'find the # of decimal places
       nRet = Focas1.cnc_getfigure(FlibHndl, Focas1.AXIS_DATA, nValid_fig, nDec_fig_in, nDec_fig_out)
       ErrRet.Text = CStr(nRet)

       'update form with absolute position
           dTmp = CDbl(
           dTmp = dTmp / (10 ^ nDec_fig_in(i))
           Select Case i
               Case 0
                   XPos.Text = dTmp.ToString("0.0000")
               Case 1
                   'x slave
               Case 2
                   YPos.Text = dTmp.ToString("0.0000")
               Case 3
                   ZPos.Text = dTmp.ToString("0.0000")
           End Select
       Next i

   End Sub

After doing some investigating, i found out that this new controller has the capability to control more machine axis's compared to the old controller. Instead of 8, the new controller can control 32. After I changed the MAX_AXIS constant from 8 to 32, everything worked fine.

My current app runs on multiple machines with the old controller. The app is currently deployed via click once. Over the next few months, we plan to deploy multiple machines with the new controller. For simplicity sake, I was hoping I could run the same app on both controllers. I was planning on adding a new setting to my apps config file so I could modify the way the app runs depending on which controller it had. To make this work, I was going to change MAX_AXIS to a integer variable vs a const and set it to 8 for the old controller and then 32 for the new controller. When I made this change, I got an error in my structure saying that a constant expression is required. I'm not 100% sure why I'm getting this error but my guess is needs to know the exact size before it compiles.

I've read online that I could fix this issue by using the #IF statement to set this constant before compiling. If I do this, I am going to have to deploy 2 different apps. Then when I publish my app, I am going to have to make sure I publish the correct version to the correct deployment folder. Is there any other option I can use to make this work using the same executable?
Member 14666942 25-Nov-19 1:16am    
can you send your codes on
im trying to read /Write data from Fanuc controller but still coding is not enough so.

Why not just replace the variable size structure ODBAXIS with an integer array allocated at run time with a length one element longer than the expected amount of data, i.e. 9 or 33. The dummy and type fields would now be in element(0) and could be extracted out if actually needed. The data proper would start at element(1).

Share this answer
Of course there are better options. Moreover, deploying two applications instead of one would be a complete lame and the end of the maintainability of you product (if you even had it so far). Functionally, they are the same. Do you remember one of the most important principle in programming? "Don't Repeat Yourself":[^].

Practically, you should create a data model describing your machine capabilities. This should serve as the metadata, something describing what you are working with. Most likely, the Fanuc API has the universal method of identifying the machine model during runtime. Your software should find the detected model in your data model and read required parameters describing the equipment and capability. Where to store this model? There are many options: database, some files. It's most likely that you can create a comprehensive description stored in just one file. Then the best option could be using Data Contract. Please see:[^].

See also my past answers:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900