⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbtest.cpp

📁 美国国家半导体公司的扫描仪芯片LM9833的驱动程序。
💻 CPP
字号:
// USBTEST.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "USBTEST.h"
#include "driver.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;
	BYTE *Buffer = NULL; //buffer
	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		// TODO: code your application's behavior here.
		//prototypes
		CString GetStringArg(void);
		int GetHexArg(void);
		const char *HexFormat(int arg);
		//open port
		CDriver Driver;		//Interface to driver
		unsigned int RawMode = 0,Type = 0,Port = 0; //Port variables

		//loop on commands
		int status = 0;		//Return status
		while (1)
		{
			//display last command error
			if (status) cout << "Error" << endl;
			status = 1;
			//get command
			cout << "Enter command (h for list of commands)" << endl;
			CString Command = GetStringArg();
			//exit
			if (Command == "exit")
			{
				break;
			}
			else if (Command == "open")
			{
				if (!Driver.InitDriver())
				{
					cout << "Error opening port" << endl;
					return 1;
				}
			}
			else if (Command == "close")
			{
				if (!Driver.CloseDriver())
				{
					cout << "Error closing port" << endl;
					return 1;
				}
			}
			else if (Command[0] == 'f')
			{
				unsigned char Addr = GetHexArg();	
				unsigned int  Size = GetHexArg();
				CString FileName = GetStringArg();
				Buffer = new BYTE[0x200000];
				if (Command[1] == 'r')
				{
					if (Command == "frbi")
					{
					//read bulk bytes with increment of reg 4,5
					//See LM9830 datasheet; for test set reg 3,4,5,7=0,20h,0,0
					//and read up to 1024 bytes from reg 6
						if (!Driver.BulkIn(Addr,Buffer,Size, true)) continue;					
					}
					else if (Command == "frb")
					{
					//read bulk bytes without increment of reg 4,5
					//Note: not for address 6 for parallel port
						if (!Driver.BulkIn(Addr,Buffer,Size,false)) continue;					
					}
					else if (Command == "fr")
					{
					//read register bytes
					if (!Driver.RegIn(Addr,Buffer, Size)) continue;
					}
					else continue;
					//buffer to file
					FILE* fp = fopen(FileName,"wt");
					if (fp == NULL)
					{
						cout << "Cant open file" << endl;
						continue;
					}
					for (UINT i = 0; i < Size; i++)
					{
						if (fprintf(fp,"%02x ",Buffer[i]) < 0) continue;
						if ((i%16) == 15 ) fprintf(fp,"\n");
					}
					fclose(fp);
				}
				//w
				else if (Command[1] == 'w')
				{
					//file to buffer
					FILE* fp = fopen(FileName,"rt");
					if (fp == NULL)
					{
						cout << "Cant open file" << endl;
						continue;
					}
					for (UINT i = 0; i < Size;)
					{
						//if hit end, rewind
						if (fscanf(fp,"%x",&Buffer[i]) != EOF) i++; 
						else 
						{
							if (!i) break; //no file
							rewind(fp);
						}
					}
					if (!i) continue;
					fclose(fp);

					if (Command == "fwbi")
					{
					//write bulk bytes with increment of reg 4,5
					//See LM9830 datasheet; for test set reg 3,4,5,7,8=0,0,0,0,2
					//and write up to 1024 bytes for register 6
					//Note: only for address 6 for parallel port
						if (!Driver.BulkOut(Addr,Buffer,Size, true)) continue;					
					}
					else if (Command == "fwb")
					{
					//write bulk bytes without increment of reg 4,5
					//Note: not for address 6 for parallel port
						if (!Driver.BulkOut(Addr,Buffer,Size, false)) continue;					
					}
					else if (Command == "fw")
					{
					//write register bytes
						if (!Driver.RegOut(Addr,Buffer, Size)) continue;
					}
					else continue;
				}
				//display file
				for (UINT i = 0; i < min(Size,0x1000); i++)
				{
					cout << HexFormat(Buffer[i]) << " ";
					if ((i%16) == 15 ) cout << endl;
				}
				cout << endl;
			}
			//r
			else if (Command[0] == 'r')
			{
				unsigned char Addr = GetHexArg();	
				unsigned char Value = 0xff;
				if (Command == "rbi")
				{
					//read single bulk byte with increment of reg 4,5
					//note that reg 3,4,5 should be set first 
					//See LM9830 datasheet; for test set reg 3,4,5,7,8=0,20h,0,0,2
					//Note: only for address 6 for parallel port
					if (!Driver.BulkIn(Addr,&Value,1,true)) continue;					
				}
				else if (Command == "rb")
				{
					//read single bulk byte without increment of reg 4,5
					//Note: not for address 6 for parallel port
					if (!Driver.BulkIn(Addr,&Value,1,false)) continue;					
				}
				else if (Command == "r")
				{
					//read single register byte, for example reg 5
					if (!Driver.RegIn(Addr,&Value, 1)) continue;
				}
                else continue;
				cout << HexFormat(Addr) << "=";
				cout << HexFormat(Value) << endl;
			}
			//w
			else if (Command[0] == 'w')
			{
				unsigned char Addr = GetHexArg();	
				unsigned char Value = GetHexArg();
				if (Command == "wbi")
				{
					//write single bulk byte with increment of reg 4,5
					//note that reg 3,4,5 should be set first
					//See LM9830 datasheet; for test set reg 3,4,5,7,8=0,0,0,0,2
					//Note: only for address 6 for parallel por
					if (!Driver.BulkOut(Addr,&Value,1,true)) continue;					
				}
				else if (Command == "wb")
				{
					//write single bulk byte without increment of reg 4,5
					//Note: not for address 6 for parallel port
					if (!Driver.BulkOut(Addr,&Value,1,false)) continue;					
				}
				else if (Command == "w")
				{
					//write single register byte, for example reg 5
					if (!Driver.RegOut(Addr, &Value, 1)) continue;
				}
				else continue;
				cout << HexFormat(Addr) << "=";
				cout << HexFormat(Value) << endl;
			}	
			// s - status 
			else if (Command[0] == 's')
			{
				DWORD dwOnlineState(0), dwEventState(0);
				if (!Driver.QueryStatus(&dwOnlineState, &dwEventState)) continue;					
				cout << "Online State : " << HexFormat(dwOnlineState) << endl;
				cout << "Event State : "  << HexFormat(dwEventState)  << endl;
			}	
			// z - reset 
			else if (Command[0] == 'z')
			{
				if (!Driver.Reset()) continue;					
			}	
			// p - power management
			else if (Command[0] == 'p')
			{
                if (Command == "pe")
                {
                    if (!Driver.EnableRemoteWakeup())
                        continue;
                }
                else if (Command == "pd")
                {
                    if (!Driver.DisableRemoteWakeup())
                        continue;
                }
            }
			// n - notifications
			else if (Command[0] == 'n')
			{
				// Enable device notifications
				if (Command == "ne")
				{
					if (!Driver.EnableNotifications()) continue;					
				}
				else if (Command == "nd")
				{
					if (!Driver.DisableNotifications()) continue;					
				}
				else if (Command == "nc")
				{
					bool bEventOccured;
					GUID guidEventCode;

					if (!Driver.CheckForEvents(bEventOccured, guidEventCode)) continue;
					if (bEventOccured)
					{
						cout << HexFormat(guidEventCode.Data1)  << "-";
						cout << HexFormat(guidEventCode.Data2)  << "-"; 
						cout << HexFormat(guidEventCode.Data3)  << "-";
						cout << HexFormat(guidEventCode.Data4[0]) ;
						cout << HexFormat(guidEventCode.Data4[1]) << "-"  ;
						cout << HexFormat(guidEventCode.Data4[2]) ;
						cout << HexFormat(guidEventCode.Data4[3]) << "-"  ;
						cout << HexFormat(guidEventCode.Data4[4]) ;
						cout << HexFormat(guidEventCode.Data4[5]) << "-"  ;
						cout << HexFormat(guidEventCode.Data4[6]) ;
						cout << HexFormat(guidEventCode.Data4[7]) ;
						cout << endl;
					}
					cout << (bEventOccured ? "Event Detected!" : "No Events") << endl;
				}
				else continue;
			}	
			else 
			{
				cout << endl;
				cout << "Control Commands" << endl;
				cout << "exit                       //quit" << endl;
				cout << "open						//open" << endl;
				cout << "close						//close" << endl;							
				cout << "z							//device reset" << endl;
				cout << "s							//get status" << endl;
				cout << "ne							//enable notifications" << endl;						
				cout << "nc							//check for events" << endl;
				cout << "nd							//disable notifications" << endl;
				cout << "pe							//enable remotewakeup" << endl;						
				cout << "pd							//disable remotewakeup" << endl;
				cout << "Data Commands" << endl;
				cout << "r    addr                  //read one reg value" << endl;
				cout << "rb   addr                  //read one bulk value" << endl;
				cout << "rbi  addr                  //read one bulk value with increment" << endl;
				cout << "w    addr value            //write one reg value" << endl;
				cout << "wb   addr value            //write one bulk value" << endl;
				cout << "wbi  addr value            //write one bulk value with increment" << endl;
				cout << "//following are multiple byte versions of above, to/from filename" << endl;
				cout << "fr   addr size filename" << endl;
				cout << "frb  addr size filename" << endl;
				cout << "frbi addr size filename" << endl;
				cout << "fw   addr size filename" << endl;
				cout << "fwb  addr size filename" << endl;
				cout << "fwbi addr size filename" << endl;
				cout << "Where" << endl;
				cout << "addr is register addr, value is one byte" << endl;
				cout << "size is number of bytes read/written to filename" << endl;
				cout << "numeric arguments are in hex" << endl << endl;
			}
			status = 0;
		}
	}
	if (Buffer) delete Buffer;
	return nRetCode;
}

//get arg
CString GetStringArg(void)
{
	char arg[80];
	cin >> arg;
	CString s = arg;
	s.MakeLower();
	return s;
}
//return TRUE if ok
int GetHexArg(void)
{
	char arg[80];
	cin >> arg;
	int i;
	if (sscanf(arg,"%x",&i) != 1) return 0;
	return i;
}
//return Cstring in hex
const char* HexFormat(int arg)
{
	static CString s;
	s.Format("%02x",arg);
	return (LPCSTR)s;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -