2009-06-05 5 views
4

마이크로 칩 PIC USB DLL을 유니 코드와 제대로 작동시키는 데 많은 시간을 할애할만한 사람을 돕기위한 질문입니다.Delphi 2009와 함께 Microchip PIC USB DLL을 호출하는 방법

여러 문자열이 필요하며 이것들은 간단하지만 PAnsiChar이지만 DLL 호출 규칙을 올바르게 조합하면 나이가 들었습니다. 이 DLL에는 Delphi (비 유니 코드)를 사용하고 살인을 저지르는 사람들이 많이 있습니다.

unit UArtPic32USBDriver; 

interface 

uses 
    Windows, 
    SysUtils, 
    UArtGeneralHwDefs; 


type 
    EArtPic32Usb = class(EArtGeneralHw); 

function Pic32Usb_GetDllVersion : integer; 
// Returns a number representing the DLL version. 

function Pic32Usb_GetDeviceCount(const AVendorProductID : string) : integer; 
// Returns the number of devices with this vendor ID 

function Pic32Usb_Open(
      AInstance : DWORD; 
      const AVendorProductID, AEndpointID : string; 
      ADirectionCode : integer; 
      ARaise   : boolean) : THANDLE; 
// Opens an endpoint. Can raise an exception if no valid handle is returned. 


procedure Pic32Usb_Close(AHandle : THandle); 
// Closes the device 

function Pic32Usb_Write(
      AHandle    : THANDLE; 
      ABufferPtr   : pointer; 
      ALengthBytes   : integer) : DWORD; 
// Performs a write using this handle. Returns the number of bytes written 

function Pic32Usb_Read(
      AHandle    : THANDLE; 
      ABufferPtr   : pointer; 
      ALengthBytes   : integer) : DWORD; 
// Performs a Read using this handle. Returns the number of bytes read 

const 
    MP_WRITE     = 0; 
    MP_READ     = 1; 

implementation 



var 

    _MPUSBGetDLLVersion : function() :DWORD; cdecl stdcall; 

    // Number of vid & pid matching USB Devices 
    _MPUSBGetDeviceCount : function(pVID_PID : PAnsiChar) : DWORD; cdecl; 

    _MPUSBOpen   : function (instance : DWORD; 
            pVID_PID : PAnsiChar; 
            pEP  : PAnsiChar; 
            dwDir  : DWORD; 
            dwReserved : DWORD) : THANDLE; cdecl; 

    _MPUSBClose   : function (handle :THANDLE) : DWORD; cdecl; 

    _MPUSBRead   : function ( handle   : THANDLE; 
             pData   : pointer; 
             dwLen   : DWORD; 
            var pLength   : DWORD; 
             dwMilliseconds : DWORD ) : DWORD; cdecl; 

    _MPUSBReadInt  : function ( handle   : THANDLE; 
            var pData   : pointer; 
             dwLen   : DWORD; 
            var pLength  : PDWORD; 
             dwMilliseconds : DWORD ) : DWORD; cdecl; 

    _MPUSBWrite   : function ( handle   : THANDLE; 
             pData   : pointer; 
             dwLen   : DWORD; 
            var pLength  : DWORD; 
             dwMilliseconds : DWORD) : DWORD; cdecl; 


    UsbDllHandle : THandle = 0; 

const 

    MillisecondPollingInterval = 100; //Represents 1 ms is hardware 
    CharBufSize = 64; 

type 
    TAnsiCharBuf64 = array[0..CharBufSize-1] of AnsiChar; 


function LoadDLL : THandle; 
var 
    S : string; 
begin 
    S := 'mpusbapi.dll'; 
    UsbDllHandle := LoadLibrary(PChar(S)); 

    If UsbDllHandle = 0 then 
    Raise EArtPic32Usb.CreateFmt(
     'The usb library is required but cannot be loaded. Check that it is installed. (%s)', 
     [S] 
     ); 

    @_MPUSBGetDLLVersion := GetProcAddress(UsbDllHandle,'_MPUSBGetDLLVersion'); 
    Assert(@_MPUSBGetDLLVersion <> nil); 

    @_MPUSBGetDeviceCount := GetProcAddress(UsbDllHandle,'_MPUSBGetDeviceCount'); 
    Assert(@_MPUSBGetDeviceCount <> nil); 

    @_MPUSBOpen := GetProcAddress(UsbDllHandle,'_MPUSBOpen'); 
    Assert(@_MPUSBOpen <> nil); 

    @_MPUSBClose := GetProcAddress(UsbDllHandle,'_MPUSBClose'); 
    Assert(@_MPUSBClose <> nil); 

    @_MPUSBRead := GetProcAddress(UsbDllHandle,'_MPUSBRead'); 
    Assert(@_MPUSBRead <> nil); 

    @_MPUSBReadInt := GetProcAddress(UsbDllHandle,'_MPUSBReadInt'); 
    Assert(@_MPUSBReadInt <> nil); 

    @_MPUSBWrite := GetProcAddress(UsbDllHandle,'_MPUSBWrite'); 
    Assert(@_MPUSBWrite <> nil); 

    Result := UsbDllHandle; 

end; 

procedure NeedDLL; 
begin 
    If UsbDllHandle = 0 then 
    LoadDll; 
end; 



function Pic32Usb_GetDllVersion : integer; 
// Returns a number representing the DLL version. 
begin 
    NeedDLL; 
    Result := _MPUSBGetDLLVersion(); 
end; 


function Pic32Usb_GetDeviceCount(const AVendorProductID : string) : integer; 
// Returns the number of devices with this vendor ID 
var 
    bufVendorProductID : TAnsiCharBuf64; 
begin 
    NeedDLL; 

    StrPCopy(bufVendorProductID, AnsiString(AVendorProductID)); 

    Result := _MPUSBGetDeviceCount(@bufVendorProductID); 
end; 


function Pic32Usb_Open(
      AInstance : DWORD; 
      const AVendorProductID, AEndpointID : string; 
      ADirectionCode : integer; 
      ARaise : boolean) : THANDLE; 
// Opens an endpoint. Can raise an exception if no valid handle is returned. 
var 
    bufVendorProductID, bufEndpointID : TAnsiCharBuf64; 
begin 
    NeedDLL; 

    StrPCopy(bufVendorProductID, AnsiString(AVendorProductID)); 
    StrPCopy(bufEndpointID, AnsiString(AEndpointID)); 

    Result := _MPUSBOpen(
    AInstance, 
    @bufVendorProductID, 
    @bufEndpointID, 
    DWORD(ADirectionCode), 
    0); 

    if Result = 0 then 
    If ARaise then 
     Raise EArtPic32Usb.CreateFmt(
     'Unable to open USB device "%s", endpoint "%s"', [AVendorProductID, AEndpointID]); 
end; 


procedure Pic32Usb_Close(AHandle : THandle); 
begin 
    If UsbDllHandle <> 0 then 
    _MPUSBClose(AHandle); 
end; 


function Pic32Usb_Write(
      AHandle    : THANDLE; 
      ABufferPtr   : pointer; 
      ALengthBytes   : integer) : DWORD; 
// Performs a write using this handle. Returns the number of bytes written 
var 
    I : integer; 
begin 
    I := _MPUSBWrite(
    AHandle, 
    ABufferPtr, 
    DWORD(ALengthBytes), 
    Result, 
    MillisecondPollingInterval); 
    if I <> 1 then 
    Raise EArtPic32Usb.CreateFmt('Error performing USB write', []); 
end; 


function Pic32Usb_Read(
      AHandle    : THANDLE; 
      ABufferPtr   : pointer; 
      ALengthBytes   : integer) : DWORD; 
// Performs a Read using this handle. Returns the number of bytes read 
var 
    I : integer; 
begin 
    I := _MPUSBRead(
    AHandle, 
    ABufferPtr, 
    DWORD(ALengthBytes), 
    Result, 
    MillisecondPollingInterval); 
    if I <> 1 then 
    Raise EArtPic32Usb.CreateFmt('Error performing USB read', []); 
end; 



initialization 
finalization 
    If UsbDllHandle <> 0 then 
    FreeLibrary(UsbDllHandle) 
end. 
+6

흠 ... 엄밀히 말하자면, 이것은 질문이 아닙니다. 나는 그것을 닫는 것이 타당하지 않다고 생각하지만 아마도 공통점을 가지고 실제로 대답 부분을 편집해야합니다. 그런 다음 액트 해답으로 게시하고 동의하십시오. – unwind

답변

1

임시 답변으로 게시하겠습니다. @Brian Frost는 전체 코드를 게시 할 수 있습니다. 이야기의 요지는 기대하는 드라이버를 다룰 ​​때 단일 바이트 ANSI 문자열을 사용하는 것입니다.

StrPCopy(bufVendorProductID, AnsiString(AVendorProductID)); 
관련 문제