problems with intptr indexing and marshaling data

Discussion in 'ASP .Net Web Services' started by Tien Pham via .NET 247, Mar 30, 2005.

  1. 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

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>u1f74mf+gU2HXBPrAi7UZQ==</Id>
    Tien Pham via .NET 247, Mar 30, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BestNews

    how to cast system.intptr to struct

    BestNews, Sep 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    4,740
    BestNews
    Sep 3, 2003
  2. C
    Replies:
    0
    Views:
    496
  3. Emin
    Replies:
    4
    Views:
    408
    Paul McGuire
    Jan 12, 2007
  4. Replies:
    7
    Views:
    485
  5. Replies:
    1
    Views:
    761
    Bo Persson
    Aug 26, 2006
Loading...

Share This Page