⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmwriteitem.frm

📁 vb开发的一个opcclient源代码
💻 FRM
📖 第 1 页 / 共 2 页
字号:
    DataTypes(7) = "Invalid Data Type for this example" ' Date
    DataTypes(8) = "String - 8 bit Signed Characters"
    DataTypes(9) = "Invalid Data Type for this example" ' Object
    DataTypes(10) = "Invalid Data Type for this example" ' Error
    DataTypes(11) = "Boolean - True or Flase"
    DataTypes(12) = "Invalid Data Type for this example" ' Variant
    DataTypes(13) = "Invalid Data Type for this example" ' Data Access
    DataTypes(14) = "Invalid Data Type for this example" ' Decimal
    DataTypes(15) = "Invalid Data Type for this example" ' Unspecified type
    DataTypes(16) = "Invalid Data Type for this example" ' Unspecified type
    DataTypes(17) = "Byte 8 bit Unsigned "
    
    ' Now that we have a list of data type descriptions
    ' here where we use them
    If Module1.SelectedOPCItem.GetItemCanonicalType < 18 Then
        ' For normal datatypes use the strin found in the array
        DataType.Text = DataTypes(Module1.SelectedOPCItem.GetItemCanonicalType)
    ElseIf (Module1.SelectedOPCItem.GetItemCanonicalType > 8192) And (Module1.SelectedOPCItem.GetItemCanonicalType < 8210) Then
        ' For arrays the data type will the value vbArray(8192) added to the normal datatype
        ' This simply removes the array type to get the normal data type then
        ' sticks the word "Array" on the end of teh datatypes text.
        DataType.Text = DataTypes(Module1.SelectedOPCItem.GetItemCanonicalType - 8192) + "Array"
    Else
        ' You may see this if the data type of the item is an unsigned data type
        ' in these cases you may not be able to do a write.  I have seen this
        ' occur when trying to write to an array of unsigned integers.
        ' Writes to a single unsigned value have worked just fine however so
        ' doesn't appear to be a clear rule of thumb here. The issue with
        ' writes to the unsigned array appears to be an issue in the Automation
        ' Wrapper itself.
        DataType.Text = "Possible Unsigned data type, Writes may not work"
    End If
    
    ' Enable the appropriate edit control for the given data type
    If Module1.SelectedOPCItem.GetItemCanonicalType = vbBoolean Then
        BooleanOn.Enabled = True
        BooleanOff.Enabled = True
        WriteValue.Enabled = False
    Else
        BooleanOn.Enabled = False
        BooleanOff.Enabled = False
        WriteValue.Enabled = True
        If Module1.SelectedOPCItem.GetItemCanonicalType <> vbString Then
            WriteValue.Text = "0"
        Else
            WriteValue.Text = "String Data"
        End If
    End If
    ValueChange = False
    
End Sub


' On each click of the Apply button write the current
' value to the OPC item.  By setting ValueChange to True
' the WriteNewValue function will write the data each time.
'
Private Sub ApplyValue_Click()
    ValueChange = True
    WriteNewValue
End Sub


' The next two function simply create a radio button effect
' between the BooleanOn and BooleanOff check boxes.
'
Private Sub BooleanOff_Click()
    BooleanOn.Value = False
    ' Make sure the OK button cna write the value if needed
    ValueChange = True
End Sub

Private Sub BooleanOn_Click()
    BooleanOff.Value = False
    ' Make sure the OK button cna write the value if needed
    ValueChange = True
End Sub


' The Deactivate event is used here as a cheap way
' of keeping the form on top of the application if
' the user clicks on the main window.  There may be
' other ways to do this but this works fairly well.
'
Private Sub Form_Deactivate()
    ' use the show memthod to force the form
    ' back to the front
    frmWriteItem.Show
End Sub


' On OK click call the WriteNewValue it will check to see if
' ValueChange flag is true.
'
Private Sub OkButton_Click()
    WriteNewValue
    ' Reenable the tree view
    fMainForm.tvTreeView.Enabled = True
    Unload Me
End Sub


' Each time the user changes the WriteValue text box
' set the ValueChange flag true so the OK button can
' cause the value to be written.
'
Private Sub WriteValue_Change()
    ValueChange = True
End Sub


' The WriteNewValue sub handles changing the value of
' a single OPC item.  Depending on the data type the
' WriteNewValue function will interpret the proper
' input control and write the data accordingly.  For
' Boolean, Numeric, and Boolean this is fairly straight
' forward.  For arrays its a little more complex.
' When the data type is not Boolean we need to see if
' the item is possibly an array. If it is not an array
' we can check for a String type and then send the new value
' as either a single numeric or a single string.
'
' For array I allow the values to be enter in a string of
' values separated by commas.  You can even enter commas
' with no values between them to skip over elements of the
' array.  To demonstrate, if you have an array of four 16
' bit integers you can enter a string in the WriteValue
' text box of 1,2,3,4 .  This will send the new values
' 1,2,3,4 to each of the four array elements.  If you enter
' the string ,,55, you will only change the value of
' element 3.
'
Private Sub WriteNewValue()
    Dim ValueToWrite As Variant
    Dim CurrentValue As Variant
    
    ' Don't write unless there has been a change
    If ValueChange = True Then
           
        If Module1.SelectedOPCItem.GetItemCanonicalType = vbBoolean Then
            ' Write the Boolean state.  Since the two check box controls'
            ' act as radio buttons we only need to look at the state
            ' of one of them.
            ValueToWrite = BooleanOn.Value
        Else
            ' Get the current item in its variant form
            CurrentValue = Module1.SelectedOPCItem.GetItemValue(OPCItemDirect)
            
            ' Check to see if it is an array
            If Not IsArray(CurrentValue) Then
                ' its not an array see if it is not a string
                If Module1.SelectedOPCItem.GetItemCanonicalType <> vbString Then
                    ' It is not a string so convert the value in the WriteValue text box
                    ValueToWrite = Val(WriteValue.Text)
                Else
                    ' It is a string so send the string as is.
                    ValueToWrite = WriteValue.Text
                End If
            Else
                ' When writing an array we first grab
                ' the CurrentValues this allows us to change
                ' only the items we want within the array.
                ValueToWrite = CurrentValue
                Dim i As Integer
                Dim Length As Integer
                Dim LastStartPos As Integer
                Dim LastEndPos As Integer
                Length = Len(WriteValue.Text)
                Dim ArrayValue As Variant
                LastStartPos = 1
                LastEndPos = 1
                For i = 0 To UBound(ValueToWrite)
                    LastEndPos = InStr(LastStartPos, WriteValue.Text, ",")
                    If LastEndPos = 0 Then
                        LastEndPos = Length + 1
                    End If
                    ' See if there is data for this element number by checking
                    ' the gap between the LastStartPos and LastEndPos.
                    If LastStartPos < LastEndPos Then
                        ' new value for element so load into the working array
                        ValueToWrite(i) = Val(Mid(WriteValue.Text, LastStartPos, LastEndPos - LastStartPos))
                    End If
                    LastStartPos = LastEndPos + 1
                Next i  ' try to get the next array element
            End If
        End If
        
        ' Now that a Variant datatype has been developed that contains
        ' the value to be written, send it. In this case we are directly
        ' calling the AsyncWriteOPCItem of the OPCGroupClasss on the
        ' selected group.
        Module1.SelectedOPCGroup.AsyncWriteOPCItem Module1.SelectedOPCItem, ValueToWrite
    End If
    ' Now that the value has been written clear the ValueChange flag.
    ValueChange = False
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -