AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

DllCall problem controlling LEDs with PacDrive.dll, syntax?

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
gwarble
Guest





PostPosted: Sat May 02, 2009 1:55 am    Post subject: DllCall problem controlling LEDs with PacDrive.dll, syntax? Reply with quote

Hey Everyone,

I'm trying to control LEDs with the PacDrive and its PacDrive.dll, but am not having much success. Any help would be greatly appreciated:

PacDrive SDK (.dll) info can be found here: http://www.ultimarc.com/pacdrive.html at the very bottom of the page

From the DLLs readme:
Code:
int PacInitialize()

- Initialize all PacDrive and U-HID Devices
- Returns the number of PacDrives and U-HID's on the PC
- They are sorted in order of PacDrive's Version Numbers, then U-HID's PID.

void PacShutdown()

- Shutdown all PacDrive and U-HID Devices
- No return value

bool PacSetLEDStates(int id, USHORT data)

- Sends data to the PacDrive or U-HID specified by id
- Returns true for success and false for failure
- You can use up to 16 PacDrive's and 16 U-HID's (id 0 to id 31). The order they are sorted is PacDrive by Version number first then U-HID by PID.

int PacGetVendorId(int id)
- Returns the Vendor Id of the device specified by id


And here's where i'm stuck:
Code:

 If DllCall("PacDrive.dll\PacInitialize")
 {
  MsgBox, % DllCall("PacDrive.dll\PacGetVersionNumber","Int",0) "`n`nErrorLevel:"
ErrorLevel
 MsgBox, % DllCall("PacDrive.dll\PacSetLEDStates",Int,0,UShort,0xFFFF) "`n`nErrorLevel: " ErrorLevel
 }


So PacInitialize called from ahk is working properly, returning 0 if no devices are found and returning 1 if the device is plugged in.

But the other two functions are returning 0 (ie failure) no matter how i configure the parameters... I know this is hard to diagnose without the actual hardware connected, but am i missing something simple here? The code examples for other languages that are provided with the dll are working properly to control the pacdrive, which tells me the dll and hardware are working properly

thanks in advance!

- gwarble[/url]
Back to top
gwarble
Guest





PostPosted: Sat May 02, 2009 2:02 am    Post subject: Reply with quote

FYI, here are the example calls from other languages:

VB.NET:
Code:

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices

Class PacDrive
    <DllImport("PacDrive.dll", CallingConvention:=CallingConvention.StdCall)> _
    Private Shared Function PacInitialize() As Integer
    End Function

    <DllImport("PacDrive.dll", CallingConvention:=CallingConvention.StdCall)> _
    Private Shared Sub PacShutdown()
    End Sub

    <DllImport("PacDrive.dll", CallingConvention:=CallingConvention.StdCall)> _
    Private Shared Function PacSetLEDStates(ByVal id As Integer, ByVal data As UShort) As Boolean
    End Function

    Public NumDevices As Integer = 0

    Public Sub New()
    End Sub

    Public Function Initialize() As Integer
        NumDevices = PacInitialize()

        Return NumDevices
    End Function

    Public Sub Shutdown()
        For i As Integer = 0 To NumDevices - 1
            SetLEDStates(i, New Boolean() {False, False, False, False, False, False, _
            False, False, False, False, False, False, _
            False, False, False, False})
        Next

        PacShutdown()
    End Sub

    Public Sub SetLEDStates(ByVal id As Integer, ByVal data As UShort)
        PacSetLEDStates(id, data)
    End Sub

    Public Sub SetLEDStates(ByVal id As Integer, ByVal data As Boolean())
        Dim dataSend As UShort = 0

        For i As Integer = 0 To data.Length - 1
            If data(i) Then
                dataSend = dataSend Or CUShort(1 << i)
            End If
        Next


        PacSetLEDStates(id, dataSend)
    End Sub
End Class


VB6:
Code:

Private Declare Function PacInitialize Lib "PacDrive" () As Integer
Private Declare Sub PacShutdown Lib "PacDrive" ()
Private Declare Function PacSetLEDStates Lib "PacDrive" (ByVal id As Integer, ByVal data As Integer) As Boolean

Private Sub Command1_Click()
    Dim numdevices As Integer
   
    numdevices = PacInitialize
   
    If numdevices > 0 Then
        PacSetLEDStates 0, &HAAAA
    End If
End Sub

Private Sub Command2_Click()
    PacSetLEDStates 0, 0
   
    PacShutdown
End Sub


C#:
Code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace PacDriveDemo
{
    class PacDrive
    {
        [DllImport("PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern int PacInitialize();

        [DllImport("PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void PacShutdown();

        [DllImport("PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool PacSetLEDStates(int id, ushort data);

        [DllImport("PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern int PacGetVendorId(int Id);

        [DllImport("PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern int PacGetProductId(int Id);

        [DllImport("PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern int PacGetVersionNumber(int Id);

        [DllImport("PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void PacGetVendorName(int Id, StringBuilder VendorName);

        [DllImport("PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void PacGetProductName(int Id, StringBuilder ProductName);

        [DllImport("PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void PacGetSerialNumber(int Id, StringBuilder SerialNumber);

        [DllImport("PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void PacGetDevicePath(int Id, StringBuilder DevicePath);

        public int NumDevices = 0;

        public PacDrive()
        {
        }

        public int Initialize()
        {
            NumDevices = PacInitialize();

            return NumDevices;
        }

        public void Shutdown()
        {
            for(int i=0; i<NumDevices; i++)
                SetLEDStates(i, new bool[] { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false });

            PacShutdown();
        }

        public void SetLEDStates(int Id, ushort Data)
        {
            PacSetLEDStates(Id, Data);
        }

        public void SetLEDStates(int Id, bool[] Data)
        {
            ushort dataSend = 0;

            for (int i=0; i < Data.Length; i++)
                if (Data[i]) dataSend |= (ushort)(1 << i);

            PacSetLEDStates(Id, dataSend);
        }

        public int GetVendorId(int Id)
        {
            return PacGetVendorId(Id);
        }

        public int GetProductId(int Id)
        {
            return PacGetProductId(Id);
        }

        public int GetVersionNumber(int Id)
        {
            return PacGetVersionNumber(Id);
        }

        public string GetVendorName(int Id)
        {
            StringBuilder sb = new StringBuilder(256);

            PacGetVendorName(Id, sb);

            return sb.ToString();
        }

        public string GetProductName(int Id)
        {
            StringBuilder sb = new StringBuilder(256);

            PacGetProductName(Id, sb);

            return sb.ToString();
        }

        public string GetSerialNumber(int Id)
        {
            StringBuilder sb = new StringBuilder(256);

            PacGetSerialNumber(Id, sb);

            return sb.ToString();
        }

        public string GetDevicePath(int Id)
        {
            StringBuilder sb = new StringBuilder(256);

            PacGetDevicePath(Id, sb);

            return sb.ToString();
        }
    }
}


Delphi:
Code:

unit SDIMAIN;

interface

uses Windows, Classes, Graphics, Forms, Controls, Menus,
  Dialogs, StdCtrls, Buttons, ExtCtrls, ComCtrls, ImgList, StdActns,
  ActnList, ToolWin, SysUtils;

const
  PACDRIVE_MAX_DEVICES : Integer = 16;

type
  TSDIAppForm = class(TForm)
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type
  PACDRIVEHANDLE = THandle;

Function PacInitialize() : Integer; stdcall; external 'PacDrive.dll';
Procedure PacShutdown() stdcall; external 'PacDrive.dll';
Function PacSetLEDStates(id: Integer; data: Word) : Boolean; stdcall; external 'PacDrive.dll';

var
  SDIAppForm: TSDIAppForm;

implementation

{$R *.dfm}

procedure TSDIAppForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  PacSetLEDStates(0, 0);
  PacShutdown();
end;

procedure TSDIAppForm.FormShow(Sender: TObject);
var
  numdevices: Integer;
begin
  numdevices := PacInitialize();

  if (numdevices > 0) then
  begin
    PacSetLEDStates(0, StrToInt('$AAAA'))
  end;
end;

end.


Thanks again!
- gwarble
Back to top
Guest






PostPosted: Sat May 02, 2009 2:12 am    Post subject: Reply with quote

Code:
DllCall("LoadLibrary",Str,"PacDrive.dll")
Back to top
gwarble
Guest





PostPosted: Sat May 02, 2009 2:59 am    Post subject: Thanks! Reply with quote

Thanks for the help.... Its working as expected now... i'll post the working code for reference in case anyone else wants to get one of these working...

For my information (i'm new to ahk and have only used dllcalls for simple things like blat), what makes the "LoadLibrary" necessary? Reading thru the documentation before i posted, i came across this feature but it is only documented as a way to speed up the call (under the performance heading)

- gwarble
Back to top
engunneer



Joined: 30 Aug 2005
Posts: 7698
Location: Germany (but I only speak English)

PostPosted: Sat May 02, 2009 4:27 am    Post subject: Reply with quote

it can also be used to keep a hardware driver 'alive' in memory. I have used ahk to talk to CNC motion control hardware, and the dll only wants to be loaded once to keep everything active.

if you don't LoadLibrary, then the dll is opened and closed on every call, so the second call is just like forgetting to initialize it, since the initialized call was closed when the other command finished.

Hope this helps.
_________________
Unless noted, all code is UNTESTED.
Answers Here: 1.(Loops, Viruses, etc.) 2.Search 3.RTFM 4.Ask for Help.
PMs will be ignored unless you are hiring me.
Back to top
View user's profile Send private message Visit poster's website
gwarble
Guest





PostPosted: Sat May 02, 2009 6:06 am    Post subject: Reply with quote

ah, i see

thanks for the explanation... makes total sense

thats funny you mentioned cnc motion control... i'm actually using this for the cnc machine shop where i work... not for direct motion control though, just for machine/network integration (we've got a bunch of VMCs) and machine monitoring/logging/status lights/DNC/etc...

if anyone's interested in getting started with a PacDrive or U-HID using pacdrive.dll, here's a quick and dirty script to flash LEDs 1 and 2 randomly

Code:

#SingleInstance Force
#Persistent
#NoEnv
SetWorkingDir, %A_ScriptDir%

 If DllCall("LoadLibrary", "str", "PacDrive.dll")
  If DllCall("PacDrive.dll\PacInitialize")
   DllCall("PacDrive.dll\PacSetLEDStates",Int,0,UShort,3) "`n`nError: " ErrorLevel
 Else
  ExitApp
 SetTimer, ChangeLED, 100
Return

ChangeLED:
 Random, rand, 0, 3
 DllCall("PacDrive.dll\PacSetLEDStates",Int,0,UShort,rand)
Return


Thanks again
- gwarble
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group