Reworked driver code, part II

This commit is contained in:
2026-01-01 01:58:54 +01:00
parent 3ed3511f50
commit 550382207e
5 changed files with 55 additions and 87 deletions

View File

@@ -100,20 +100,14 @@ BCDriverDummy::BCDriverDummy( QObject* parent )
{ {
} }
BCDriver::DriverState BCDriverDummy::loadAndInitDriver()
{
return BCDriver::DriverState::Ready;
}
uint32_t BCDriverDummy::readRawByte( uint32_t deviceID, uint8_t registerID ) const uint32_t BCDriverDummy::readRawByte( uint32_t deviceID, uint8_t registerID ) const
{ {
//if( getState() != DriverState::Ready) if( getState() != DriverState::Ready)
// throw BCException( "readRawValue error: driver not loaded." ); throw BCException( "readRawValue error: driver not loaded." );
uint32_t myRandomByte = static_cast<uint32_t>(QRandomGenerator::global()->bounded(256)); uint32_t myRandomByte = static_cast<uint32_t>(QRandomGenerator::global()->bounded(256));
return myRandomByte; return myRandomByte;
} }
void BCDriverDummy::writeRawByte( uint32_t deviceID, uint8_t registerID, uint8_t value ) const void BCDriverDummy::writeRawByte( uint32_t deviceID, uint8_t registerID, uint8_t value ) const

View File

@@ -96,7 +96,7 @@ public:
Error, Error,
Loaded, Loaded,
Initialized, Initialized,
Opended, // bis hierher: dll vorhanden, Treiber geladen Opened, // bis hierher: dll vorhanden, Treiber geladen
Ready // hier: devices connectable Ready // hier: devices connectable
}; };
Q_ENUM(DriverState) Q_ENUM(DriverState)
@@ -115,7 +115,7 @@ public slots:
signals: signals:
void driverStateChanged( DriverState state, const QString& message="" ); void driverStateChanged( DriverState state, const QString& message="" ) const;
protected: protected:

View File

@@ -46,6 +46,14 @@ BCDriverTinyCan::BCDriverTinyCan( QObject* parent )
} }
void BCDriverTinyCan::onStartDriver()
{
if( _driverState < DriverState::Opened)
loadDriver();
if( _driverState == DriverState::Opened)
_driverState = connectDriver();
}
void BCDriverTinyCan::loadDriver() void BCDriverTinyCan::loadDriver()
{ {
@@ -71,12 +79,7 @@ void BCDriverTinyCan::loadDriver()
Q_UNUSED(state) Q_UNUSED(state)
if( ::CanDeviceOpen( 0, NULL ) < 0 ) if( ::CanDeviceOpen( 0, NULL ) < 0 )
return std::unexpected(QString("Driver Error: 'DeviceOpen'")); return std::unexpected(QString("Driver Error: 'DeviceOpen'"));
return DriverState::Opended;
};
auto callSetDeviceMode = [](DriverState state) -> Result
{
Q_UNUSED(state)
::TDeviceStatus deviceStatus; ::TDeviceStatus deviceStatus;
::CanSetSpeed( 0, CAN_125K_BIT ); ::CanSetSpeed( 0, CAN_125K_BIT );
@@ -84,20 +87,19 @@ void BCDriverTinyCan::loadDriver()
::CanGetDeviceStatus( 0, &deviceStatus ); ::CanGetDeviceStatus( 0, &deviceStatus );
if( deviceStatus.DrvStatus < DRV_STATUS_CAN_OPEN ) if( deviceStatus.DrvStatus < DRV_STATUS_CAN_OPEN )
std::unexpected(QString("Driver Error: could not open device." )); return std::unexpected(QString("Driver Error: could not open device." ));
if( deviceStatus.CanStatus == CAN_STATUS_BUS_OFF ) if( deviceStatus.CanStatus == CAN_STATUS_BUS_OFF )
{ {
::CanSetMode( 0, OP_CAN_RESET, CAN_CMD_NONE ); ::CanSetMode( 0, OP_CAN_RESET, CAN_CMD_NONE );
std::unexpected(QString("Driver Error: CAN Status 'BusOff'" )); return std::unexpected(QString("Driver Error: CAN Status 'BusOff'" ));
} }
return BCDriver::DriverState::Ready; return DriverState::Opened;
}; };
auto newDriverState = callLoadDriver() auto newDriverState = callLoadDriver()
.and_then( callInitDriver ) .and_then( callInitDriver )
.and_then( callOpenDevice ) .and_then( callOpenDevice );
.and_then( callSetDeviceMode );
_driverState = DriverState::Error; _driverState = DriverState::Error;
QString message("Driver Ready."); QString message("Driver Ready.");
@@ -110,8 +112,37 @@ void BCDriverTinyCan::loadDriver()
emit driverStateChanged( _driverState, message ); emit driverStateChanged( _driverState, message );
} }
void BCDriverTinyCan::initDriver() // __fix
BCDriver::DriverState BCDriverTinyCan::connectDriver()
{ {
uint32_t console = static_cast<uint32_t>(BCDevice::ID::Console);
uint8_t slaveFlag = static_cast<uint8_t>(BC::ID::Cons_Status_Slave);
qDebug() << "XXX BCDriverTinyCan::Driver Init: putting Console in slave mode ... ";
// Console already in slave mode. good!
if( readRawByte( console, slaveFlag ) )
return DriverState::Ready;
qDebug() << "BCDriverTinyCan::BCDriverTinyCan::XXX Driver Init: putting Console in slave mode ... ";
unsigned int retry = cTimeOuts;
emit driverStateChanged( _driverState, "Driver Init: putting Console in slave mode ... " );
uint32_t isSlave = 0;
do
{
writeRawByte( console, slaveFlag, 1 );
isSlave = readRawByte( console, slaveFlag );
bc::delay_millis( 200 );
} while( retry-- && !isSlave );
bc::delay_millis( 500 ); // give the Console some time to settle
//if( !isSlave )
//emit statusHint( QString("putting Console in slave mode ") + (isSlave ? "done" : "failed") );
// ist das jetzt irgendwie schlimm, wenn wir keine slave Console haben
return isSlave ? DriverState::Ready : DriverState::Opened;
} }
@@ -119,58 +150,20 @@ void BCDriverTinyCan::initDriver()
/* /*
try try
{ {
loadAndInitDriver();
initBCDevice::ID::Console(); initBCDevice::ID::Console();
} }
catch( std::exception& except ) catch( std::exception& except )
{ {
::CanDownDriver(); ::CanDownDriver();
::UnloadAndInitDriver(); ::UnloadDriver();
// re-throw // re-throw
throw BCException( except.what() ); throw BCException( except.what() );
} }
*/ */
/*
BCDriver::DriverState BCDriverTinyCan::initDriver()
{
uint32_t console = static_cast<uint32_t>(BCDevice::ID::Console);
uint8_t slaveFlag = static_cast<uint8_t>(BC::ID::Cons_Status_Slave);
qDebug() << "XXX BCDriverTinyCan::Driver Init: putting BCDevice::ID::Console in slave mode ... ";
// BCDevice::ID::Console already in slave mode. good!
if( readRawByte( console, slaveFlag ) )
return DriverState::Ready;
qDebug() << "BCDriverTinyCan::BCDriverTinyCan::XXX Driver Init: putting BCDevice::ID::Console in slave mode ... ";
unsigned int retry = cTimeOuts;
emit statusHint( "Driver Init: putting BCDevice::ID::Console in slave mode ... " );
uint32_t isSlave = 0;
do
{
writeRawByte( console, slaveFlag, 1 );
isSlave = readRawByte( console, slaveFlag );
bc::delay_millis( 200 );
} while( retry-- && !isSlave );
bc::delay_millis( 500 ); // give the Console some time to settle
//if( !isSlave )
emit statusHint( QString("putting BCDevice::ID::Console in slave mode ") + (isSlave ? "done" : "failed") );
// ist das jetzt irgendwie schlimm, wenn wir keine slave BCDevice::ID::Console haben
//return isSlave ? BCDriver::connected
return DriverState::Ready;
}
*/
uint32_t BCDriverTinyCan::readRawByte( uint32_t deviceID, uint8_t registerID ) const uint32_t BCDriverTinyCan::readRawByte( uint32_t deviceID, uint8_t registerID ) const
{ {
@@ -263,7 +256,7 @@ void BCDriverTinyCan::writeRawByte( uint32_t deviceID, uint8_t registerID ,uint8
bc::delay_millis( cTIMEOUT_MS ); bc::delay_millis( cTIMEOUT_MS );
if( timeout_count == -1 ) if( timeout_count == -1 )
emit statusHint( QString( "error: could not send value to %1" ).arg( deviceID ) ); emit driverStateChanged( _driverState, QString( "error: could not send value to %1" ).arg( deviceID ) );
} }

View File

@@ -49,26 +49,23 @@ public:
QString getNodeName( unsigned char id ); QString getNodeName( unsigned char id );
public slots:
void onStartDriver() override;
protected: protected:
void loadDriver(); void loadDriver();
void initDriver(); DriverState connectDriver();
// typen?
//void setValue( unsigned char receipient, unsigned char reg, unsigned char value );
//unsigned int getValue( unsigned char receipient, unsigned char reg );
//const char* CBCDLL_LIN = "libmhstcan.so"; //const char* CBCDLL_LIN = "libmhstcan.so";
//const char* CBCDLL_WIN = "mhstcan.dll"; //const char* CBCDLL_WIN = "mhstcan.dll";
static constexpr int cRetries = 5; static constexpr int cRetries = 5;
static constexpr int cTimeOuts = 20; static constexpr int cTimeOuts = 20;
static constexpr int cTIMEOUT_MS = 10; // 10ms static constexpr int cTIMEOUT_MS = 10; // 10ms
static constexpr int cTIMEOUT_COUNT = 10; static constexpr int cTIMEOUT_COUNT = 10;
//const unsigned int BATTERY = 0x010; //const unsigned int BATTERY = 0x010;
//const unsigned int MOTOR = 0x020; //const unsigned int MOTOR = 0x020;
const unsigned int BIB = 0x048; const unsigned int BIB = 0x048;

View File

@@ -50,7 +50,7 @@ void BCTransmitter::onToggleConnectionState( bool connect )
if( _canDriver->getState() != BCDriver::DriverState::Ready ) if( _canDriver->getState() != BCDriver::DriverState::Ready )
_canDriver->onStartDriver(); _canDriver->onStartDriver();
// fix! // __fix!
uint32_t hwVersion = _canDriver->readRawByte( (uint32_t)BCDevice::ID::Console, (uint8_t)BC::ID::Cons_Rev_Hw); uint32_t hwVersion = _canDriver->readRawByte( (uint32_t)BCDevice::ID::Console, (uint8_t)BC::ID::Cons_Rev_Hw);
if(!hwVersion) if(!hwVersion)
@@ -59,23 +59,7 @@ void BCTransmitter::onToggleConnectionState( bool connect )
} }
else else
{ {
/*
swVersion = getValue(CONSOLE, CONSOLE_REF_SW);
printf( "Console information:" _NL
" hardware version ........: %02d" _NL
" software version ........: %02d" _NL
" assistance level ........: %d" _NL,
hwVersion, swVersion,
getValue(CONSOLE, CONSOLE_ASSIST_INITLEVEL)
);
if (!gNoSerialNumbers)
printf( " part number .............: %05d" _NL
" item number .............: %05d" _NL _NL,
((getValue(CONSOLE, CONSOLE_SN_PN_HI) << 8) + getValue(CONSOLE, CONSOLE_SN_PN_LO)),
((getValue(CONSOLE, CONSOLE_SN_ITEM_HI) << 8) + getValue(CONSOLE, CONSOLE_SN_ITEM_LO))
);
*/
} }