Rework driver code, again.

This commit is contained in:
2026-01-01 13:28:17 +01:00
parent 6b039e4b7f
commit 6e860d8d05
10 changed files with 91 additions and 71 deletions

View File

@@ -35,7 +35,7 @@
#include <bcdrivertinycan.h>
#include <can_drv.h>
#include <expected>
@@ -57,16 +57,15 @@ void BCDriverTinyCan::onStartDriver()
void BCDriverTinyCan::loadDriver()
{
using Result=std::expected<DriverState,QString>;
auto callLoadDriver = []() -> Result
auto callLoadDriver = []() -> DriverStateResult
{
if( ::LoadDriver( NULL ) < 0 )
return std::unexpected(QString("Driver Error: 'LoadDriver'"));
return DriverState::Loaded;
};
auto callInitDriver = [](DriverState state) -> Result
auto callInitDriver = [](DriverState state) -> DriverStateResult
{
Q_UNUSED(state)
if( ::CanInitDriver( NULL ) < 0 )
@@ -74,7 +73,7 @@ void BCDriverTinyCan::loadDriver()
return DriverState::Initialized;
};
auto callOpenDevice = [](DriverState state) -> Result
auto callOpenDevice = [](DriverState state) -> DriverStateResult
{
Q_UNUSED(state)
if( ::CanDeviceOpen( 0, NULL ) < 0 )
@@ -122,27 +121,27 @@ BCDriver::DriverState BCDriverTinyCan::connectDriver()
// Console already in slave mode. good!
if( readRawByte( console, slaveFlag ) )
return DriverState::Ready;
return DriverState::DeviceReady;
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;
TransmitResult isSlave = 0;
do
{
writeRawByte( console, slaveFlag, 1 );
isSlave = readRawByte( console, slaveFlag );
isSlave = readRawByte( console, slaveFlag );
bc::delay_millis( 200 );
} while( retry-- && !isSlave );
} 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;
return isSlave ? DriverState::DeviceReady : DriverState::Opened;
}
@@ -165,12 +164,18 @@ BCDriver::DriverState BCDriverTinyCan::connectDriver()
uint32_t BCDriverTinyCan::readRawByte( uint32_t deviceID, uint8_t registerID ) const
BCDriver::TransmitResult BCDriverTinyCan::readRawByte( uint32_t deviceID, uint8_t registerID ) const
{
if( getState() != DriverState::Ready)
throw BCException( "readRawValue error: driver not loaded." );
//TransmitResult
qDebug() << " --- DriverState: " << _driverState;
/*
Nicht hier!
if( getState() != DriverState::DeviceReady)
//throw BCException( "readRawValue error: driver not loaded." );
return std::nullopt;
*/
::TCanMsg msg;
// msg verpacken
@@ -192,7 +197,7 @@ uint32_t BCDriverTinyCan::readRawByte( uint32_t deviceID, uint8_t registerID ) c
bc::delay_millis( cTIMEOUT_MS );
if( timeOuts == -1 )
throw BCException( "readRawValue error: could not send value" );
return std::unexpected(QString("readRawValue error: could not send value" ));
retry:
@@ -202,8 +207,8 @@ retry:
while( timeOuts-- && !::CanReceiveGetCount( 0 ) )
bc::delay_millis( cTIMEOUT_MS );
if( timeOuts == -1 )
throw BCException( "getValue error: no response from node" );
if( timeOuts == -1 )
return std::unexpected(QString("getValue error: no response from node" ));
// message empfangen
int err = ::CanReceive( 0, &msg, 1 );
@@ -222,22 +227,23 @@ retry:
goto retry;
if( !timeOuts )
throw BCException( "CAN --response errror" );
return std::unexpected(QString("CAN response errror: timeout" ));
return (uint32_t) msg.MsgData[3];
return (uint8_t) msg.MsgData[3];
}
// void BCDriverTinyCan::setValue( unsigned char receipient, unsigned char reg, unsigned char value )
void BCDriverTinyCan::writeRawByte( uint32_t deviceID, uint8_t registerID ,uint8_t value ) const
BCDriver::TransmitResult BCDriverTinyCan::writeRawByte( uint32_t deviceID, uint8_t registerID ,uint8_t value ) const
{
if( getState() != DriverState::Ready)
/*
Nicht hier!
if( getState() != DriverState::DeviceReady)
throw BCException( "writeRawValue error: driver not loaded." );
*/
qDebug() << " --- BCDriverTinyCan writeRawValue: " << value;
return;
::TCanMsg msg;
int timeout_count = cTIMEOUT_COUNT;
@@ -256,7 +262,8 @@ void BCDriverTinyCan::writeRawByte( uint32_t deviceID, uint8_t registerID ,uint8
bc::delay_millis( cTIMEOUT_MS );
if( timeout_count == -1 )
emit driverStateChanged( _driverState, QString( "error: could not send value to %1" ).arg( deviceID ) );
return std::unexpected(QString("error: could not send value to %1" ).arg( deviceID ) );
return 1; // als 'true'
}