C++ 读取数据时RFID虚拟串行端口超时

C++ 读取数据时RFID虚拟串行端口超时,c++,timeout,serial-port,rfid,C++,Timeout,Serial Port,Rfid,感谢您抽出时间来关注我的问题 我正在使用NFC编码器设备,该设备创建了一个虚拟串行端口。我试图通过在设备上滑动RFID标签,将一些数据读入控制台应用程序(C++) 我意识到有一个非常相似的问题。我已经尝试了该主题中的所有建议,但没有成功。显然,那篇文章的作者找到了一个解决方案,但没有发布。我无法直接联系作者,当我试图通过回答问题来问他时,我的答案会被删除 在下面的代码中,您可以看到我在所有com端口上循环以检测活动端口。当检测到一个端口时,我尝试打开它,成功了。但是,如果我尝试从设备读取数据,我

感谢您抽出时间来关注我的问题

我正在使用NFC编码器设备,该设备创建了一个虚拟串行端口。我试图通过在设备上滑动RFID标签,将一些数据读入控制台应用程序(C++)

我意识到有一个非常相似的问题。我已经尝试了该主题中的所有建议,但没有成功。显然,那篇文章的作者找到了一个解决方案,但没有发布。我无法直接联系作者,当我试图通过回答问题来问他时,我的答案会被删除

在下面的代码中,您可以看到我在所有com端口上循环以检测活动端口。当检测到一个端口时,我尝试打开它,成功了。但是,如果我尝试从设备读取数据,我总是会收到一个“-1030超时,当从端口读取时”-错误,尽管我正在设备上刷标签

当我使用带有示例软件的设备时,我能够读取/写入RFID标签。所以我肯定错过了什么。任何帮助都将不胜感激

#include "fecom.h"
#include <iostream>

int main(void)
{
    int Err;
    char cPortNr[4];
    int iRecProtLen;
    UCHAR cRecBuf[256];
    int handle;

    //loop through ports 1 to 256 to detect devices.
    for(int iPortNr=1; iPortNr<257; ++iPortNr)
    {
        if(0 == FECOM_DetectPort( iPortNr ))
        {
        std::cout << "Detected port: " << iPortNr << std::endl;

        itoa( iPortNr, cPortNr, 10 ); // Convert Integer to Char

        handle = FECOM_OpenPort( cPortNr ); // COM:cPortNr should be opened
        if( handle < 0 )
        {
            // code here for error condition
            std::cout << "Error opening port: COM:" << cPortNr << std::endl;
        }
        else
        {   // Communication through COM:cPortNr, if successful received data are in RecBuf
            // code here for communication or other
            std::cout << "Successfully connected to port COM:" << cPortNr << std::endl;
            std::cout << "Press enter to read from COM:" << cPortNr << " (5s)" << std::endl;
            std::cin.get();
            Err = FECOM_SetPortPara(handle,"Timeout","5000");
            Err = FECOM_SetPortPara(handle,"CharTimeout","5000");

            iRecProtLen = FECOM_Receive( handle, cRecBuf, 256 );
            if( iRecProtLen < 0 )
            {
                // Communication error or buffer overflow
                if( iRecProtLen == FECOM_ERR_OVL_RECBUF )
                { // Buffer overflow: Data in RecBuf are valid receive data
                    std::cout << "Buffer overflow error" << std::endl;
                }
                else
                {
                    char cErrorText[256];
                    FECOM_GetErrorText(iRecProtLen, cErrorText);
                    std::cout << "Error: " << iRecProtLen << " " << cErrorText << std::endl;
                    std::cout << std::endl;
                }
            }
            else 
            {
                std::cout << "Data received: " << cRecBuf << std::endl;
                std::cout << std::endl;
            }
            Err = FECOM_ClosePort(handle);
            }
        }
    }

    std::cout << "Program terminated. Press enter to close." <<std::endl;
    std::cin.get();
    return 0;
}

显然,我需要使用不同的协议与端口通信

虽然我必须使用FECOM来打开/关闭端口,但我不能将其用于设备上的读/写操作。为此,我必须使用FEISC,一种不同的协议/库

一旦我有了一些工作代码,我就把它放在这里,但我想我们可以考虑这个问题得到解决。
无论如何,感谢所有看过这篇文章的人。

是的,在Windows 7上。在最初的帖子中应该已经明确了这一点,对不起。你能不能修改程序,让你能把你想要通话的实际COM端口传递给它,而不是遍历所有的COM端口,看看这是否有帮助?这是我第一次尝试的。失败后,我决定这样做。设备位于COM端口2上。当我对所有这些进行循环时,程序会检测到COM1和COM2。COM1失败,因为没有连接读卡器设备,但COM2不应该失败。你是绑定到Windows还是可以在Linux上尝试此功能?我可以尝试在Linux上安装它,但任务描述说它最终应该可以在Windows上运行。(我在做实习)明天我和一个同学有个约会,也许他有个主意。如果我找到了解决方案,我会把它贴在这里。谢谢你调查这件事。
/*-------------------------------------------------------
|                                                       |
|                       fecom.h                         |
|                                                       |
---------------------------------------------------------

Copyright  2000-2010    FEIG ELECTRONIC GmbH, All Rights Reserved.
                        Lange Strasse 4
                        D-35781 Weilburg
                        Federal Republic of Germany
                        phone    : +49 6471 31090
                        fax      : +49 6471 310999
                        e-mail   : obid-support@feig.de
                        Internet : http://www.feig.de

Author              :   Markus Hultsch

Version             :   Windows:        02.08.12 / 25.03.2010 / M. Hultsch
Version             :   Linux:          02.08.10 / 18.08.2009 / M. Sahm
Version             :   Windows CE:     02.08.11 / 22.03.2010 / M. Hultsch

Operation Systems   :   Windows 2000/XP/Vista/7
                    Linux
                    Windows CE


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

#ifndef _FECOM_INCLUDE_H
#define _FECOM_INCLUDE_H


#if defined(_MSC_VER) || defined(__BORLANDC__)
#ifdef FECOMDLL
    #define DLL_EXT_FUNC __declspec(dllexport) __stdcall
#else
    #define DLL_EXT_FUNC __declspec(dllimport) __stdcall
#endif

#ifndef CALLBACK
    #define CALLBACK __stdcall
#endif
#else
#define DLL_EXT_FUNC
#define CALLBACK
#endif


// type defines
#ifdef __GNUC__
#ifndef __int64
    #define __int64 long long
#endif
#endif

#ifndef UCHAR
#define UCHAR unsigned char
#endif

#ifndef UINT
#define UINT unsigned int
#endif

#ifdef __cplusplus
    extern "C" {
#endif




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

// 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
#define FECOM_ERR_PORT_NOT_FOUND            -1005
#define FECOM_ERR_UNSUPPORTED               -1006

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

// 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
#define FECOM_ERR_INIT_COMM_PROCESS         -1033
#define FECOM_ERR_FLUSH_INPUT_BUFFER        -1034
#define FECOM_ERR_FLUSH_OUTPUT_BUFFER       -1035
#define FECOM_ERR_CHANGE_PORT_PARA          -1036
#define FECOM_ERR_TRANSMIT_PROCESS          -1037
#define FECOM_ERR_RECEIVE_NOISE_DATA        -1038
#define FECOM_ERR_PORT_IS_BUSY              -1039
#define FECOM_ERR_READ_PORT_PARA            -1040

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



// constants for dynamic load of library

// miscellaneous functions
#define FECOM_GET_DLL_VERSION           1000
#define FECOM_GET_ERROR_TEXT            1001
#define FECOM_GET_LAST_ERROR            1002
#define FECOM_DETECT_PORT               1003

// functions for event notification
#define FECOM_ADD_EVENT_HANDLER         1010
#define FECOM_DEL_EVENT_HANDLER         1011

// port functions
#define FECOM_OPEN_PORT                 1020
#define FECOM_CLOSE_PORT                1021
#define FECOM_GET_PORT_LIST             1022
#define FECOM_GET_PORT_PARA             1023
#define FECOM_SET_PORT_PARA             1024
#define FECOM_DO_PORT_CMD               1025
#define FECOM_GET_PORT_HND              1026

// communication function
#define FECOM_TRANSCEIVE                1030
#define FECOM_TRANSMIT                  1031
#define FECOM_RECEIVE                   1032



// constants for structure _FECOM_EVENT_INIT

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

#define FECOM_PORT_CLOSED_EVENT     20  // only for Windows CE (customized   versions)
#define FECOM_PORT_REOPEN_EVENT     21  // only for Windows CE (customized versions)



// #####################################################
// 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
{
//#if defined(_MSC_VER) || defined(__BORLANDC__)
#if defined(__BORLANDC__)
    DWORD   dwThreadID;         // for thread-ID
    HWND    hwndWnd;            // for window-handle
    HANDLE  hEvent;             // for event-handle
#endif
    void    (*cbFct)(int, int); // for callback-function
#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     );
int  DLL_EXT_FUNC FECOM_DetectPort( int iPortNr );

// 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 library 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*);
typedef int  (CALLBACK* LPFN_FECOM_DETECT_PORT)(int);

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

// port functions
typedef int  (CALLBACK* LPFN_FECOM_OPEN_PORT)(char*);
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