473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 = 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

Feb 27 '07 #1
5 1442
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.
Feb 27 '07 #2
Inline

"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:ez**************@TK2MSFTNGP06.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
Feb 28 '07 #3
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

"Galen Somerville" <ga***@community.nospamwrote in message
news:ef**************@TK2MSFTNGP03.phx.gbl...
Inline

"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:ez**************@TK2MSFTNGP06.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


Feb 28 '07 #4
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...orum/1965.html
[Subject: VB6 Read/Write Binary File]

Hope this helps.
Sincerely,
Wen Yuan

Mar 2 '07 #5
Thanks, will give that a try.

Galen

""WenYuan Wang"" <v-******@online.microsoft.comwrote in message
news:Mm**************@TK2MSFTNGHUB02.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.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...orum/1965.html
[Subject: VB6 Read/Write Binary File]

Hope this helps.
Sincerely,
Wen Yuan

Mar 2 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Charles Hunt | last post by:
Hi, When running this code in VB2003 Sub guidtest() Dim gstring As String Dim gid As Guid
0
by: Larry Lard | last post by:
This came out of a thread explaining to "BK" about error BC42025 ("Access of shared member through an instance; qualifying expression will not be evaluated"); Frans Clasener then came up with...
0
by: MikeCS | last post by:
Problem: I wanted to implement what I did in VB6 where I would use a global structure and dimension an array of structures then the program could load data into it at start up. Global routines would...
1
by: MikeCS | last post by:
Problem: I wanted to implement what I did in VB6 where I would use a global structure and dimension an array of structures then the program could load data into it at start up. Global routines would...
7
by: Galen Somerville | last post by:
I have been converting a VB6 project to VB2005 for lo these many months. Most of my problems have been solved thanks to people like WanYuan Wang and Mattias Sjogren. But now the Graphics aspect is...
15
by: Aalaan | last post by:
I am presently a user of classic vb6 and hang out on those newsgroups. Some of you may be aware that there is a very anti MS and vb2005 feeling there. I have tried to get them to tell me which...
13
by: Javad | last post by:
Hello I know that I should get the information of windows internet connections by using "rasapi32.dll" library, and I also have some sample codes, but I can't make them work. My exact need is to...
3
by: =?Utf-8?B?QmVu?= | last post by:
Hi all, I am new to vb2005. I have two list boxes on a form. one on the left and the other on the right. The left contains a list of items. Between the two list boxes I have a button to add...
1
by: Vae07 | last post by:
Ok so here is a brief summary of my problem. I need a pop up form that submits input text box information to a pocket excel workbook upon a command botton click. Text box inputs are checked for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.