开发者

RFID Serial Port Time Out When Reading Data

thanks for taking the time to look at my issue!

I have a Texas Instruments S4100 RFID scanner. I am attempting to simply read the ID of the tags I swipe over the scanner with a C++ console app. I've been pulling my hair out since this is the first time I've ever worked with serial ports. I found out quite a bit about the issue. I first used the createfile function and then the ReadFile function. I am extremely confident I opened the port correctly as the functions returned true. And when I attempted to open a program I know connects to the device (the demo program that came with the scanner) it says the port is already in use.

Since I could not get data from that method I switched gears and found a .dll from TI itself. The FeComm.dll .. I found online documentation for the .dll here:

http://www.ti.com/rfid/docs/manuals/refmanuals/S6000ProgramLibraryFECOM.pdf

Again I am able to open the port but now I am getting a timeout error even when I swipe the card over the device! In the code below you will see that I manually set the timeout to 8000ms (yes I was pulling at straws haha) but still nothing! Am I missing something really obvious? Perhaps I have to send a command to the device first to "start" it up? The demo program had no source code and I can't find anything that works online. Please offer me a clue as to what is going on! Thanks in advance =)

Here is my main() function:

#include "stdafx.h"
#include <windows.h>

#include "FeCom.h"
#include "FeComDef.h"

#include <windows.h>
#include <tchar.h>
#include <assert.h>
#include <stdio.h>
#include <iomanip>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    int iRecProtLen;
    char cPortNr[4];
    char cValue[128];
    char* para = "Timeout";
    char* toValue = "8000";

    _itoa( 1, cPortNr, 10 ); // Convert Integer to Char
    UCHAR cRecBuf[256]; // Buffer size may have to be matched to the receive data

    int handle = FECOM_OpenPort( cPortNr ); // COM:1 is opened
    if( handle < 0 )
    {
        // Code in the event of an error
        cout<<"Error opening the port"<<endl;
    }
    else
    { // Communication via COM:1; if successful, the receive data are located in cRecBuf

        FECOM_SetPortPara(handle, para, toValue);

        if(!FECOM_GetPortPara(handle, para, cValue))
        {
            // code here for displaying the COM parameter
            cout<<"TimeOut Parameter: "<<cValue<<endl;
        }

        iRecProtLen = FECOM_Receive( handle, cRecBuf, 256 );
        // If this is true then the function has thrown an error

        if( iRecProtLen < 0 )
        {
            // Communication erorr or buffer overflow
            if( iRecProtLen == FECOM_ERR_OVL_RECBUF )
            { // Buffer overflow: data in RecBuf are valid receive data
                cout<<"Buffer Overflow"<<endl;
            }

            //cout<<"FECOM ERR OVL RECBUF: "<<FECOM_ERR_OVL_RECBUF<<endl;
        }
        cout<<"IRECTPROTLEN: "<<iRecProtLen<<endl;
        cout<<"Return from Recieve: "<<cRecBuf<<endl;
    }
    return 0;
}

Here is the FeCom.h file.

/*-------------------------------------------------------
|                                                       |
|                       FECOM.h                         |
|                                                       |
---------------------------------------------------------


Operation Systems   :   Windows 9x/ME/NT/2000


This file contains the constants, datatypes and function declartions of FECOM.DLL
*/

#ifndef _FECOM_INCLUDE_H
#define _FECOM_INCLUDE_H


#ifdef FECOMDLL
    #define DLL_EXT_FUNC __declspec(dllexport) __stdcall
#else
    #define DLL_EXT_FUNC __declspec(dllimport) __stdcall
#endif



#ifdef __cplusplus
extern "C" {
#endif




// #####################################################
// FECOM constants
// #####################################################

// defines for uiFlag in FECOM_EVENT_INIT
#define FECOM_THREAD_ID     1
#define FECOM_WND_HWND      2
#define FECOM_CALLBACK      3
#define FECOM_EVENT         4

// defines for uiUse in FECOM_EVENT_INIT
#define FECOM_CTS_EVENT     1
#define FECOM_DCD_EVENT     2
#define FECOM_DSR_EVENT     3
#define FECOM_RTS_EVENT     4
#define FECOM_DTR_EVENT     5



// #####################################################
// FECOM structures
// #####################################################

// structure for transfering thread-IDs, message-handles or callbacks
typedef struct _FECOM_EVENT_INIT
{
    UINT uiUse;     // defines the event (e.g. FECOM_CTS_EVENT)
    UINT uiMsg;     // message code used with dwThreadID and hwndWnd (e.g. WM_USER_xyz)
    UINT uiFlag;    // specifies the use of the union (e.g. FECOM_WND_HWND)
    union
    {
        DWORD   dwThreadID;         // for thread-ID
        HWND    hwndWnd;            // for window-handle
        void    (*cbFct)(int, int); // for callback-function
        HANDLE  hEvent;             // for event-handle
#ifdef __cplusplus
    };
#else
    }Method;
#endif

} FECOM_EVENT_INIT;



// #####################################################
// FECOM functions
// #####################################################

// miscellaneous functions
void DLL_EXT_FUNC FECOM_GetDLLVersion( char* cVersion );
int  DLL_EXT_FUNC FECOM_GetErrorText( int iErrorCode, char* cErrorText );
int  DLL_EXT_FUNC FECOM_GetLastError( int iPortHnd, int* iErrorCode, char* cErrorText );

// functions for event notification
int  DLL_EXT_FUNC FECOM_AddEventHandler(int iPortHnd, FECOM_EVENT_INIT* pInit);
int  DLL_EXT_FUNC FECOM_DelEventHandler(int iPortHnd, FECOM_EVENT_INIT* pInit);

// port functions
int  DLL_EXT_FUNC FECOM_OpenPort( char* cPortNr );
int  DLL_EXT_FUNC FECOM_ClosePort( int iPortHnd );
int  DLL_EXT_FUNC FECOM_GetPortList( int iNext );
int  DLL_EXT_FUNC FECOM_GetPortPara( int iPortHnd, char* cPara, char* cValue );
int  DLL_EXT_FUNC FECOM_SetPortPara( int iPortHnd, char* cPara, char* cValue );
int  DLL_EXT_FUNC FECOM_DoPortCmd( int iPortHnd, char* cCmd, char* cValue );
int  DLL_EXT_FUNC FECOM_GetPortHnd( char* cPortNr );

// communication function
int  DLL_EXT_FUNC FECOM_Transceive( int iPortHnd, UCHAR* cSendProt, int iSendLen, UCHAR* cRecProt, int iRecLen );
int  DLL_EXT_FUNC FECOM_Transmit( int iPortHnd, UCHAR* cSendProt, int iSendLen );
int  DLL_EXT_FUNC FECOM_Receive( int iPortHnd, UCHAR* cRecProt, int iRecLen );


#undef DLL_EXT_FUNC

#ifdef __cplusplus
}
#endif


// #####################################################
// typedefs of DLL-functions for explicite loading
// #####################################################

// miscellaneous functions
typedef void (CALLBACK* LPFN_FECOM_GET_DLL_VERSION)(char*);
typedef int  (CALLBACK* LPFN_FECOM_GET_ERROR_TEXT)(int, char*);
typedef int  (CALL开发者_如何学PythonBACK* LPFN_FECOM_GET_LAST_ERROR)(int, int*, char*);

// functions for event notification
typedef int  (CALLBACK* LPFN_FECOM_ADD_EVENT_HANDLER)(FECOM_EVENT_INIT*);
typedef int  (CALLBACK* LPFN_FECOM_DEL_EVENT_HANDLER)(FECOM_EVENT_INIT*);

// port functions
typedef int  (CALLBACK* LPFN_FECOM_OPEN_PORT)(long);
typedef int  (CALLBACK* LPFN_FECOM_CLOSE_PORT)(int);
typedef int  (CALLBACK* LPFN_FECOM_GET_PORT_LIST)(int);
typedef int  (CALLBACK* LPFN_FECOM_GET_PORT_PARA)(int, char*, char*);
typedef int  (CALLBACK* LPFN_FECOM_SET_PORT_PARA)(int, char*, char*);
typedef int  (CALLBACK* LPFN_FECOM_DO_PORT_CMD)(int, char*, char*);
typedef int  (CALLBACK* LPFN_FECOM_GET_PORT_HND)(char*);

// communication function
typedef int  (CALLBACK* LPFN_FECOM_TRANSCEIVE)(int, UCHAR*, int, UCHAR*, int);
typedef int  (CALLBACK* LPFN_FECOM_TRANSMIT)(int, UCHAR*, int);
typedef int  (CALLBACK* LPFN_FECOM_RECEIVE)(int, UCHAR*, int);

#endif // _FECOM_INCLUDE_H

Here is the FeComDef.h file:

/*-------------------------------------------------------
|                                                       |
|                       FECOMDef.h                      |
|                                                       |
---------------------------------------------------------


Operation Systems   :   Windows 9x/ME/NT/2000


This file contains the error codes for FECOM.DLL
*/

#ifndef _FECOMDEF_H_
#define _FECOMDEF_H_


// FECOM error codes

// common errors
#define FECOM_ERR_NEWPORT_FAILURE           -1000
#define FECOM_ERR_EMPTY_LIST                -1001
#define FECOM_ERR_POINTER_IS_NULL           -1002
#define FECOM_ERR_NO_MEMORY                 -1003
#define FECOM_ERR_UNSUPPORTED_HARDWARE      -1004   // new in V2.00.00

// error while open the port
#define FECOM_ERR_NO_PORT                   -1010
#define FECOM_ERR_NO_CONNECT                -1011
#define FECOM_ERR_LINK_ID                   -1012
#define FECOM_ERR_PORT_IS_OPEN              -1013   // new in V2.00.00

// handle errors
#define FECOM_ERR_UNKNOWN_HND               -1020
#define FECOM_ERR_HND_IS_NULL               -1021
#define FECOM_ERR_HND_IS_NEGATIVE           -1022
#define FECOM_ERR_NO_HND_FOUND              -1023

// communication errors
#define FECOM_ERR_TIMEOUT                   -1030
#define FECOM_ERR_NO_SENDPROTOCOL           -1031
#define FECOM_ERR_RECEIVE_PROCESS           -1032   // renamed in V2.00.00
#define FECOM_ERR_INIT_COMM_PROCESS         -1033   // new in V2.00.00
#define FECOM_ERR_FLUSH_INPUT_BUFFER        -1034   // new in V2.00.00
#define FECOM_ERR_FLUSH_OUTPUT_BUFFER       -1035   // new in V2.00.00
#define FECOM_ERR_CHANGE_PORT_PARA          -1036   // new in V2.00.00
#define FECOM_ERR_TRANSMIT_PROCESS          -1037   // new in V2.00.00

// parameter errors
#define FECOM_ERR_UNKNOWN_PARAMETER         -1050
#define FECOM_ERR_PARAMETER_OUT_OF_RANGE    -1051
#define FECOM_ERR_ODD_PARAMETERSTRING       -1052
#define FECOM_ERR_PORTNR_OUT_OF_RANGE       -1053
#define FECOM_ERR_UNKNOWN_ERRORCODE         -1054

// receive buffer overflow
#define FECOM_ERR_OVL_RECBUF                -1070


#endif _FECOMDEF_H_

If you run the code iRecProtLen = -1030 which according to the documentation is a timeout error. And yes I'm swiping the card over the device while the program runs ;)

I'm not sold on the idea of using these functions. If someone can offer me a solution that they know about I'd be fine with that or if you need more information please ask. I just don't know what the problem is. The port opens but no data read. Really confusing to me. I hope you guys can help!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜