none
PDA程式與RFID Readr API資料型態的問題 RRS feed

  • 問題

  • 要麻煩各位先進了
    我用VB.NET寫PDA的程式 並使用RFID Readr API(dll 用c++寫)
    我要使用dll中的『SUNLITRFID_Open』function

     

    從RFID Readr API的文件中得知:

     

    BYTE SUNLITRFID_Open (
    HWND hWnd,
    LPCWSTR PortName,
    DWORD BaudRate,
    SUNLITRFID_ENVIRONMENT *Environment
    );
    SUNLITRFID_Open is used to open the comport

     

    Parameters:
    hWnd:The program’s HANDLE as a parameter that send thru the Function. If you don’t know what’s your program’s HANDLE, use
    NULL.
    PortName:The comport name you want to open.
    BaudRate:The comport baud rate that you want to open with.
    Environment:Please see 「TYPE」 definition.

     

    Return Values:
    The function will return byte value that indicate the execution result.Please see 「MESSAGE」 definition for byte value.

     

    For example:
    SUNLITRFID_ENVIRONMENT Environment;
    WCHAR wcaPortName[]={‘C’,’O’,’M’,’1’,0x00};
    if(SUNLITRFID_Open(NULL,wcaPortName,19200,&Environment)==HANDLE_SUCCESS)
    {}//Open comport successfully
    else //Open comport failed
    {}

     

    而SUNLITRFID_ENVIRONMENT結構如下:

     

    typedef struct {
    HANDLE haEvent[ENVIRONMENT_EVENT_NUMBER];
    SUNLITRFID_TAGID ActiveTagID;
    } SUNLITRFID_ENVIRONMENT;

    Type definition of enviroment status.
    The type has two elements: haEvent and
    ActiveTagID.
    haEvent is HANDLE array that use for wait
    events. For more information, Please see
    EVENTS Introduction.
    ActiveTagID was introduced at above definition.

     

    typedef struct {
    unsigned char data[TAG_LENGTH];
    } SUNLITRFID_TAGID;

    Type definition of tag ID
    Since Mu-ID data is represented by 128 bits
    format, so we condense this 128 bit information
    by byte format. And every 8 bits data is
    respectively each byte, for example, data[0]
    represents bit120~bit127; data[1] represents
    bit112~bit119; data[TAG_LENGTH-1]
    represents bit0~bit7.

     

    #define ENVIRONMENT_EVENT_NUMBER 3
    Define the number of events happened.

     

    #define TAG_LENGTH 16
    Define the length of Tag ID data. (Unit : byte)

     

    我在VB.NET裡宣告了以下:


    Const TAG_LENGTH = 16
    Const ENVIRONMENT_EVENT_NUMBER = 3
    Public Structure SUNLITRFID_TAGID
        Dim data() As Char 'TAG_LENGTH = 16
    End Structure
    Public Structure SUNLITRFID_ENVIRONMENT
        Dim haEvent() As Long 'ENVIRONMENT_EVENT_NUMBER = 3
        Dim ActiveTagID As SUNLITRFID_TAGID
    End Structure
    <DllImport("Sunlitrfidppc.dll")> _
    Public Function SUNLITRFID_Open(ByVal hWnd As Long, ByVal ProtName As String, ByVal BaudRate As UInt32, ByRef Environment As SUNLITRFID_ENVIRONMENT) As Byte
    End Function

     

    並用以下程式碼call dll中的function


    Dim wcaPortName As String = "COM6"
    Dim Environment As SUNLITRFID_ENVIRONMENT
    SUNLITRFID_Open(Nothing, wcaPortName, 19200, Environment)


    但卻出現無法辨識的錯誤

    我已經有去查了
    http://msdn.microsoft.com/zh-tw/library/ac7ay120(VS.80).aspx
    的資料型態
    也確定了RFID Readr是使用COM6
    但除此之外 卻沒有頭緒是哪邊出了問題
    不知是宣告時就出了問題 還是call function時有問題
    真的要麻煩各位先進了 非常感謝

     

     


     

    2009年1月20日 上午 03:05

解答

  • 結構的部分如果是byref,我會用New去產生一個新的執行個體出來,之後再用傳址的方式傳過去;再上面"資源分享"的文章中有一個Make p/invoke easy,也可以利用那個工具幫你轉換看看。

    不果我覺得你還是直接問原廠換比較快,看他們有沒有VB的sample;再不然就去要通訊協定,自己用RS232去做通訊處理,不用DLL。

     

    2009年1月20日 上午 03:47
    版主

所有回覆

  • 結構的部分如果是byref,我會用New去產生一個新的執行個體出來,之後再用傳址的方式傳過去;再上面"資源分享"的文章中有一個Make p/invoke easy,也可以利用那個工具幫你轉換看看。

    不果我覺得你還是直接問原廠換比較快,看他們有沒有VB的sample;再不然就去要通訊協定,自己用RS232去做通訊處理,不用DLL。

     

    2009年1月20日 上午 03:47
    版主
  • 先謝謝bauann版主熱心地回答我的問題 ^^

    我來自問自答

    Module Module2
        Public Const ENVIRONMENT_EVENT_NUMBER As Integer = 3
        Public Const TAG_LENGTH As Integer = 16

        <StructLayout(LayoutKind.Sequential)> _
        Friend Structure SUNLITRFID_TAGID
            <MarshalAs(UnmanagedType.ByValArray, SizeConst:=TAG_LENGTH)> _
            Public data As Byte()
        End Structure

        <StructLayout(LayoutKind.Sequential)> _
        Friend Structure SUNLITRFID_ENVIRONMENT
            <MarshalAs(UnmanagedType.ByValArray, SizeConst:=ENVIRONMENT_EVENT_NUMBER)> _
            Private haEvent As IntPtr()
            Private ActiveTagID As SUNLITRFID_TAGID
        End Structure

        Class PDADriverProxy2
            <DllImport("Sunlitrfidppc.dll")> _
            Public Shared Function SUNLITRFID_CFExist(ByVal CFName As String) As Boolean
            End Function
        End Class

        Class PDADriverProxy
            <DllImport("Sunlitrfidppc.dll")> _
            Public Shared Function SUNLITRFID_Open(ByVal hWnd As IntPtr, ByVal PortName As String, ByVal BaudRate As UInt32, ByRef Environment As SUNLITRFID_ENVIRONMENT) As Byte
            End Function
        End Class

    End Module

    Module Module3
        Public Function CFreaderExistOpen() As Boolean
            Dim CFExist As String 
            Dim rCEFxist As Boolean
            rCEFxist = PDADriverProxy2.SUNLITRFID_CFExist(CFExist)

            Dim r As Byte
            Dim envt As New SUNLITRFID_ENVIRONMENT
            r = PDADriverProxy.SUNLITRFID_Open(IntPtr.Zero, CFExist, 4800, envt)
        End Function

    End Module 

    2009年3月23日 上午 11:01