VB2005 shared mem problem

G

Galen Somerville

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 = CreateFileMapping(New IntPtr(-1), sa,
PageProtection.ReadWrite, 0, _
MaxDataSize + 4, ChannelName + "_BUFFER")
If (m_SharedFile = IntPtr.Zero) Then
Throw CreateApplicationException("Failed to create a file mapping to
slot " _
+ ChannelName + "_BUFFER")
End If

m_SharedMem = MapViewOfFile(m_SharedFile, SECTION_MAP_WRITE, 0, 0,
MaxDataSize + 4)
If (m_SharedMem = IntPtr.Zero) Then
Throw CreateApplicationException("Failed to create a mapping view
for slot " + ChannelName + "_BUFFER")
End If

Recieve routine
Dim arrSize As Integer = Marshal.ReadInt32(m_SharedMem)
Dim data(arrSize - 1) As Byte
Dim i As Integer
For i = 0 To arrSize - 1
data(i) = Marshal.ReadByte(New IntPtr(m_SharedMem.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 = CreateFileMapping(-1, sa, 4, 0, Quant + 4, ChannelName &
"_BUFFER")
hSharedMem = MapViewOfFile(hSharedFile, SECTION_MAP_WRITE, 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
 
M

Mattias Sjögren

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
 
G

Galen Somerville

Inline

Mattias Sjögren said:
Galen,


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

Galen
 
G

Galen Somerville

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(New IntPtr(m_SharedMem.ToInt32() + 4), bytAry, 0, 24)
For i = 1 To arrSize - 1 Step 2
TestAry(TestCount) = 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
 
W

WenYuan Wang

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.dat binarayfile2.dat". 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.dat"))
binWriter.BaseStream.Write(data,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/wwwboard/forum/1965.html
[Subject: VB6 Read/Write Binary File]

Hope this helps.
Sincerely,
Wen Yuan
 

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

Similar Threads


Top