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:
<StructLayout(LayoutKind.Sequential, Pack:=4)> _
Public Structure IODBMR
Public datano_s As Short
Public dummy As Short
Public datano_e As Short
Public data() As IODBMR_data
End Structure
<StructLayout(LayoutKind.Sequential,Pack:=4)> _
Public Structure IODBMR_data
Public mcr_val As Integer
Public dec_val As Short
End Structure
Function Call:
' 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:
Private Sub BtnReadMacroRange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReadMacroRange.Click
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)
nRet = Focas1.cnc_rdmacror(FlibHndl, StartVar, EndVar, Length, MacroRange)
TextBox1.Text = CStr(nRet)
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,