Solved

vb to foxpro conversion

Posted on 2011-03-04
8
841 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

778 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