Attribute VB_Name = "Module1" ' ------------------------------------------------------ ' Created by chun@icpdas.com on Nov.29,2006 ' ------------------------------------------------------ Option Explicit ' Each variable must be declared well ' VB 6.0 format ' ------------------------------------------------------------------------------------- ' Single : 4 bytes : -3.4028235E+38 through -1.401298E-45 for negative values ' 1.401298E-45 through 3.4028235E+38 for positive values ' Long : 4 bytes : -2,147,483,648 through 2,147,483,647 (signed) ' Integer : 2 bytes : -32,768 through 32,767 (signed) ' Byte : 1 byte : 0 through 255 (unsigned) ' Boolean : : True or False ' ------------------------------------------------------------------------------------- ' vMap function or sub, Must using in INTEL format CPU ' +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ' ' Int32_to_Mbus_four_Byte( ) : Map Int32 value to become four Byte values in Modbus format ' Mbus_four_Byte_to_Int32( ) : Map four Byte values in Modbus format to become Int32 value ' Single_to_Mbus_four_Byte( ) : Map 32-bit Single floating point value to become four Byte values in Modbus format ' Mbus_four_Byte_to_Single( ) : Map four Byte values in Modbus format to become 32-bit Single floating point value ' Int16_to_Mbus_two_Byte( ) : Map Int16 value to become two Byte value in Modbus format ' Mbus_two_Byte_to_Int16( ) : Map two Byte value in Modbus format to become Int16 value ' Byte_to_Mbus_eight_Boolean( ) : Map Byte value to eight Boolean values in Modbus format ' Mbus_eight_Boolean_to_Byte( ) : Map eight Boolean values in Modbus format to Byte value ' +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) ' Map Int32 value to become four Byte values in Modbus format ' Parameter: ' Int32_val: Int32 value to be convertered ' Byte1 ~ 4: Byte1 to 4 in Modbus protocol order to store the result (LoWord Hi, LoWord Lo, HiWord Hi, HiWord Lo) Public Sub Int32_to_Mbus_four_Byte(ByVal Int32_val As Long, ByRef Byte1 As Byte, ByRef Byte2 As Byte, _ ByRef Byte3 As Byte, ByRef Byte4 As Byte) Dim B(8) As Byte Dim V As Long V = Int32_val CopyMemory B(0), V, 4 ' store the result Byte1 = B(1) ' LoWord Hi Byte2 = B(0) ' LoWord Lo Byte3 = B(3) ' HiWord Hi Byte4 = B(2) ' HiWord Lo End Sub ' Map four Byte values in Modbus format to become Int32 value ' Parameter: ' Byte1 ~ 4: Byte1 to 4 in Modbus protocol order to be converted (LoWord Hi, LoWord Lo, HiWord Hi, HiWord Lo) ' Int32_val: Int32 value-pointer which will store the result Public Sub Mbus_four_Byte_to_Int32(ByVal Byte1 As Byte, ByVal Byte2 As Byte, ByVal Byte3 As Byte, _ ByVal Byte4 As Byte, ByRef Int32_val As Long) Dim B(8) As Byte Dim V As Long B(0) = Byte2 ' Lowest byte B(1) = Byte1 B(2) = Byte4 B(3) = Byte3 ' Highest CopyMemory V, B(0), 4 ' store the result Int32_val = V End Sub ' Map 32-bit Single floating point value to become four Byte values in Modbus format ' Parameter: ' Single_val: 32-bit Single floating point value to be convertered ' Byte1 ~ 4: Byte1 to 4 in Modbus protocol order to store the result (LoWord Hi, LoWord Lo, HiWord Hi, HiWord Lo) ' True: succeed ! ' False: Failed. Public Function Single_to_Mbus_four_Byte(ByVal Single_val As Single, ByRef Byte1 As Byte, ByRef Byte2 As Byte, _ ByRef Byte3 As Byte, ByRef Byte4 As Byte) As Boolean Dim err As Boolean Dim B(8) As Byte Dim F As Single Single_to_Mbus_four_Byte = True ' int return value as True at the beginning ' activate error handler err = False On Error GoTo errhandler F = Single_val ' not a float value If IsNumeric(F) = False Then Single_to_Mbus_four_Byte = False ' return False Exit Function End If CopyMemory B(0), F, 4 ' store the result If err = False Then ' No error happens Byte1 = B(1) ' LoWord Hi Byte2 = B(0) ' LoWord Lo Byte3 = B(3) ' HiWord Hi Byte4 = B(2) ' HiWord Lo End If Exit Function ' If error happens errhandler: err = True Single_to_Mbus_four_Byte = False ' return False Exit Function Resume Next End Function ' Map four Byte values in Modbus format to become 32-bit Single floating point value ' Parameter: ' Byte1 ~ 4: Byte1 to 4 in Modbus protocol order to be converted (LoWord Hi, LoWord Lo, HiWord Hi, HiWord Lo) ' Single_val: 32-bit Single floating point value-pointer which will store the result ' True: succeed ! ' False: Failed. Public Function Mbus_four_Byte_to_Single(ByVal Byte1 As Byte, ByVal Byte2 As Byte, ByVal Byte3 As Byte, _ ByVal Byte4 As Byte, ByRef Single_val As Single) As Boolean Dim err As Boolean Dim B(8) As Byte Dim F As Single Mbus_four_Byte_to_Single = True ' int return value as True at the beginning ' activate error handler err = False On Error GoTo errhandler B(0) = Byte2 ' Lowest byte B(1) = Byte1 B(2) = Byte4 B(3) = Byte3 ' Highest CopyMemory F, B(0), 4 ' not a float value If IsNumeric(F) = False Then Mbus_four_Byte_to_Single = False ' return False Exit Function End If ' store the result If err = False Then Single_val = F End If Exit Function ' If error happens errhandler: err = True Mbus_four_Byte_to_Single = False ' return False Exit Function Resume Next End Function ' Map Int16 value to become two Byte value in Modbus format ' Parameter: ' Int16_val: Int16 value to be convertered ' Byte1 ~ 2: Byte1 to 2 in Modbus protocol order to store the result (HiByte, LoByte) Public Sub Int16_to_Mbus_two_Byte(ByVal Int16_val As Integer, ByRef Byte1 As Byte, ByRef Byte2 As Byte) Dim W As Integer Dim B(8) As Byte W = Int16_val CopyMemory B(0), W, 2 ' store the result Byte1 = B(1) ' HiByte Byte2 = B(0) ' LoByte End Sub ' Map two Byte value in Modbus format to become Int16 value ' Parameter: ' Byte1 ~ 2: Byte1 to 2 in Modbus protocol order to be converted (HiByte, LoByte) ' Int16_val: Int16 value-pointer which will store the result Public Sub Mbus_two_Byte_to_Int16(ByVal Byte1 As Byte, ByVal Byte2 As Byte, ByRef Int16_val As Integer) Dim W As Integer Dim B(8) As Byte B(0) = Byte2 ' Lowest byte B(1) = Byte1 CopyMemory W, B(0), 2 ' store the result Int16_val = W End Sub ' Map Byte value to eight Boolean values in Modbus format ' Parameter: ' Byte_val: Byte value to be converted (one Byte contains 8 bits) ' B1 to B8: 8 Boolean value-pointer which will store the result Public Sub Byte_to_Mbus_eight_Boolean(ByVal Byte_val As Byte, ByRef B1 As Boolean, ByRef B2 As Boolean, _ ByRef B3 As Boolean, ByRef B4 As Boolean, ByRef B5 As Boolean, _ ByRef B6 As Boolean, ByRef B7 As Boolean, ByRef B8 As Boolean) B1 = (Byte_val And &H1) > 0 B2 = (Byte_val And &H2) > 0 B3 = (Byte_val And &H4) > 0 B4 = (Byte_val And &H8) > 0 B5 = (Byte_val And &H10) > 0 B6 = (Byte_val And &H20) > 0 B7 = (Byte_val And &H40) > 0 B8 = (Byte_val And &H80) > 0 End Sub ' Map eight Boolean values in Modbus format to Byte value ' Parameter: ' B1 to B8 : 8 Boolean value be converted ' Byte_val : Byte value-pointer which will store the result Public Sub Mbus_eight_Boolean_to_Byte(ByVal B1 As Boolean, ByVal B2 As Boolean, _ ByVal B3 As Boolean, ByVal B4 As Boolean, ByVal B5 As Boolean, _ ByVal B6 As Boolean, ByVal B7 As Boolean, ByVal B8 As Boolean, _ ByRef Byte_val As Byte) Dim B_val As Byte B_val = 0 If B1 Then B_val = B_val Or &H1 End If If B2 Then B_val = B_val Or &H2 End If If B3 Then B_val = B_val Or &H4 End If If B4 Then B_val = B_val Or &H8 End If If B5 Then B_val = B_val Or &H10 End If If B6 Then B_val = B_val Or &H20 End If If B7 Then B_val = B_val Or &H40 End If If B8 Then B_val = B_val Or &H80 End If End Sub