读取数据时RFID串口超时

时间:2011-03-22 09:37:17

标签: c++ timeout serial-port rfid

感谢您花时间看我的问题!

我有一台德州仪器S4100 RFID扫描仪。我试图用C ++控制台应用程序简单地读取我在扫描仪上扫描的标签的ID。我一直在拔头发,因为这是我第一次使用串口。我发现了很多关于这个问题的信息。我首先使用createfile函数,然后使用ReadFile函数。我非常自信我正确地打开了端口,因为函数返回true。当我试图打开一个我知道的程序连接到设备(扫描仪附带的演示程序)时,它说该端口已经在使用中。

由于我无法从该方法获取数据,所以我换了齿轮,从TI本身找到了一个.dll。 FeComm.dll ..我在这里找到.dll的在线文档:

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

我再次能够打开端口,但现在即使我在设备上刷卡,我也会收到超时错误!在下面的代码中,您将看到我手动将超时设置为8000毫秒(是的,我拉着稻草哈哈)但仍然没有!我错过了一些非常明显的东西吗也许我必须首先向设备发送命令以“启动”它?演示程序没有源代码,我找不到任何在线工作的东西。请告诉我发生了什么事!在此先感谢=)

这是我的main()函数:

#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;
}

这是FeCom.h文件。

/*-------------------------------------------------------
|                                                       |
|                       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  (CALLBACK* 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

这是FeComDef.h文件:

/*-------------------------------------------------------
|                                                       |
|                       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_

如果运行代码iRecProtLen = -1030,根据文档是超时错误。是的,我在程序运行时将卡刷在设备上;)

我没有出售使用这些功能的想法。如果有人能为我提供他们知道的解决方案,我会对此感到满意,或者如果您需要更多信息,请询问。我只是不知道问题是什么。端口打开但没有数据读取。对我来说真的很困惑。我希望你们能帮忙!

0 个答案:

没有答案