📄 rtdxevent.cpp
字号:
// RtdxEvent.cpp: implementation of the CRtdxEvent class.
//
//////////////////////////////////////////////////////////////////////
#include "RtdxEvent.h"
#define HEADER_FORMAT "%10s %10s %20s %10s"
#define BUFFER1_FORMAT "%10i %#10x %20s"
#define BUFFER2_FORMAT "%10i"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CRtdxEvent::CRtdxEvent()
{
// Initialize pointers and variables
m_pEvent=NULL;
m_total_events=0;
m_integer_size=0;
m_FOUND_MSGID=FALSE;
m_logfile.open("EventProg.txt", ios::out); // Open log file
if (!m_logfile)
{
cerr << "\nError: *** Log file could not be created! ***\n";
exit(1);
}
}
CRtdxEvent::~CRtdxEvent()
{
// Return all allocated memory back to heap
if (m_pEvent) delete [] m_pEvent;
}
void CRtdxEvent::SetTargetIntSize(char *TargetIntSize)
{
// If command-line argument is for 16-Bit
if ((strcmp(TargetIntSize,"-2"))==0)
m_integer_size = 2; // Set 16-Bit integer size
else
m_integer_size = 4; // Set 32-Bit integer size
return;
}
unsigned int CRtdxEvent::GetTargetIntSize()
{
return m_integer_size; // Return target integer size
}
void CRtdxEvent::NameEvents()
{
int i=0;
long status;
if (m_total_events <= 0) // If there are no events
return;
do
{
char event_name[80]="";
long id=0;
// Prompt for naming each event
cout << "\nEnter a name(MAX of 80 CHARS.) for event "
<< (i + 1)
<< " >";
cin >> event_name;
status = m_pRtdx->GetChannelID(event_name, &id);
if (id != NULL)
{
m_pEvent[i].id = id;
strcpy(m_pEvent[i].name, event_name);
} else {
char option='\0';
cout << "\nEvent "
<< event_name
<< " is not found\n";
cout << "\nDo you wish to reenter event name [y/n] >";
cin >> option;
if ((option == 'y') || (option == 'Y'))
{
i--; // decrement counter
}
}
i++;
} while (i <= (signed)(m_total_events - 1));
return;
}
HRESULT CRtdxEvent::GetRTDXObject(char *channel_name)
{
USES_CONVERSION;
CBoardProcessor boardproc;
TCHAR BoardName[MAX_PATH];
TCHAR ProcessorName[MAX_PATH];
long status=Success;
HRESULT hr=S_OK;
::CoInitialize(NULL); // initialize COM
// ########## RTDX(TM) ########## //
hr = m_pRtdx.CreateInstance(__uuidof(RTDXINTLib::RtdxExp));
// ########## RTDX(TM) ########## //
if (!SUCCEEDED(hr))
{
cerr << "\n*** Error: Instantiation Failed! ***\n";
m_logfile << "\n*** Error: Instantiation Failed! ***\n";
return E_FAIL;
}
// Get desired board and processor
if (boardproc.GetBoard((TCHAR*)&BoardName) == FALSE) {
cerr << _T("\n*** Error: Unable to get desired board!.\n***");
return -1;
}
if (boardproc.GetProcessor((TCHAR*)&BoardName,(TCHAR*)&ProcessorName) == FALSE) {
cerr << _T("\n*** Error: Unable to get desired processor!.\n***");
return -1;
}
status = m_pRtdx->SetProcessor(T2BSTR(BoardName),
T2BSTR(ProcessorName));
if (status != Success) {
cerr << "\n*** Error: Call to SetProcessor failed!.\n***"
<< "Attaching to default board(0) and processor(0).\n";
m_logfile << "\n*** Error: Call to SetProcessor failed!.\n***"
<< "Attaching to default board(0) and processor(0).\n";
}
// ########## RTDX(TM) ########## //
status = m_pRtdx->Open(channel_name, "R");
// ########## RTDX(TM) ########## //
if (status != Success)
{
cerr << "\n*** Error: Opening of a channel failed! ***\n";
m_logfile << "\n*** Error: Opening of a channel failed! ***\n";
return Failure;
}
return S_OK;
}
long CRtdxEvent::ProbeChannel()
{
char header[80]="";
long msgid=0; // Message id returned from GetMsgID
long probe_status=0;
char *windir = getenv("windir"); // get windows directory
char editor[MAX_PATH];
strcpy(editor, windir);
strcat(editor, "\\notepad.exe");
// Print Heading
sprintf(header, HEADER_FORMAT,"Message #","Message ID","Event","Data");
cout << "\n" << header << endl;
m_logfile << "\n" << header << endl;
do
{
// ########## RTDX(TM) ########## //
probe_status = m_pRtdx->GetMsgID(&msgid);
// ########## RTDX(TM) ########## //
switch (probe_status)
{
case ENoDataAvailable:
cout << "\n\nNo Data is currently available!" << endl;
cout << "\n\nWould you like to continue reading [y or n]?" << endl;
char option;
cin >> option;
if ((option == 'y') || (option == 'Y'))
break;
else
exit(1);
break;
case EEndOfLogFile:
cout << "\n\nData Processing Complete!" << endl;
m_logfile << "\n\nData Processing Complete!" << endl;
m_logfile.close(); // Close log file
ShellExecute(NULL, NULL,editor,"EventProg.txt",NULL,SW_SHOW);
break;
case Failure:
cerr << "\n*** Error: Attemp to get message id failed! ***\n";
m_logfile << "\n*** Error: Attemp to get message id failed! ***\n";
exit(1);
break;
case Success:
ProcessMessage(msgid);
break;
default:
cerr << "\n*** Error: Unknown return code! ***\n";
m_logfile << "\n*** Error: Unknown return code! ***\n";
exit(1);
} // End Switch
}while (probe_status != EEndOfLogFile);
return Success;
}
void CRtdxEvent::ProcessMessage(long msgid)
{
char buffer1[256]="";
char *buffer2=NULL;
long msgnumber=0; // Message sequence number
long read_status=0; // Status of ReadSA{I2,I4}
long status=0; // Status of GetMsgNumber
VARIANT sa; // Variant pointer to SAFEARRAY
int i;
// ########## RTDX(TM) ########## //
status = m_pRtdx->GetMsgNumber(&msgnumber);
// ########## RTDX(TM) ########## //
if (status != Success)
{
cerr << "\n*** Error: Attempt to get message number failed! ***\n";
m_logfile << "\n*** Error: Attempt to get message number failed! ***\n";
return;
}
for (i=0; i<=(signed)(m_total_events - 1); i++)
{
if (m_pEvent[i].id == msgid)
{ // If Message ID matches one of the listed ID's, print data
m_FOUND_MSGID = TRUE;
sprintf(buffer1, BUFFER1_FORMAT, msgnumber, msgid, m_pEvent[i].name);
break;
}
}
if (!(m_FOUND_MSGID))
sprintf(buffer1, BUFFER1_FORMAT, msgnumber, msgid, " ");
cout << "\n" << buffer1;
m_logfile << "\n" << buffer1;
// Initialize variant pointer
VariantInit(&sa);
// Read message(array)
if (m_integer_size == 2)
// ########## RTDX(TM) ########## //
read_status = m_pRtdx->ReadSAI2(&sa);
// ########## RTDX(TM) ########## //
else
// ########## RTDX(TM) ########## //
read_status = m_pRtdx->ReadSAI4(&sa);
// ########## RTDX(TM) ########## //
if ((read_status == Success) &&
(sa.parray->rgsabound[0].cElements > 0))
{
// allocate enough memory for data
buffer2 = new char[sa.parray->rgsabound[0].cElements*20];
// Extract elements out of array
for (i=0; i<(signed)sa.parray->rgsabound[0].cElements; i++)
{
if (i==0)
{
if (m_integer_size == 2)
sprintf(buffer2, BUFFER2_FORMAT, ((short *)sa.parray->pvData)[i]);
else
sprintf(buffer2, BUFFER2_FORMAT, ((long *)sa.parray->pvData)[i]);
} else {
char tempbuf[20]="";
if (m_integer_size == 2)
sprintf(tempbuf, BUFFER2_FORMAT, ((short *)sa.parray->pvData)[i]);
else
sprintf(tempbuf, BUFFER2_FORMAT, ((long *)sa.parray->pvData)[i]);
strcat(buffer2,tempbuf);
}
}
cout << buffer2;
m_logfile << buffer2;
cout << flush;
m_logfile << flush;
if (buffer2) delete [] buffer2;
}
VariantClear(&sa); // Clear variant
m_FOUND_MSGID=FALSE;
}
long CRtdxEvent::ReleaseRTDXObject()
{
long status=0;
// ########## RTDX(TM) ########## //
status = m_pRtdx->Close();
// ########## RTDX(TM) ########## //
m_pRtdx.Release();
CoUninitialize(); // Uninitialize COM
return status;
}
void CRtdxEvent::SetNumOfEvents(char *NumOfEvents)
{
int i=0;
i = atoi((const char*)(NumOfEvents));
if (i >= 0)
{
m_total_events = i;
m_pEvent = new EVENTATTRIB[m_total_events]; // Allocate memory for events
}
}
unsigned int CRtdxEvent::GetNumOfEvents()
{
return m_total_events;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -