Files
MIL_RT_MODE/bcmil.cpp
2025-08-12 21:51:30 +03:00

196 lines
4.7 KiB
C++

#include <QDebug>
#include "bcmil.h"
#include "WDMTMKv2.cpp"
//#include <windows.h>
//TTmkEventData tmkEvD;
HANDLE hBcEvent;
//HANDLE hRtEvent;
int nTmk;
unsigned long dwGoodStarts = 0, dwBusyStarts = 0, dwErrStarts = 0, dwStatStarts = 0;
unsigned long dwStarts = 0L;
TMK_DATA wSubAddr, wState, wStatus;
MIL::MIL(int dev_index) //open the driver, configuration, create event
{
qDebug() << "MIL constructor";
if (TmkOpen())
{
qDebug() << "TmkOpen() error";
closeAllrt();
//closeAll();
}
else
{
if ((dev_index > tmkgetmaxn()) || (dev_index < 0))
{
closeAllrt();
//closeAll();
}
else
{
this->m_TmkIndex = dev_index;
if (tmkconfig(this->m_TmkIndex))
{
closeAllrt();
//closeAll();
}
}
}
}
/*int MIL::WaitInt(TMK_DATA wCtrlCode)
{
//Wait for an interrupt
DWORD k;
k = WaitForSingleObject(hBcEvent, 1000);
switch (k)
{
case WAIT_OBJECT_0:
ResetEvent(hBcEvent);
break;
case WAIT_TIMEOUT:
qDebug() << "Interrupt timeout error\n";
return 1;
default:
qDebug() << "k = " << k;
qDebug() << "Interrupt wait error\n";
return 1;
}
// Get interrupt data
// We do not need to check tmkEvD.nInt because bcstartx with CX_NOSIG
// guarantees us only single interrupt of single type nInt == 3
tmkgetevd(&tmkEvD);
if (tmkEvD.bcx.wResultX & SX_IB_MASK)
{
// We have set bit(s) in Status Word
if (((tmkEvD.bcx.wResultX & SX_ERR_MASK) == SX_NOERR) || ((tmkEvD.bcx.wResultX & SX_ERR_MASK) == SX_TOD))
{
wStatus = bcgetansw(wCtrlCode); // We have either no errors or Data Time Out (No Data) error
if (wStatus & BUSY_MASK) // We have BUSY bit set
++dwBusyStarts;
else // We have unknown bit(s) set
++dwStatStarts;
}
else // We have an error
++dwErrStarts;
}
else if (tmkEvD.bcx.wResultX & SX_ERR_MASK) // We have an error
++dwErrStarts;
else // We have a completed message
++dwGoodStarts;
++dwStarts;
return 0;
}*/
/*void MIL::closeAll()
{
qDebug() << "Close All";
//printf("\nGood: %ld, Busy: %ld, Error: %ld, Status: %ld\n", dwGoodStarts, dwBusyStarts, dwErrStarts, dwStatStarts);
bcreset();
//Close all opened things
CloseHandle(hBcEvent);
tmkdone(ALL_TMKS);
TmkClose();
}*/
void MIL::closeAllrt()
{
qDebug() << "Close All";
//printf("\nGood: %ld, Busy: %ld, Error: %ld, Status: %ld\n", dwGoodStarts, dwBusyStarts, dwErrStarts, dwStatStarts);
rtreset();
//Close all opened things
CloseHandle(hBcEvent);
tmkdone(ALL_TMKS);
TmkClose();
}
MIL::~MIL()
{
qDebug() << "MIL destructor";
}
/*BC_MIL::BC_MIL(int dev_index): MIL(dev_index)
{
qDebug() << "BC_MIL constructor";
//Find first configured device
for (nTmk = 0; nTmk <= MAX_TMK_NUMBER; ++nTmk)
if (!tmkselect(nTmk))
break;
if (nTmk > MAX_TMK_NUMBER)
{
qDebug() << "tmkselect error";
closeAll();
}
//Try to reset in BC mode
if (bcreset())
{
qDebug() << "bcreset error";
closeAll();
}
}
BC_MIL::~BC_MIL()
{
qDebug() << "BC_MIL destructor";
}*/
RT_MIL::RT_MIL(int dev_index, unsigned short Addr): MIL(dev_index)
{
qDebug() << "RT_MIL constructor";
this->m_Addr = Addr;
//Find first configured device
for (nTmk = 0; nTmk <= MAX_TMK_NUMBER; ++nTmk)
if (!tmkselect(nTmk))
break;
if (nTmk > MAX_TMK_NUMBER)
{
qDebug() << "tmkselect error";
closeAllrt();
}
//Try to reset in RT mode
if (rtreset())
{
qDebug() << "rtreset error";
closeAllrt();
}
//Set RT address, set flag mode, enable data irqs
rtdefaddress(Addr);
rtdefmode(rtgetmode()|RT_FLAG_MODE);
//rtdefmode(rtgetmode()&~RT_FLAG_MODE);
rtdefirqmode(rtgetirqmode()&~RT_DATA_BL);
for (wSubAddr = 1; wSubAddr <= 30; ++wSubAddr)
{
//Ready to receive, not ready to transmit
rtdefsubaddr(RT_RECEIVE, wSubAddr);
rtclrflag();
rtdefsubaddr(RT_TRANSMIT, wSubAddr);
rtclrflag();
}
hBcEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!hBcEvent)
{
qDebug() << "create event error";
closeAllrt();
}
ResetEvent(hBcEvent);
tmkdefevent(hBcEvent, TRUE);
}
RT_MIL::~RT_MIL()
{
qDebug() << "RT_MIL destructor";
}