1

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

我有一台德州仪器 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 根据文档是超时错误。是的,我在程序运行时在设备上刷卡;)

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

4

0 回答 0