Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
First of all, this is my first post here so I hope I post this correctly. I've used this site many times to find answers to other issues but I have never had to post a question before.

I have a VB.net program 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 Fanuc controller has a series of macro variables used to store numeric values. I am currently able to call different Fanuc functions to read and write a single variable at a time.
My VB.net program currently reads several macro variables at a time via a timer so I am would like to limit the number of calls I make to the Fanuc. I noticed that the Fanuc also offers another function that can be used to either read or write a series of variables at once. I have been trying to get this function to work but I have not been having any luck.

The documentation I got from Fanuc has an example in it but it looks like its written in C++. I called tech support at Fanuc and they provided me with some VB examples files that look like they were written in code prior to VB6
Based on all this information, I created the following code. The structure the FANUC uses for this function has a nested structure and I have never done one of those before. I have tried writing the nested structure 2 ways. The C++ example I have showed the nested structure defined within the primary structure. The VB3 example showed the nested structure as its own structure that gets called by the primary structure. Both way didn't work. I think this where I'm having my issue because the other functions that I used to read/write single variables are not nested. The following is an example of the code I'm using:

Structures:
VB
<StructLayout(LayoutKind.Sequential, Pack:=4)> _
    Public Structure IODBMR
        Public datano_s As Short  ' start macro number 
        Public dummy As Short     ' dummy 
        Public datano_e As Short  ' end macro number 
        Public data() As IODBMR_data ' data
    End Structure
   
 <StructLayout(LayoutKind.Sequential,Pack:=4)> _
    Public Structure IODBMR_data
        Public mcr_val As Integer ' macro variable 
        Public dec_val As Short   ' decimal point 
    End Structure


Function Call:
vp
' read custom macro variables(area specified)
   Declare Function cnc_rdmacror Lib "FWLIB32.DLL" _
       ( ByVal FlibHndl As Integer, ByVal a As Short, ByVal b As Short, ByVal c As Short, ByRef d As IODBMR ) As Short


Code to read 5 variables at once:

VB
Private Sub BtnReadMacroRange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReadMacroRange.Click
        'Read a series of 5 variables from the Fanuc
        'StartVar = start macro variable number which is input via the GUI
        'End Var = ending macro variable number which is input via the GUI
        'Length = length of structure which is input via the GUI
        Try
            Dim StartVar As Integer = CInt(Me.TxtStartVar.Text)
            Dim EndVar As Integer = CInt(Me.TxtEndVar.Text)
            Dim Length As Integer
            Dim McrVal As Integer
            Dim DecVal As Integer
            Dim VariableNumber As Integer
            Dim Result As String = Nothing
            Dim MacroRange As New Focas1.IODBMR
            Length = CInt(Me.TxtReadMacroRangeLength.Text)

            'Call the Fanuc Function
            'FlibHndl needs to be obtained first
            nRet = Focas1.cnc_rdmacror(FlibHndl, StartVar, EndVar, Length, MacroRange)
            'MsgBox("FlibHndl = " & FlibHndl)
            TextBox1.Text = CStr(nRet)
            'Read the result of a single variable and write the info to the GUI
            For x As Integer = 0 To 4
                VariableNumber = StartVar + x
                McrVal = MacroRange.data(x).mcr_val
                DecVal = MacroRange.data(x).dec_val
                If McrVal = 0 And DecVal = -1 Then
                    Result = "Null"
                Else
                    Result = McrVal * 10 ^ -(DecVal)
                End If

                Select Case x
                    Case 0
                        Me.LblReadMacroRangeResult1.Text = VariableNumber
                        Me.TxtReadMacroRangeResult1.Text = Result
                    Case 1
                        Me.LblReadMacroRangeResult2.Text = VariableNumber
                        Me.TxtReadMacroRangeResult2.Text = Result
                    Case 2
                        Me.LblReadMacroRangeResult3.Text = VariableNumber
                        Me.TxtReadMacroRangeResult3.Text = Result
                    Case 3
                        Me.LblReadMacroRangeResult4.Text = VariableNumber
                        Me.TxtReadMacroRangeResult4.Text = Result
                    Case 4
                        Me.LblReadMacroRangeResult5.Text = VariableNumber
                        Me.TxtReadMacroRangeResult5.Text = Result
                End Select
            Next
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

When I try to run this code, the whole application crashes and gives me the dreaded "Sorry for the inconvience error" despite having the code wrapped in try/catch. I've been working with the techs at Fanuc but they don't seem to have many people that know a lot about VB.net. Can anyone find anything that I'm doing wrong?

Thanks,
Posted
Updated 4-Dec-17 16:32pm
v2
Comments
Simon_Whale 22-Jun-11 17:17pm    
If you stepped through the code where would it error?
Kschuler 22-Jun-11 17:31pm    
If you add an UnhandledException event to your application events, you might be able to get more information on the error. See here for how to:
http://msdn.microsoft.com/en-us/library/3a02k5s0(v=vs.90).aspx

Maybe my mind is vegging out, but where is "Focas1" defined? Are you issuing an Import somewhere else?
 
Share this answer
 
Comments
theskiguy 23-Jun-11 9:54am    
The structure and the function call are both in a public class called FOCAS1. Sorry I did not include that in my original post.

I also tried adding an unhandled exception event in my application events but I still get the "Sorry for the inconvience error"
Hi, here is mine example:
Imports System
Imports System.Runtime.InteropServices
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim odbm1 As New ODBM
        Dim IODBMR1 As New IODBMR
        Dim ret As Short
        Dim hndl As Integer
        cnc_allclibhndl(hndl)
        'Read a single variable from Fanuc
        'Start = 500
        'Length = 10
        ret = cnc_rdmacro(hndl, 500, 10, odbm1)
        If ret = 0 Then
            Label1.Text = odbm1.mcr_val.ToString
        Else
            Label1.Text = "ERR: " & ret.ToString
        End If
        'Read a series of 5 variables from Fanuc
        'Start = 500
        'End   = 505
        'Length = 8+8*6
        ret = cnc_rdmacror(hndl, 500, 505, 56, IODBMR1)
        If ret = 0 Then
            Label2.Text = IODBMR1.data.data1.mcr_val
        Else
            Label2.Text = "ERR: " & ret.ToString
        End If
    End Sub
    <structlayout(layoutkind.sequential,> _
   Public Structure ODBM
        Public datano As Short    ' variable number 
        Public dummy As Short     ' dummy 
        Public mcr_val As Integer ' macro variable 
        Public dec_val As Short   ' decimal point 
    End Structure
    <structlayout(layoutkind.sequential,> _
    Public Structure IODBMR_data
        Public mcr_val As Integer ' macro variable 
        Public dec_val As Short   ' decimal point 
    End Structure
    <structlayout(layoutkind.sequential,> _
    Public Structure ODBMVINF
        Public use_no1 As Short
        Public use_no2 As Short
    End Structure
    <structlayout(layoutkind.sequential,> _
    Public Structure IODBMR1
        Public data1 As IODBMR_data
        Public data2 As IODBMR_data
        Public data3 As IODBMR_data
        Public data4 As IODBMR_data
        Public data5 As IODBMR_data
    End Structure  ' In case that the number of data is 5 

    <structlayout(layoutkind.sequential,> _
  Public Structure IODBMR
        Public datano_s As Short  ' start macro number 
        Public dummy As Short     ' dummy 
        Public datano_e As Short  ' end macro number 
        Public data As IODBMR1
    End Structure
    Declare Function cnc_rdmacro Lib "Fwlib64.dll" _
       (ByVal FlibHndl As Integer, ByVal a As Short, ByVal b As Short, ByRef c As ODBM) As Short
    Declare Function cnc_rdmacror Lib "Fwlib64.dll" _
       (ByVal FlibHndl As Integer, ByVal a As Short, ByVal b As Short, ByVal c As Short, ByRef d As IODBMR) As Short
    Declare Function cnc_allclibhndl Lib "FWLIB64.DLL" _
        (ByRef FlibHndl As Integer) As Short
End Class
 
Share this answer
 
v3
Comments
theskiguy 8-Jul-11 10:11am    
That worked, I just had to use the Fwlib32.dll rather then the FwLib64.dll in the function call. You must be on a newer controller then the Fanuc F150i. The only issue I got is that the IODBMR1 structure needs to be hard coded to the number of variables you want to read. In the above example, the IODBMR1 structure is set to read 5 variables because it holds 5 IODBMR_data structures. What do I do if i want to read 10 variables one time and 50 variables the next time. Is there a way to code the IODBMR1 structure so it can hold a random amount of IODBMR_data structures?

Thanks,
muhamad yudy 30-Nov-17 3:20am    
Hi Sir,
I've tried a way to communicate with Fanuc Model I16i and the coding I made was wrong. if sir permit to share project source to me.
muhamad.yudy084@gmail.com
s.ralev 27-Jul-11 10:07am    
Sorry for my late answer. Ofcourse you can make an array structure:
Public Structure IODBMR1
Public data() As IODBMR_data
End Structure

somewhere in code:

ReDim IODBMR1.data(N) ' N-number of macro variable to read

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