USB interface errors UPGRADING from VB6 to VB2008

Trying to get a USB device working in VB2008

Page 1 of 1

2 Replies - 6167 Views - Last Post: 05 February 2009 - 10:08 AM Rate Topic: -----

#1 jtr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 26-January 09

USB interface errors UPGRADING from VB6 to VB2008

Posted 26 January 2009 - 02:54 PM

I am trying to write a Windows application in VB2008 that needs to communicate with a USB device.
I am running Windows XP pro SP2.

The device is a QBridge-I2C-USB host adaptor . It uses an 8-bit PIC 18F2455 chip,
(I posted on microchip site too but no reply yet).

I downloaded some sample code from http://www.qprotos.com/downloads.htm, I got the QBridge-I2C-USB V1.41 installation files (application, driver, samples), from the website...

It gives 2 versions for the samples. One uses mpusbapi.dll, and the other one uses mpusbapi_vb.dll. I've tried both and the one that uses mpusbapi_vb.dll looks closer to what I need, so I will talk about that one.

Attached File  jtr_USB_Vb6_Original_code.txt (24.79K)
Number of downloads: 637

I hooked up the hardware including the QBridge-I2C-USB and the I2C device (a Taos TCS3414 light sensor),
and tested it using the downloaded program.

Everything worked fine, and I was able to manually write and read from the light sensor and get light readings from it with no problem.

Ok great... now I just have to write my own application to do all the stuff I want......

I can just start with the sample code and edit it from there, right ???

I attached all the original and updated code for reference, I will detail smaller chunks below,
(I tried to put everything in here before , butthe post got cut off at the bottom)

Well it turns out it is not quite that easy.....

The code is written in VB6, which I don't have. (I did find some old Visual Studio 5 discs that msdn sent to me years ago, but I figured that they were probably too old...)

So I went to the msdn site and downloaded VBexpress2008 and installed it, and when I went to open the project file, It insisted on UPGRADING the code to VB2008.....................

Ok.....So Iet VB2008 do its Upgrade thing, And this is what I got:

Attached File  jtr_USB__VB2008_upgraded_code.txt (24.33K)
Number of downloads: 645

When I tried to build it, I got these warnings and errors:
--------------------------------------------------------------------------------------------
(msg), File, Line, Description

(Warning), VB6_QBrige_frm.vb, 30, function 'CalXOrChksum'does't return a value on all code paths. A null reference exception could occur at run time when the result is used.

(Error), VB6_QBrige_bas.vb, 129, Name 'VarPtr' is not declared.

(Error), VB6_QBrige_bas.vb, 132, Name 'VarPtr' is not declared.
--------------------------------------------------------------------------------------------

so I went to look at the errors first, and found this error:
'UPGRADE_ISSUE: VarPtr function is not supported.


(This is the snippet with the VarPtr error, all the orginal and upgraded code is attached also)

'/////////////-< Code With VarPtr Errors >-BEGINNING////////////////////////


'/////////////////////////////////////////////////////////////////////////////
	'//
	'// A typical application would send a command to the target device and expect
	'// a response.
	'// SendReceivePacket is a wrapper function that facilitates the
	'// send command / read response paradigm
	'//
	'// SendData - pointer to data to be sent
	'// SendLength - length of data to be sent
	'// ReceiveData - Points to the buffer that receives the data read from the call
	'// ReceiveLength - Points to the number of bytes read
	'// SendDelay - time-out value for MPUSBWrite operation in milliseconds
	'// ReceiveDelay - time-out value for MPUSBRead operation in milliseconds
	'//
	
	Function SendReceivePacket(ByRef SendData() As Byte, ByRef SendLength As Integer, ByRef ReceiveData() As Byte, ByRef ReceiveLength As Integer, ByVal SendDelay As Integer, ByVal ReceiveDelay As Integer) As Integer
		
		
		Dim SentDataLength As Integer
		Dim ExpectedReceiveLength As Integer
		
		ExpectedReceiveLength = ReceiveLength
		
		If (myOutPipe <> INVALID_HANDLE_VALUE And myInPipe <> INVALID_HANDLE_VALUE) Then
			
			'UPGRADE_ISSUE: VarPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
			If (MPUSBWrite(myOutPipe, VarPtr(SendData(0)), SendLength, SentDataLength, SendDelay) = MPUSB_SUCCESS) Then
				
				'UPGRADE_ISSUE: VarPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
				If (MPUSBRead(myInPipe, VarPtr(ReceiveData(0)), ExpectedReceiveLength, ReceiveLength, ReceiveDelay) = MPUSB_SUCCESS) Then
					
					If (ReceiveLength <= ExpectedReceiveLength) Then
						SendReceivePacket = 1 '// Success!
						Exit Function
					End If
					
				Else
					CheckInvalidHandle()
				End If
			Else
				CheckInvalidHandle()
			End If
		End If
		
		SendReceivePacket = 0 '// Operation Failed
		
	End Function

'/////////////////////-< Code With VarPtr Errors >-END////////////////////////







I googled around to find stuff on pointers, And it looks like for pointer stuff, you are supposed to use "marshaling" for VB2008. ( Thats a new one for me..... makes me think of guys from microsoft with sheriff's badges )

So I dug around some more and I found this VarPtr replacement code on PlanetSourceCode:


< VarPtr replacement from PlanetSourceCode >
'/////////-< VarPtr replacement from PlanetSourceCode>-BEGINNING/////////


	Public Function VarPtr(ByVal o As Object) As Integer
		Dim GC As System.Runtime.InteropServices.GCHandle = _
		System.Runtime.InteropServices.GCHandle.Alloc(o, System.Runtime.InteropServices.GCHandleType.Pinned)

		Dim ret As Integer = GC.AddrOfPinnedObject.ToInt32

		GC.Free()

		Return ret

	End Function  
'//////////-< VarPtr replacement from PlanetSourceCode>-END//////////





I stuck in the VarPtr replacementCode before the Sub CheckInvalidHandle().

I can build it ok now, but I still get this warning:
--------------------------------------------------------------------------------------------
(msg), File, Line , Description

(warning), VB6_QBrige_frm.vb, 30, function 'CalXOrChksum'does't return a value on all code paths. a null reference exception could occurat run time when the result is used.

--------------------------------------------------------------------------------------------
(Ill put the Snippet with this error in the next post because I dont have enough room here.)


I copied mpusbapi_vb.dll into the Bin folder.
I Started Debug, and when I clicked :


Open USB button, I get: Msgbox(Windows Error 31),

When I click ok, I get: Failed To openI2C Port,

When I click ok, I get: Msgbox(Windows Error 997)

When I click ok, I get: Msgbox(Failed to set BaudRate)

-----------------------------------------------

I wrote some test code to check the pipes, and when I called OpenMPUSBDevice() I could see the handles:
myOutPipe = 1308
myInPipe = 1356

When I called CloseMPUSBDevice() I could see:
myOutPipe = -1
myInPipe = -1

and I could repeat opening and closing a few times ok.

When I unplug the USB and try it again, the msg box pops up and says "No Devices Connected",

so that seems to look ok ........

Now I must confess that this is the first time in my life that I have ever tried to make a USB device interface with anything.......
(The last time I played with VB was so long ago that I don't think USB had even been invented yet...)



Does anybody have any idea about:

1) How can I cure the the CalXOrChksum warning?

2) Is that VarPtr replacement snippet ok?

3) Am I missing something, or doing something really dumb?

4) Should I be using a different approach to the USB interface for VB2008?


Any help or advice would be much appreciated.....


Thx

jtr

This post has been edited by jtr: 26 January 2009 - 05:48 PM


Is This A Good Question/Topic? 1

Replies To: USB interface errors UPGRADING from VB6 to VB2008

#2 jtr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 26-January 09

Re: USB interface errors UPGRADING from VB6 to VB2008

Posted 31 January 2009 - 07:57 PM

OK, never mind..... I figured it out myself....

VarPtr(SendData(0)) needed to be changed to VarPtr(SendData) and
VarPtr(ReceiveData(0)) needed to be changed to VarPtr(ReceiveData)


I still get the checksum warnings, but it talks to the USB device ok now..

;)

jtr
Was This Post Helpful? 0
  • +
  • -

#3 jtr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 26-January 09

Re: USB interface errors UPGRADING from VB6 to VB2008

Posted 05 February 2009 - 10:08 AM

View Postjtr, on 31 Jan, 2009 - 06:57 PM, said:

OK, never mind..... I figured it out myself....

VarPtr(SendData(0)) needed to be changed to VarPtr(SendData) and
VarPtr(ReceiveData(0)) needed to be changed to VarPtr(ReceiveData)


I still get the checksum warnings, but it talks to the USB device ok now..

;)

jtr



I got rid of the Checksum warnings too....

I had to change function CalXOrChksum to a Public Sub instead of a Function.....

:P

jtr
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1