problems with intptr indexing and marshaling data

  • Thread starter Tien Pham via .NET 247
  • Start date
T

Tien Pham via .NET 247

i am not sure if my last post went through. I am working on anapp that communicates to remote terminal units.
I have structure that contains my protocol. The stucture is asfollowed.

<StructLayout(LayoutKind.Sequential, Pack:=1)> Public StructureAG_Mem_Msg
Public dle As Byte
Public stx As Byte
Public addr As Short
Public status As Byte
Public command As Byte
Public subcode As Byte
Public param_cnt As Byte
Public byte_cnt_1 As Byte
Public mem_type As Byte
Public page_num As Byte
Public ofs_addr As Short
Public byte_cnt_2 As Byte
Public data_ptr As IntPtr
End Structure

The data_ptr in this structure is the data being sent to/from thehost to the rtu. I declared it as an intptr because the datacan vary from 1 thru 243 bytes. Thus, i can assign all the datato the header of my message but when i am manipulating orsetting the value for the data_ptr, i am having problems andgetting errors. The question to this post is how can i indexinto my dataptr and assign a byte value to a specific index ofthe ptr and how can i copy the whole structure onto my bufferfor transmission. I tried the following but i am not having anysuccess.

AG_Mem_Struct.dle = &H10
AG_Mem_Struct.stx = &H2
AG_Mem_Struct.status = 0
AG_Mem_Struct.command = &HB
AG_Mem_Struct.subcode = &H2
AG_Mem_Struct.param_cnt = &H2
AG_Mem_Struct.byte_cnt_1 = &H9
AG_Mem_Struct.mem_type = CByte(Me.cbx_MemType.SelectedIndex)
AG_Mem_Struct.page_num = CByte(Me.cbx_PageNum.SelectedIndex)
AG_Mem_Struct.ofs_addr = ((uiHex << 8) + (uiHex >> 8))
AG_Mem_Struct.byte_cnt_2 =System.Convert.ToByte(Me.txt_AddCnt.Text, 16)

' i tried to index into the intptr
Dim i As Integer
For i = 0 To AG_Mem_Struct.byte_cnt_2 - 1
'AG_Mem_Struct.data_ptr(i) =System.Convert.ToByte(Me.rtxt_Write.Text.Chars(i), 16)
Next

'AG_Mem_Struct.data(i) = &H10
'AG_Mem_Struct.data(i + 1) = &H3

TMP_Q_DEF.TX_COUNT = 18 + AG_Mem_Struct.byte_cnt_2
TMP_Q_DEF.RX_COUNT = &HFF

' marshal structure to ptr and then copy data from intptr tobuffer
Dim TempPtr As IntPtr = Marshal.AllocHGlobal(COMM_BUFFER_SIZE)
Marshal.StructureToPtr(AG_Mem_Struct, TempPtr, True)
Marshal.Copy(TempPtr, TMP_Q_DEF.TX_PTR, 0, TMP_Q_DEF.TX_COUNT)
Marshal.FreeHGlobal(TempPtr)

From: Tien Pham
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top