📄 baccdemo.cpp
字号:
//-----------------------------------------------------------------------------
// (c) 2002 by Basler Vision Technologies
// Section: Vision Components
// Project: BCAM
// $Header: baccdemo.cpp, 11, 19.06.2006 19:07:32, Nebelung, H.$
//-----------------------------------------------------------------------------
/**
\file baccdemo.cpp
\brief Defines the entry point for the console application.
*/
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "resource.h"
#include "ParseCmd.h"
#include <conio.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
/// The one and only application object
CWinApp theApp;
using namespace std;
using namespace Bcam;
typedef std::list< CBcamAdapter* > AdapterList;
/// Visit the node and then the child nodes
static void Visit( CNode* p, int indent, ostream& os )
{
if (! p)
return;
CString s;
s.Format( _T( "%*d, %s -- %s -- %X -- %X%08X" ),
indent, p->PhysicalId(), (LPCSTR)p->VendorName(), (LPCSTR)p->ModelName(),
p->NodeID().LowPart >> 4, p->NodeID().LowPart & 0xff, p->NodeID().HighPart );
os << (LPCTSTR) s << endl;
for (unsigned int i= 0; i<p->NumPorts(); i++)
{
CNodePtr ptr;
ptr.Attach( p->Child( i ), false );
Visit( ptr, indent+2, os );
}
}
/// Get the bus topology of all adapters
void GetTopology()
{
AdapterList m_Adapters; ///< list with all adapters found
const list<CString>devs = CBcamAdapter::DeviceNames();
for (list<CString>::const_iterator sit = devs.begin(); sit != devs.end(); sit++)
{
CBcamAdapter *p = new CBcamAdapter();
m_Adapters.push_back( p );
p->Open( *sit );
}
for (AdapterList::iterator it = m_Adapters.begin(); it != m_Adapters.end(); it++)
{
CNodePtr ptrRoot;
ptrRoot.Attach( (*it)->GetTree(), false );
Visit( ptrRoot, 0, cout );
ptrRoot = 0;
(*it)->Close();
delete *it; *it = NULL;
}
}
/// Reset the bus of the first adapter
void ResetBus()
{
const list <CString> devs = CBcamAdapter::DeviceNames();
if (devs.empty())
{
cerr << "No devices available" << endl;
return;
}
CBcamAdapter Adapter;
Adapter.Open( *devs.begin() );
cout << _T("Resetting ") << (LPCTSTR) Adapter.GetDeviceName() << endl;
Adapter.ResetBus();
Adapter.Close();
}
/// Get the bus resources of the first adapter
void GetBusResource( )
{
const list <CString>devs = CBcamAdapter::DeviceNames();
if (devs.empty())
{
cerr << _T("No devices available") << endl;
return;
}
CBcamAdapter Adapter;
Adapter.Open( *devs.begin() );
TransferRate s = TR_400;
ULONG bpp = Adapter.ResourcesAvailable();
cout << bpp << _T(" Bytes per packet available") << endl;
CBcamAdapter::ISOCHANNELSET ch;
ULONG n;
n = Adapter.NumChannelAvailable( ch );
cout << n << _T(" isochronous channels available:") << endl
<< ch << endl;
Adapter.Close();
}
/// Callback function, which will be invoked if a bus reset occurs.
void BusResetCallback(CBcamAdapter& adapter, void* pv)
{
cout << _T("Bus reset occured. Adapter ") << (int) pv << _T(" (") << (LPCTSTR) adapter.GetDeviceName() << _T(")") << endl;
}
/// Notice bus resets
void NoticeBusResets()
{
const list <CString> devs = CBcamAdapter::DeviceNames();
if (devs.empty())
{
cerr << _T("No devices available") << endl;
return;
}
// for each adapter register a bus reset callback function
AdapterList adapters;
int i = 0;
for ( list<CString>::const_iterator it = devs.begin(); it != devs.end(); it++, i++ )
{
CBcamAdapter* pAdapter = new CBcamAdapter();
pAdapter->Open( *it );
// Register our callback function. When a bus reset occurs, the callback function will be invoked.
// A reference to the adapter and a void pointer holding additional information (such as an instance pointer)
// will be passed to the callback function.
pAdapter->SetOnBusResetCallback(
BusResetCallback, // function pointer
(void*) i // additional information
);
adapters.push_back( pAdapter );
}
cout << _T("Waiting for bus resets. Press a key to exit the program.") << endl;
// wait until a key is pressed
_getch();
// clean up
for ( AdapterList::iterator adapter = adapters.begin(); adapter != adapters.end(); adapter++ )
{
(*adapter)->Close();
delete (*adapter);
}
}
/// Program Options
enum Option
{
optGetTopology, ///< Get the bus topology
optGetBusResources, ///< Get the bus resources
optResetBus, ///< Reset the bus
optNoticeBusResets
};
/// Usage
void Usage( ostream &os )
{
os << _T("BaccDemo [(-|/) (Reset | Topology | Resources | NoticeBusResets | Help | ?) ]") << endl
<< _T("\tReset \t issues a bus reset") << endl
<< _T("\tTopology(default) \t retrieves the bus topology") << endl
<< _T("\tResoucrces \t retrieves the available bus resources") << endl
<< _T("\tNoticeBusResets \t notices bus resets") << endl
<< _T("\t?,Help \t shows this message") << endl;
}
/// Get Option
Option GetOption()
{
CString cl( ::GetCommandLine() );
cl.MakeLower();
CCmdLineParser c( (LPCTSTR)cl );
if (c.isParameter( _T("/?") ) || c.isParameter( _T("/help") ) ||
c.isParameter( _T("-?") ) || c.isParameter( _T("-help") ))
{
Usage( cout );
exit( EXIT_SUCCESS );
}
if (c.isParameter( _T("/reset") ) || c.isParameter( _T("-reset") ))
return optResetBus;
if (c.isParameter( _T("/topology") ) || c.isParameter( _T("-topology") ))
return optGetTopology;
if (c.isParameter( _T("/resources") ) || c.isParameter( _T("-resources") ))
return optGetBusResources;
if ( c.isParameter( _T("/noticebusresets") ) || c.isParameter( _T("-noticebusresets") ))
return optNoticeBusResets;
return optGetTopology;
}
/// Main entry point
int _tmain()
{
int nRetCode = 0;
// 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
{
try
{
switch(GetOption())
{
case optGetBusResources:
GetBusResource();
break;
case optResetBus:
ResetBus();
break;
case optGetTopology:
GetTopology();
break;
case optNoticeBusResets:
NoticeBusResets();
break;
default:
ASSERT( false && _T("no option selected") );
};
}
catch (std::exception &e)
{
cerr << e.what() << endl;
nRetCode = 1;
}
catch (BcamException &e)
{
cerr << _T( "0x" ) << hex << e.Error() << _T(" : ") << (LPCTSTR) e.Description() << endl;
nRetCode = 1;
}
catch (...)
{
BcamException e( ::GetLastError() );
cerr << _T( "setUp: unexpected error" );
cerr << _T( "0x" ) << hex << e.Error() << _T(" : ") << (LPCTSTR) e.Description() << endl;
nRetCode = 1;
}
}
return nRetCode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -