Documentation:Reference:VB - MdsWiki
Navigation
Personal tools

From MdsWiki

Jump to: navigation, search

An MDSplus component for Visual Basic has been developed and built using both VB.Net and VB 2005 and can be downloaded from the Downloadspage.

To use this component you would extract the files from the zip file and then in VB (or Excel etc.) use the Add Reference... menu item and select the MDSplusVBC.dll file from the zip file. Below is a snippet of VB code which uses the MDSplus component:

Imports MDSplusVBC
Module MyModule
   Sub Main()
       Dim status As Integer
       Dim mds = New MDSplus("alcdata.psfc.mit.edu")
       mds.MdsOpen("CMOD", 1060414001)
       Console.Write(mds.MdsValue("42", status))
   End Sub
End Module

The MDSplusVBC component provides the MDSplus object. When you create a new MDSplus object you can optionally pass the host name in as an argument to the MDSplus creation routine:

Dim mds = New MDSplus("alcdata.psfc.mit.edu")

or connect to the host later:

Dim mds = New MDSplus()
mds.Connect("alcdata.psfc.mit.edu")

Once an MDSplus object is connected to an mdsip server you can open MDSplus trees using the MdsOpen method:

mds.MdsOpen("CMOD",-1)

You can also evaluate MDSplus expressions using the MdsValue method:

Dim answer
Dim status as Integer
answer = mds.MdsValue("42+43",status)

You can pass optional parameters to be used in the expression as well:

Dim answer
Dim mynum as Integer
mynum = 42
answer = mds.MdsValue("$+43",mynum,status)

Note that the MdsValue object could return a variety of data types based on the expression given. The answer may be a scalar value of types such as Integer, Byte, Long, Single, Double or String or arrays of these quantities. You can write code to determine the type of answer if you are writing a general program. For example:

      Select Case TypeName(ans)
           Case "String"
               Answer.Text = ans
           Case "Byte"
               Answer.Text = ans
           Case "Integer"
               Answer.Text = ans
           Case "Long"
               Answer.Text = ans
           Case "Single"
               Answer.Text = ans
           Case "Double"
               Answer.Text = ans
           Case "Byte()"
               Dim val As Byte
               Dim arrstring As String
               arrstring = "[ "
               For Each val In ans
                   arrstring = arrstring + val.ToString + " , "
               Next
               Answer.Text = arrstring.Substring(0, arrstring.Length - 2) + "]"
           Case "Short()"
               Dim val As Int16
               Dim arrstring As String
               arrstring = "[ "
               For Each val In ans
                   arrstring = arrstring + val.ToString + " , "
               Next
               Answer.Text = arrstring.Substring(0, arrstring.Length - 2) + "]"
           Case "Integer()"
               Dim val As Int32
               Dim arrstring As String
               arrstring = "[ "
               For Each val In ans
                   arrstring = arrstring + val.ToString + " , "
               Next
               Answer.Text = arrstring.Substring(0, arrstring.Length - 2) + "]"
           Case "Long()"
               Dim val As Int64
               Dim arrstring As String
               arrstring = "[ "
               For Each val In ans
                   arrstring = arrstring + val.ToString + " , "
               Next
               Answer.Text = arrstring.Substring(0, arrstring.Length - 2) + "]"
           Case "Single()"
               Dim val As Single
               Dim arrstring As String
               arrstring = "[ "
               For Each val In ans
                   arrstring = arrstring + val.ToString + " , "
               Next
               Answer.Text = arrstring.Substring(0, arrstring.Length - 2) + "]"
           Case "Double()"
               Dim val As Double
               Dim arrstring As String
               arrstring = "[ "
               For Each val In ans
                   arrstring = arrstring + val.ToString + " , "
               Next
               Answer.Text = arrstring.Substring(0, arrstring.Length - 2) + "]"
       End Select

The MDSplus object also supports the MdsPut method which allows you to store data into an MDSplus tree:

mds.MdsPut("mynode","build_signal($,*,$)",values,timestamps,status)

Any of the MDSplus object methods may throw an exception if an error occurs. The exception will contain a test string describing the error. You can catch these exceptions in your program and handle them appropriately:

           Try
               mds.MdsOpen(Tree.Text, Shot.Text)
           Catch except
               Error.Text = except.Message
               Return
           End Try

RESTRICTIONS: Currently the MDSplus VB interface can only handle scalars or one-dimensional arrays of the following types:

Byte 8-bit Signed Integer
Short 16-bit Signed Integer
Integer 32-bit Signed Integer
Long 64-bit Signed Integer
Single 32-bit IEEE Floating Point
Double 64-bit IEEE Double-Precision Floating Point
String Text string
Byte() One-Dimensional Array
Short() One-Dimensional Array
Integer() One-Dimensional Array
Long() One-Dimensional Array
Single() One-Dimensional Array
Double() One-Dimensional Array

If you want to store a multi-dimensional array in MDSplus you can do this by passing a one-dimensional array to the MdsPut method but using the MDSplus "set_range" function in the expression. For example:

mds.MdsPut("mynode","build_signal(set_range(200,400,$),*,$)",values,timestamps,status)

Where values would be a one-dimensional array of 200 x 400 elements. MDSplus would reshape this one-dimensional array into a two-dimensional array using the set_range() function.