RFID串口读取数据超时
感谢您花时间查看我的问题!
我有一台 Texas Instruments 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,根据文档,这是一个超时错误。是的,我在程序运行时在设备上刷卡;)
我不相信使用这些功能的想法。如果有人可以向我提供他们知道的解决方案,我会同意,或者如果您需要更多信息,请询问。我只是不知道问题是什么。端口打开但未读取数据。真的让我很困惑。我希望你们能帮忙!
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 (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
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论