Solved

vb to foxpro conversion

Posted on 2011-03-04
8
813 Views
Last Modified: 2012-05-11
Can someone help me convert this function call to VFP?
I am calling CBW32.dll which is used with data aquisition card from omega.
VFP is not a specifically supported language but should work.
They give examples of how to use the DLL with C/C++, Visual Basic and Delphi.
the examples are:
C/C++
int cbDIn(int BoardNum, int PortNum, unsigned short *DataValue)
Visual Basic
Function cbDIn(ByVal BoardNum&, ByVal PortNum&, DataValue%) As Long
Delphi
function cbDIn(BoardNum:Integer; PortNum:Integer; var DataValue:Word):Integer; StdCall;
Arguments
BoardNum=1
PortNum=10
DataValue=Digital value returned here.
Returns
Error code or 0 if no errors
DataValue = place holder for the state of the port - (on or off)

When I issue this from VFP:
DECLARE integer cbDIn IN cbw32.dll integer,integer,integer
DataValue=0
nResult = cbDIn(1, 10, DataValue) &&foxpro whines "Declare DLL returned an exception"
If I issue:
DECLARE integer cbDIn IN cbw32.dll integer,integer,string
DataValue=""
nResult = cbDIn(1, 10, DataValue)

 nResult returns a 0 which means it completed okay but I cant get any value from DataValue. it remains "" no mater whether the port is on or off.
I know parameter 1 and 10 are correct. It seens to have a problem with DataValue.
I think it may have something to do with "unsigned short*DataValue" or maybe the dataValue is binary or somthing. I have been trying differrent stuff for two days and cant read the port.
Thanks
0
Comment
Question by:donrbrowning
  • 4
  • 3
8 Comments
 
LVL 41

Accepted Solution

by:
pcelba earned 500 total points
ID: 35040743
The DataValue is output parameter passed by reference, so you have to use @ prefix and reserve two bytes for result:

DECLARE integer cbDIn IN cbw32.dll integer,integer,string
DataValue = CHR(0)+CHR(0)
nResult = cbDIn(1, 10, @DataValue)

To convert string result into a number you may use following expression (hope the byte order is correct):

lnResult = ASC(DataValue) + 256 * ASC(SUBSTR(DataValue, 2, 1))

or CTOBIN() function: http://msdn.microsoft.com/en-us/library/sfw383zh(VS.80).aspx
0
 
LVL 41

Expert Comment

by:pcelba
ID: 35040777
One correction - Even the function declaration should contain reference prefix:

DECLARE integer cbDIn IN cbw32.dll integer,integer,@string
DataValue = CHR(0)+CHR(0)
nResult = cbDIn(1, 10, @DataValue)

0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 35042149
Yes, pcelba is right.

Anytime the c declaration has a pointer *something in it's parameters this translates to a parameter passed in by reference and not by value. The declaration of the parameter needs a @ and usage too.

Bye, Olaf.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 35042161
Correction: Not all pointers need call by reference, eg a const char* simply is a string to VFP, but you can nevertheless then also pass in the string by reference, it does not hurt, it just won't be modidfied.

Bye, Olaf.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 41

Expert Comment

by:pcelba
ID: 35043083
It seems the string formal parameter declaration does not require @ prefix because all strings are passed by reference. The function call then requires @ prefix before output parameter to allow return value propagation back to the passed variable.

But I did not test above assumptions.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 35045185
Yes, pavel, I already corrected myself. In thecase of the cbDIn() function there is no other string parameter though, I assume you talk about the general case of a string paramter, not used as output parameter.

Bye, Olaf.
0
 

Author Comment

by:donrbrowning
ID: 35056470
thanks to all,
Here is what worked
DECLARE integer cbDIn IN CBW32.dll integer,integer, integer @ DataVale
DataValue=0
=cbDIn(1, 10, @DataValue)
DataValue now returns 255 or 254 depending on the state of the port.
0
 
LVL 41

Expert Comment

by:pcelba
ID: 35056715
Good to see you've updated our solutions to your needs.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Microsoft Visual FoxPro (short VFP) is a programming language with it’s own IDE and database, ranking somewhat between Access and VB.NET + SQL Server (Express). Product Description: http://msdn.microsoft.com/en-us/vfoxpro/default.aspx (http://msd…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now