VB2005 shared mem problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Galen Somerville

    #1

    VB2005 shared mem problem

    My VB2005 program has a DataReciever thread that recieves data from an
    Activex VB6 thread. The data comes from a USB device that delivers 10 bit
    data in a series of bytes.

    So the low byte can be 0 to 255 but the high byte can only be 0 to 4. What's
    happening is that quite often a zero high byte becomes 255 after it goes
    through shared memory.

    The bytes are in groups of 24 bytes.

    As you will see below, I added a check loop in the VB6 transmitter that will
    present a msgbox if the high byte is greater than 4. Never happens.

    I added the same feature to the data receiver and in a group of 24 bytes I
    get at least four or five MsgBox messages. The values are always 255 instead
    of 0.

    Anyone have a clue as to what's wrong?

    Galen


    VB2005
    Shared memory
    m_SharedFile = CreateFileMappi ng(New IntPtr(-1), sa,
    PageProtection. ReadWrite, 0, _
    MaxDataSize + 4, ChannelName + "_BUFFER")
    If (m_SharedFile = IntPtr.Zero) Then
    Throw CreateApplicati onException("Fa iled to create a file mapping to
    slot " _
    + ChannelName + "_BUFFER")
    End If

    m_SharedMem = MapViewOfFile(m _SharedFile, SECTION_MAP_WRI TE, 0, 0,
    MaxDataSize + 4)
    If (m_SharedMem = IntPtr.Zero) Then
    Throw CreateApplicati onException("Fa iled to create a mapping view
    for slot " + ChannelName + "_BUFFER")
    End If

    Recieve routine
    Dim arrSize As Integer = Marshal.ReadInt 32(m_SharedMem)
    Dim data(arrSize - 1) As Byte
    Dim i As Integer
    For i = 0 To arrSize - 1
    data(i) = Marshal.ReadByt e(New IntPtr(m_Shared Mem.ToInt32() + i +
    4))
    Next
    For i = 1 To arrSize - 1 Step 2
    If data(i) 4 Then
    MsgBox "Too big " & i & " " & data(i)
    End If
    Next
    RaiseEvent OnData(data)



    VB6 ActiveX
    Shared memory
    hSharedFile = CreateFileMappi ng(-1, sa, 4, 0, Quant + 4, ChannelName &
    "_BUFFER")
    hSharedMem = MapViewOfFile(h SharedFile, SECTION_MAP_WRI TE, 0, 0, Quant +
    4)

    Transmit routine
    Public Sub Transmit()
    On Error GoTo Transmit_Error
    CopyMemory ByVal hSharedMem, Quant, 4
    Dim i As Long
    For i = 0 To Quant - 1
    CopyMemory ByVal (hSharedMem + 4 + i), DatAry(i), 1
    Next
    SetEvent hEventData
    For i = 1 To Quant - 1 Step 2
    If DatAry(i) 4 Then
    MsgBox "Too big " & i & " " & DatAry(i)
    Exit For
    End If
    Next
    On Error GoTo 0
    Exit Sub

    Transmit_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
    Transmit"

    End Sub



  • Mattias Sjögren

    #2
    Re: VB2005 shared mem problem

    Galen,
    >So the low byte can be 0 to 255 but the high byte can only be 0 to 4. What's
    >happening is that quite often a zero high byte becomes 255 after it goes
    >through shared memory.
    Are all the other values (the low bytes and the array size) correct?

    >Anyone have a clue as to what's wrong?
    No, the code sure looks like it should work.

    I would just recommend that you read/write the entire array at once
    (with a single call to CopyMemory in the VB6 side and by using
    Marshal.Copy on the managed site) rather than byte by byte.


    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Galen Somerville

      #3
      Re: VB2005 shared mem problem

      Inline

      "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rgwrote in message
      news:ezYRhMsWHH A.5060@TK2MSFTN GP06.phx.gbl...
      Galen,
      >
      >>So the low byte can be 0 to 255 but the high byte can only be 0 to 4.
      >>What's
      >>happening is that quite often a zero high byte becomes 255 after it goes
      >>through shared memory.
      >
      Are all the other values (the low bytes and the array size) correct?
      >
      Yes, they appear to be correct and the array size is correct.
      >
      >>Anyone have a clue as to what's wrong?
      >
      No, the code sure looks like it should work.
      >
      I would just recommend that you read/write the entire array at once
      (with a single call to CopyMemory in the VB6 side and by using
      Marshal.Copy on the managed site) rather than byte by byte.
      >
      I would like to. The array size is written by naming the variable and
      showing #bytes as 4.
      Would I just give the array name and #bytes as 24 ??
      >
      Mattias
      >
      --
      Mattias Sjögren [C# MVP] mattias @ mvps.org
      http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      Please reply only to the newsgroup.
      Galen


      Comment

      • Galen Somerville

        #4
        Re: VB2005 shared mem problem

        The aheader I go, the behinder I get.

        Changed byte reads to array copy. Same problem, 0's sent to shared memory
        become 255's in managed code.

        I added some test lines to both the data transmitter and the data reciever
        as follows:
        VB2005

        Marshal.Copy(Ne w IntPtr(m_Shared Mem.ToInt32() + 4), bytAry, 0, 24)
        For i = 1 To arrSize - 1 Step 2
        TestAry(TestCou nt) = bytAry(i)
        TestCount = TestCount + 1
        If bytAry(i) = 255 Then bytAry(i) = 0
        Next
        RaiseEvent OnData()

        VB6 ActiveX

        For i = 1 To Quant - 1 Step 2
        If DatAry(i) <0 Then
        MsgBox "Not zero " & i & " " & DatAry(i)
        Exit For
        End If
        Next

        It turns out that all integer values from USB device are 255 and below. So
        the VB6 MsgBox never appears.

        But a look at the TestAry in VB2005 shows at least 30% of the high bytes are
        255 ???

        HELP ME

        Galen

        "Galen Somerville" <galen@communit y.nospamwrote in message
        news:efDeXAuWHH A.2284@TK2MSFTN GP03.phx.gbl...
        Inline
        >
        "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rgwrote in message
        news:ezYRhMsWHH A.5060@TK2MSFTN GP06.phx.gbl...
        >Galen,
        >>
        >>>So the low byte can be 0 to 255 but the high byte can only be 0 to 4.
        >>>What's
        >>>happening is that quite often a zero high byte becomes 255 after it goes
        >>>through shared memory.
        >>
        >Are all the other values (the low bytes and the array size) correct?
        >>
        Yes, they appear to be correct and the array size is correct.
        >
        >>
        >>>Anyone have a clue as to what's wrong?
        >>
        >No, the code sure looks like it should work.
        >>
        >I would just recommend that you read/write the entire array at once
        >(with a single call to CopyMemory in the VB6 side and by using
        >Marshal.Copy on the managed site) rather than byte by byte.
        >>
        I would like to. The array size is written by naming the variable and
        showing #bytes as 4.
        Would I just give the array name and #bytes as 24 ??
        >
        >>
        >Mattias
        >>
        >--
        >Mattias Sjögren [C# MVP] mattias @ mvps.org
        >http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        >Please reply only to the newsgroup.
        >
        Galen
        >
        >

        Comment

        • WenYuan Wang

          #5
          Re: VB2005 shared mem problem

          Hi Galen,

          It seem fine with your code. I cannot figure out the root cause just now,
          but we suggest you may write all binary data into file before sent them to
          DataReciever, and write all received binary data into another file after
          DataReciever received data. These two binary files should be same. Please
          compare them with "fc /b binarayfile1.da t binarayfile2.da t". This will tell
          you what wrong with them. Additionally, you may open two binary files with
          "UltraEdit" to drill down and trouble-shot the issue.

          About how to write binary data into binary file in VB.net
          Dim binWriter As New IO.BinaryWriter (System.IO.File .Create("c:\
          binarayfile2.da t"))
          binWriter.BaseS tream.Write(dat a,0,data.Length )
          binWriter.Close ()

          About how to write binary data into file in VB, please reference the
          following document.
          http://www.computing.net/programming...orum/1965.html
          [Subject: VB6 Read/Write Binary File]

          Hope this helps.
          Sincerely,
          Wen Yuan

          Comment

          • Galen Somerville

            #6
            Re: VB2005 shared mem problem

            Thanks, will give that a try.

            Galen

            ""WenYuan Wang"" <v-wywang@online.m icrosoft.comwro te in message
            news:Mm4lxyLXHH A.5872@TK2MSFTN GHUB02.phx.gbl. ..
            Hi Galen,
            >
            It seem fine with your code. I cannot figure out the root cause just now,
            but we suggest you may write all binary data into file before sent them to
            DataReciever, and write all received binary data into another file after
            DataReciever received data. These two binary files should be same. Please
            compare them with "fc /b binarayfile1.da t binarayfile2.da t". This will
            tell
            you what wrong with them. Additionally, you may open two binary files with
            "UltraEdit" to drill down and trouble-shot the issue.
            >
            About how to write binary data into binary file in VB.net
            Dim binWriter As New IO.BinaryWriter (System.IO.File .Create("c:\
            binarayfile2.da t"))
            binWriter.BaseS tream.Write(dat a,0,data.Length )
            binWriter.Close ()
            >
            About how to write binary data into file in VB, please reference the
            following document.
            http://www.computing.net/programming...orum/1965.html
            [Subject: VB6 Read/Write Binary File]
            >
            Hope this helps.
            Sincerely,
            Wen Yuan
            >

            Comment

            Working...