解析MX-Component的Sample例程

时间:2024-04-13 07:54:32
验证通过AxActUtlTypeLib.AxActUtlType访问Q04UDV的CPU,硬件接口为以太网
新建一个VB.net  ,窗口程序,在工具栏里面添加 控件
如下图:

解析MX-Component的Sample例程
 
添加OPEN 和CLOSE函数

#Region "打开链接"
    Private Sub btn_Open_Click(sender As Object, e As EventArgs) Handles btn_Open.Click
        Dim iReturnCode As Integer              'Return code
        Dim iLogicalStationNumber As Integer    'LogicalStationNumber for ActUtlType

        ClearDisplay()

        Try
            If GetIntValue(txt_LogicalStationNumber, iLogicalStationNumber) = False Then
                'If failed, this process is end.
                Exit Sub
            End If
            AxActUtlType1.ActLogicalStationNumber = iLogicalStationNumber

            iReturnCode = AxActUtlType1.Open()

            If iReturnCode = 0 Then
                'When the Open method is succeeded, disable the TextBox of 'LogocalStationNumber'.
                txt_LogicalStationNumber.Enabled = False
            End If
        Catch exception As Exception
            MessageBox.Show(exception.Message, Name, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub

        End Try
        txt_ReturnCode.Text = String.Format("0x{0:x8} [HEX]", iReturnCode)
    End Sub
#End Region

**************
#Region "关闭链接"
    Private Sub btn_Close_Click(sender As Object, e As EventArgs) Handles btn_Close.Click
        Dim iReturnCode As Integer     'Return code
        ClearDisplay()


        Try
            iReturnCode = AxActUtlType1.Close()
            If iReturnCode = 0 Then
                txt_LogicalStationNumber.Enabled = True
            End If

        Catch exception As Exception
            MessageBox.Show(exception.Message, Name, MessageBoxButtons.OK, MessageBoxIcon.Error)


        End Try
        txt_ReturnCode.Text = String.Format("0x{0:x8} [HEX]", iReturnCode)
    End Sub
#End Region

****
测试可以通过

解析MX-Component的Sample例程

随机读取


#Region "随机写入 PLC里面的地址"
    Private Sub btn_ReadDeviceRandom2_Click(sender As Object, e As EventArgs) Handles btn_ReadDeviceRandom2.Click
        Dim iReturnCode As Integer              'Return code
        Dim szDeviceName As String = ""         'List data for 'DeviceName'
        Dim iNumberOfDeviceName As Integer = 0  'Data for 'DeviceSize'
        Dim sharrDeviceValue() As Short         'Data for 'DeviceValue'
        Dim szarrData() As String               'Array for 'Data'
        Dim iNumber As Integer                  'Loop counter

        'Displayed output data is cleared.
        ClearDisplay()

        'Get the list of 'DeviceName'.
        '  Join each line(StringType array) of 'DeviceName' by the separator '\n',
        '  and create a joined string data.
        szDeviceName = String.Join(vbLf, txt_DeviceNameRandom.Lines)

        If GetIntValue(txt_DeviceSizeRandom, iNumberOfDeviceName) = False Then
            'If failed, this process is end.
            Exit Sub
        End If

        ReDim sharrDeviceValue(iNumberOfDeviceName - 1)

        Try
            iReturnCode = AxActUtlType1.ReadDeviceRandom2(szDeviceName,  iNumberOfDeviceName, sharrDeviceValue(0))
        Catch exException As Exception
            MessageBox.Show(exException.Message, Name, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
        txt_ReturnCode.Text = String.Format("0x{0:x8} [HEX]", iReturnCode)

        If iReturnCode = 0 Then

            'Assign the array for the read data.
            ReDim szarrData(iNumberOfDeviceName - 1)

            'Copy the read data to the 'lpszarrData'.
            For iNumber = 0 To iNumberOfDeviceName - 1
                szarrData(iNumber) = sharrDeviceValue(iNumber).ToString()
            Next iNumber

            'Set the read data to the 'Data', and display it.
            txt_Data.Lines = szarrData
        End If

    End Sub

#End Region


解析MX-Component的Sample例程  
解析MX-Component的Sample例程

 

测试随机写入
#Region "随机写入 PLC里面的地址"
    Private Sub btn_WriteDeviceRandom2_Click(sender As Object, e As EventArgs) Handles btn_WriteDeviceRandom2.Click

        Dim iReturnCode As Integer              'Return code
        Dim szDeviceName As String = ""         'List data for 'DeviceName'
        Dim iNumberOfDeviceName As Integer = 0  'Data for 'DeviceSize'
        Dim sharrDeviceValue() As Short         'Data for 'DeviceValue'

        'Displayed output data is cleared.
        ClearDisplay()

        'Get the list of 'DeviceName'.
        'Join each line(StringType array) of 'DeviceName' by the separator '\n',
        'and create a joined string data.
        szDeviceName = String.Join(vbLf, txt_DeviceNameRandom.Lines)

        'Check the 'DeviceSize'.(If succeeded, the value is gotten.)
        If GetIntValue(txt_DeviceSizeRandom, iNumberOfDeviceName) = False Then
            'If failed, this process is end.
            Exit Sub
        End If

        'Check the 'DeviceValue'.(If succeeded, the value is gotten.)
        ReDim sharrDeviceValue(iNumberOfDeviceName - 1)
        If GetShortArray(txt_DeviceDataRandom, sharrDeviceValue) = False Then
            'If failed, this process is end.
            Exit Sub
        End If

        Try
            iReturnCode = AxActUtlType1.WriteDeviceRandom2(szDeviceName, iNumberOfDeviceName, sharrDeviceValue(0))
        Catch exception As Exception

            MessageBox.Show(exception.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub

        End Try
        txt_ReturnCode.Text = String.Format("0x{0:x8} [HEX]", iReturnCode)


    End Sub
#End Region

解析MX-Component的Sample例程
 
 
 
 

 

测试成块的数据读取
#Region "测试成块的数据读取"
    Private Sub btn_ReadDeviceBlock2_Click(sender As Object, e As EventArgs) Handles btn_ReadDeviceBlock2.Click

      Dim iReturnCode As Integer              'Return code
        Dim szDeviceName As String = ""         'List data for 'DeviceName'
        Dim iNumberOfDeviceName As Integer = 0  'Data for 'DeviceSize'
        Dim sharrDeviceValue() As Short         'Data for 'DeviceValue'
        Dim szarrData() As String               'Array for 'Data'
        Dim iNumber As Integer                  'Loop counter


        'Displayed output data is cleared.
        ClearDisplay()

        'Get the list of 'DeviceName'.
        '  Join each line(StringType array) of 'DeviceName' by the separator '\n',
        '  and create a joined string data.
        szDeviceName = String.Join(vbLf, txt_DeviceNameBlock.Lines)

        'Check the 'DeviceSize'.(If succeeded, the value is gotten.)
        If GetIntValue(txt_DeviceSizeBlock, iNumberOfDeviceName) = False Then
            'If failed, this process is end.
            Exit Sub
        End If

        'Assign the array for 'DeviceValue'.
        ReDim sharrDeviceValue(iNumberOfDeviceName - 1)

        Try
            iReturnCode = AxActUtlType1.ReadDeviceBlock2(szDeviceName, iNumberOfDeviceName, sharrDeviceValue(0))
        Catch exException As Exception
            MessageBox.Show(exException.Message, Name, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try


        txt_ReturnCode.Text = String.Format("0x{0:x8} [HEX]", iReturnCode)
        If iReturnCode = 0 Then

            'Assign array for the read data.
            ReDim szarrData(iNumberOfDeviceName - 1)

            'Copy the read data to the 'lpszarrData'.
            For iNumber = 0 To iNumberOfDeviceName - 1
                szarrData(iNumber) = sharrDeviceValue(iNumber).ToString()
            Next iNumber

            'Set the read data to the 'Data', and display it.
            txt_Data.Lines = szarrData
        End If


    End Sub
#End Region


解析MX-Component的Sample例程