From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

Implementing the ni845xI2cWrite call in Visual Basic

Hi There,

 

As the subject states I'm trying to implement the I2CWrite call for the 8451 in Visual Basic.   The writeup for the C routines states:

 

Format

int32 ni845xI2cWrite (

uInt32 DeviceHandle,

uInt32 ConfigurationHandle,

uInt32 WriteSize,

uInt8 * pWriteData

);

Inputs

.

.

.uInt8 * pWriteData

A pointer to an array of bytes where the data to be written resides.

 

I'm getting hung up on the pWriteData pointer.

 

I have an Array called SensorWriteData and I can get a pointer to that array using the (undocumented)  VarPtrArray Function in VB6.  Trouble is the length of the pointer is 32bits (a Long Data Type) and this call is looking for an unsigned integer.

 

My Declare statement is as follows:

 

Declare Function ni845xI2cWriteData Lib "C:\WINDOWS\SYSTEM32\Ni845x.dll" (ByVal DeviceHandle as Long, ByVal ConfigurationHandle as Long, _

ByVal pWriteSize as Long, ByVal pWriteData as Long) as Integer

 

When I execute my program I get a non-zero result from the call and the ni845xStatusToString function gives me the message:

 

Unable to decipher status code 0x6551

Error - 3 - Status Code not defined.

 

If I change the pWriteData variable type to BYTE, I can't pass the point to the function because of a type mismatch: BYTE vs LONG.

 

While there doesn't appear to be any Visual Basic programers here, some insight into reconciling how a pointer could have a uInt8 data type would be helpful.

 

I'm kind of dead in the water at the moment.

 

Any thoughts would be greatly appreciated.

 

ChrisInLG 

0 Kudos
Message 1 of 5
(3,512 Views)

Hi Chris,

 

I'm not too familiar with VB but the uInt8 * is a pointer. This means the pointer is pointing to a memory address that contains a byte so the pointer itself is not of type byte. Try passing an array of bytes instead.

 

Hope this helps!

Chris T.
0 Kudos
Message 2 of 5
(3,499 Views)

Hi Chris,

 

I found a way of creating a pointer for the array and I passed it in the call.  The problem is that I get a non-zero result from the call.  Using the StatusToString routine, I get a message that the status code 25937 is not defined.  When I look at the data line with a scope, it would appear that the two bytes I am trying to send are indeed being sent  (Address + Command).  So I don't know at this point in time whether the problem is in the syntax of the call statement declaration or in my use of the of the call statement.

 

For example I want to be able to read a read-only register in the device on the I2C bus.  The sequence should be as such

 

Send Byte 1 address ;I2c address with R/_W bit set to 0

Byte 2      command ;ReadRegister command

 

Byte 3 address ;i2c address with R/_W bit set to 1

Read Byte 4 MSB of register

Byte 5 LSB of register

Byte 6 CRC

 

So my question is do I implement this using two calls, ni845xWrite and ni845xRead or with one call using ni8451xWriteRead.

 

The 8451 programming manual doesn't really describe what actually takes place at the hardware level with these calls.

 

Thanks again Chris.....you've been most helpful.

 

/ChrisInLG

0 Kudos
Message 3 of 5
(3,495 Views)

Hi Chris,

 

In your case, you should probably use the Write and Read. Since the Write Read will perform a Write command then a Read command right after it.

 

Hope that helps!

 

Chris T.
0 Kudos
Message 4 of 5
(3,476 Views)

Thanks to all who have responded to my questions.  I have been successful in using Visual Basic to access the 8451 I2C calls and all is working great.  

 

Thanks again...ChrisInLG

 

0 Kudos
Message 5 of 5
(3,382 Views)