K8074 calling DLL from vb.net

Could someone help me with the correct code for accessing the k8074.dll in visualbasic.net?

for instance i have tried:

Public Declare Function OpenDevice Lib “K8074.dll” (ByVal szPort)

and then calling it:

If OpenDevice(“COM6”) <> True then

etc.

but i get the error

Pinvoke restriction: cannot return variants

its probably something simple but i’m a newbie with VB.NET

Anyone?

Cheers

Al

Function declarations:

Public Class K8074Native

  Declare Function OpenDevice Lib "K8074.dll" (ByVal portName As String) As Integer
  Declare Function DisplayAddress Lib "K8074.dll" (ByVal deviceId As Integer) As Boolean
  Declare Sub CloseDevice Lib "K8074.dll" (ByVal deviceId As Integer)

End Class

Sample code:

...
Dim deviceId As Integer = K8074Native.OpenDevice("COM5")
K8074Native.DisplayAddress(deviceId)
K8074Native.CloseDevice(deviceId)
...

Make sure K8074.dll is ‘visible’, eg the application can find it. The best way to achieve this is by putting the dll in your %windir%system32 folder.

Thanks VEL448

I now have the following declarations:

[code]Declare Function OpenDevice Lib “K8074.dll” _
(ByVal portName As String) As Integer

Declare Sub EmergencyStop Lib "K8074.dll" _
(ByVal deviceId As Integer)

Declare Sub SendCommand Lib "K8074.dll" _
(ByVal deviceId As Integer, ByVal address As Byte, ByVal cmd As Byte, ByVal param As Byte)

Declare Function DisplayAddress Lib "K8074.dll" _
(ByVal deviceId As Integer) As Integer

Declare Sub SetRelay Lib "K8074.dll" _
(ByVal deviceId As Integer, ByVal address As Byte, ByVal relayid As Integer)

Declare Sub ClearRelay Lib "K8074.dll" _
(ByVal deviceId As Integer, ByVal address As Byte, ByVal relayid As Integer)

Declare Sub ToggleRelay Lib "K8074.dll" _
(ByVal deviceId As Integer, ByVal address As Byte, ByVal relayid As Integer)

Declare Sub SetAddress Lib "K8074.dll" _
(ByVal deviceId As Integer, ByVal oldAddress As Byte, ByVal newAddress As Byte)

Declare Sub ForceAddressReset Lib "K8074.dll" _
(ByVal deviceId As Integer)

Declare Sub SendByte Lib "K8074.dll" _
(ByVal deviceId As Integer, ByVal address As Byte, ByVal bitmask As Byte)

Declare Sub CloseDevice Lib "K8074.dll" _
(ByVal deviceId As Integer)

Declare Sub GetDllVersion Lib "K8074.dll" ()[/code]

Now I can connect to the K8074 successfully, read it’s address, change it’s address and close the device, but the only way i can get it to switch the VM119 relay on or off is by using:

SendCommand(m_device, m_address, CByte(80), 49)

which I have adapted from the VS.NET demo project for the K8074. The code is inside a timer which is enabled on the button mousedown and disabled on the button mouseup Here’s the code from the vs.net demo:

private void TimerMom_Tick(object sender, EventArgs e) { K8074Native.SendCommand(m_device, (Byte)(cbAddress.SelectedIndex + 1), 0x50, (Byte)(48+m_MomIdx)); }

SetRelay, ToggleRelay, ClearRelay and EmergencyStop do not seem to have any effect, either in my code OR in the demo project.

In the K8074 API guide PDF, hex values for the CMD parameter of SendCommand are listed as:

[quote]
#define K8074_SET_ADDRESS 0x41 // ‘A’
#define K8074_SEND_BYTE 0x42 // ‘B’
#define K8074_CLEAR_RELAY 0x43 // ‘C’
#define K8074_DISPLAY_ADDRESS 0x44 // ‘D’
#define K8074_EMERGENCY_STOP 0x45 // ‘E’
#define K8074_FORCE_RESET 0x46 // ‘F’
#define K8074_SET_RELAY 0x53 // ‘S’
#define K8074_TOGGLE_RELAY 0x54 // ‘T’ [/quote]

in my adapted code, CByte(80) represents as hex 0x50 (or &h50 in vb.net) yet this value is not listed as a value in the above list - Is it an undocumented command?

Also, why in the PDF document does it say:

yet in my working code the param value is 49?

Is this problem something to do with controlling the VM119 as opposed to the 8 channel relay board? I have tried putting SetRelay and ClearRelay inside timers but that doesn’t work

On another related note, the document www.vellemanprojects.com/downloads/0/us … _k8074.pdf
is possibly corrupt as i have not been able to open it with any viewer.

Apologies for the long post!

Al

Please do not use SendCommand. All functions in the dll expect the integer values 1…8 as relay numbers. Internally these relay numbers are translated into their ASCII equivalent (49…56) and then sent to the K8074 using SendCommand. SendCommand was only included in the dll to provide a means of sending raw packets to the K8074. I will remove this function in the future to avoid confusion.

All of these functions return a boolean value to indicate success. However, your methods are declared as ‘Sub’, returning no value. Try adjusting your code like this:

...
Declare Function EmergencyStop Lib "K8074.dll" _
    (ByVal deviceId As Integer) As Boolean
...

Hopefully that will solve the problem. I will check if the demo works on my system.

The document is not corrupt, you need Acrobat Reader 6.0 or higher (= PDF-1.5). Get the latest reader for free from Adobe’s download center. The latest version is Acrobat Reader 8.0.

Enjoy

Only momentary and OFF ‘0’ buttons are implemented in VM119 / K8070.
All other commands are for VM118/K8056
Document is OK, check PDF reader.

Thank you both for your prompt response.

Managed to open the PDF after upgrading reader - sorry about that!

I have changed the declarations to Functions, the data types seem correct, the values being sent check out the and code is all working without errors but the VM119 just doesn’t respond…

I’m assuming that the hardware side of things is OK because i can get it to work with SendCommand (which i have now written out of my code on your advice)

I’m not really sure what else to try now. Any ideas?

Cheers

Al

Check the following

  1. Are you connecting to the correct COM port?
  2. Is the address of the board the same as the one you are passing to your functions? (call DisplayAddress to find out what address the board is on
  3. Are you now using the integer values 1…8 as relay numbers? (not 49 etc)

Hi again

In my VB.NET application i have a setup form with a combo box to select the COM channel and a ‘connect’ button. I have checked the COM port in windows XP device manager and confirmed that it is COM6.

When I press ‘connect’ it calls OpenDevice and the Device ID is displayed in a text box (it’s usually a number between 950 and 1500).

Conclusion - correct port - connects OK

Then calling DisplayAddress returns the address in another text box - it’s always 1. That value is passed to a byte called m_address which is used in the function calls.

Conclusion - correct address

An integer called m_relay is used in the function calls and it can have a value of 1-8

Conclusion - correct integer values for relay

The test button on the K8074 USB module causes LD1 on the VM119 to flash 3 times. The VS.NET demo CAN activate the relay only using the momentary button for relay 1

Conclusion - hardware checks out

In my VB.NET application there is a ‘Set Relay’ button on the form with this code in:

Dim res As Boolean res = SetRelay(m_device, m_address, m_relay)

and a ‘Clear Relay’ button

Dim res As Boolean res = ClearRelay(m_device, m_address, m_relay)

m_device is an integer - usually around 1400 in value
m_address is a byte - usually 1
m_relay is an integer - usually 1

given these values:

The set relay button will cause the TX LED on the K8074 to flash but the VM119 does nothing

the clear relay button will cause the TX LED on the K8074 to flash and will make LD1 on the VM119 flash 3 times

ToggleRelay with the same values will cause the TX LED on the K8074 to flash but VM119 does nothing.

Now, my application does the same as the VS.NET demo project in that the ON buttons do nothing and the OFF buttons causes 3 flashes from LD1 on the VM119. The TOGGLE buttons do nothing and only the MOMENTARY button triggers the relay, but that uses SendCommand which you have suggested I don’t use.

VEL417 wrote that only momentary and off buttons are implemented in VM119, you wrote that the other functions in K8074.dll use SendCommand. My experience is that SendCommand is the only Function which can get the relay to work in the VM119.

so what am i to do?

Cheers
Al

Not all commands sent by the K8074 are supported by the VM119. The K8074 Manual explains what is possible and how to use the K8074 demo application.

The solution was simple, but i’m used to testing with the K8056 board which supports every command :slight_smile:

Hi

I’m not sure you understand - I don’t have a solution.

According to what you have said, the VM119 can only receive two commands from the K8074 which are:

ClearRelay and SendCommand

And you advised me not to use SendCommand!

I can’t use SetRelay or ToggleRelay or SendByte or EmergencyStop because they don’t work. So how can I get the relay to switch on apart from using SendCommand?

And if I have to use that could you please explain the range of values for cmd and param?

Let’s start over.

Take the K8074 Manual
And the K8074 Demo project

The manual states:

Let’s look at the demo project code to see what commands are sent for those buttons.

private void TimerMom_Tick(object sender, EventArgs e)
{
  K8074Native.SendCommand(
    m_device, // Device handle
    (Byte)(cbAddress.SelectedIndex + 1), // Usually 1
    0x50, // Momentary command
    (Byte)(48 + m_MomIdx) // ASCII '1'..'8'
  );
}

private void btnSwitchOffClick(object sender, EventArgs e)
{
  K8074Native.ClearRelay(
    m_device, // Device handle
    (Byte)(cbAddress.SelectedIndex + 1), // Usually 1
    (int)((Button)sender).Tag // Integer value 1..8
  );
}

Usually you can get all the information you need by looking at the code and manuals. I was wrong about using SendCommand, you do need to use it to send the momentary command (0x50). Just don’t use it for functions that are already defined like ToggleRelay etc.