透過MX Component將DWord值取回後,要將它轉成數值顯示在畫面上,或是設定到PLC裏。
Public Function SetDWord(ByVal data As String, ByVal str As String, Optional ByVal plus As Double = 1, Optional ByVal shift As Double = 1) As String
Dim value As String = "0"
Dim s As String = CDbl(data) * plus / shift
Dim byarrBufferByte() As Byte
byarrBufferByte = BitConverter.GetBytes(CInt(s))
Try
If IsNumeric(data) And plus <> 0 Then
If str = "H" Then
value = BitConverter.ToInt16(byarrBufferByte, 2)
ElseIf str = "L" Then
value = BitConverter.ToInt16(byarrBufferByte, 0)
End If
Else
value = EMPTY_VALUE
End If
Catch ex As Exception
MsgBox(Err.Description(), MsgBoxStyle.Critical)
End Try
Return value
End Function
Public Function GetDWord(ByVal high_bit As Integer, ByVal low_bit As Integer, ByVal data_format As String, Optional ByVal shift As Double = 1, Optional ByVal plus As Double = 1) As String
Dim value As String = String.Empty
Dim byarrBufferByte(3) As Byte
Dim byarrTemp() As Byte
Try
If IsNumeric(high_bit) AndAlso IsNumeric(low_bit) And shift <> 0 Then
byarrTemp = BitConverter.GetBytes(low_bit)
byarrBufferByte(0) = byarrTemp(0)
byarrBufferByte(1) = byarrTemp(1)
byarrTemp = BitConverter.GetBytes(high_bit)
byarrBufferByte(2) = byarrTemp(0)
byarrBufferByte(3) = byarrTemp(1)
value = Format(BitConverter.ToInt32(byarrBufferByte, 0) * plus / shift, data_format)
Else
value = EMPTY_VALUE
End If
Catch ex As Exception
End Try
Return value
End Function
全站熱搜
留言列表