内存数据的读写(PC)
0 Reply , Posted in 程序代码 on 2017 01, 2017
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''form'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare Sub CopyMemoryH2S Lib "kernel32" Alias
"RtlMoveMemory" (ByVal dst As String, ByVal src As Long, ByVal SIZE As
Long)
Private Declare Sub CopyMemoryS2H Lib "kernel32" Alias
"RtlMoveMemory" (ByVal dst As Long, ByVal src As String, ByVal SIZE As
Long)
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Dim mHandle(8) As Long
Dim length() As Long
Dim Num_Tpye As Integer
Dim PLCTypeEnum() As DataType
Const GMEM_FIXED = &H0
Private Sub Command1_Click()
Dim str2 As String * 256
Dim num As Integer
Text1.Text = ""
num = Combo1.ListIndex + 1
mHandle2 = mHandle(num)
CopyMemoryH2S str2, mHandle2, length(num)
Text1.Text = str2
' GlobalFree mHandle2
End Sub
Private Sub Form_Load()
''' add the type of PLC
Combo1.AddItem "type1"
Combo1.AddItem "type2"
Combo1.AddItem "type3"
Combo1.AddItem "type4"
Combo1.AddItem "type5"
' Combo1.AddItem "type6"
Combo1.ListIndex = 0
'''''add the data for the type
num_type = Combo1.ListCount
'''''''''data write to RAM
Dim astr() As String * 256
' Dim bstr() As String * 256
'ReDim mHandle1(num_type)
ReDim astr(num_type)
ReDim bstr(num_type)
ReDim length(num_type)
ReDim PLCTypeEnum(1 To num_type)
Initializtion_data
Text2.ForeColor = vbRed
Text2.Text = ""
Text1.Text = ""
For i = 1 To num_type
astr(i) = CStr(PLCTypeEnum(i).Voltage_Max) & "|" &
CStr(PLCTypeEnum(i).Voltage_Min) & "|" &
CStr(PLCTypeEnum(i).Current_Max) & "|" &
CStr(PLCTypeEnum(i).Current_Min) & "|" &
CStr(PLCTypeEnum(i).Power_Max) & "|" &
CStr(PLCTypeEnum(i).Power_Min)
length(i) = Len(Trim(astr(i)))
'分配一个length1字节的内存块
mHandle(i) = GlobalAlloc(GMEM_FIXED, length(i))
'将字符串中的内容拷贝到分配的内存块中
CopyMemoryS2H mHandle(i), CStr(astr(i)), length(i)
''''''''
Text2.Text = Text2.Text & vbCrLf & " 字符串的存储起始地址:" &
mHandle(i) & " 字符串长度:" & length(i) & vbCrLf & " 字符串为:"
& Trim(astr(i)) & vbCrLf
' CopyMemoryH2S str2, mHandle(i), length(num)
Next i
''''''''release the space of RAM
Erase PLCTypeEnum
End Sub
Private Sub Initializtion_data()
'ReDim PLCTypeEnum(num_type) '
PLCTypeEnum(1).Voltage_Max = 5.53
PLCTypeEnum(1).Voltage_Min = 5.08
PLCTypeEnum(1).Current_Max = 0.55
PLCTypeEnum(1).Current_Min = 0.55
PLCTypeEnum(1).Power_Max = 5.88
PLCTypeEnum(1).Power_Min = 4.66
PLCTypeEnum(2).Voltage_Max = 2.22
PLCTypeEnum(2).Voltage_Min = 2#
PLCTypeEnum(2).Current_Max = 0.25
PLCTypeEnum(2).Current_Min = 0.12
PLCTypeEnum(2).Power_Max = 5.28
PLCTypeEnum(2).Power_Min = 4.26
PLCTypeEnum(3).Voltage_Max = 3.53
PLCTypeEnum(3).Voltage_Min = 3.08
PLCTypeEnum(3).Current_Max = 0.35
PLCTypeEnum(3).Current_Min = 0.25
PLCTypeEnum(3).Power_Max = 5.28
PLCTypeEnum(3).Power_Min = 4.26
PLCTypeEnum(4).Voltage_Max = 4.53
PLCTypeEnum(4).Voltage_Min = 4.08
PLCTypeEnum(4).Current_Max = 0.45
PLCTypeEnum(4).Current_Min = 0.35
PLCTypeEnum(4).Power_Max = 5.48
PLCTypeEnum(4).Power_Min = 4.46
PLCTypeEnum(5).Voltage_Max = 5.85
PLCTypeEnum(5).Voltage_Min = 5.58
PLCTypeEnum(5).Current_Max = 0.55
PLCTypeEnum(5).Current_Min = 0.55
PLCTypeEnum(5).Power_Max = 5.88
PLCTypeEnum(5).Power_Min = 4.66
End Sub
Private Sub Form_Unload(Cancel As Integer)
For i = 1 To num_type
GlobalFree mHandle(i)
Next i
Erase mHandle
Erase length
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''models''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Type DataType
Voltage_Max As Single
Voltage_Min As Single
Current_Max As Single
Current_Min As Single
Power_Max As Single
Power_Min As Single
End Type