diff --git a/drv/b15f.cpp b/drv/b15f.cpp index ff95d66..627e3ba 100644 --- a/drv/b15f.cpp +++ b/drv/b15f.cpp @@ -264,6 +264,8 @@ uint16_t B15F::analogeEingabe(uint8_t channel) bool B15F::analogEingabeSequenz(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t* buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count) { + + try { writeByte(RQ_ADC_DAC_STROKE); @@ -276,19 +278,24 @@ bool B15F::analogEingabeSequenz(uint8_t channel_a, uint16_t* buffer_a, uint32_t if(aw != MSG_OK) { - std::cout << PRE << "Mikrocontroller nicht synchronisiert" << std::endl; - throw DriverException("Mikrocontroller nicht synchronisiert"); + discard(); + return analogEingabeSequenz(channel_a, buffer_a, offset_a, channel_b, buffer_b, offset_b, start, delta, count); } for(uint16_t i = 0; i < count; i++) { - buffer_a[offset_a + i] = readInt(); - buffer_b[offset_b + i] = readInt(); - if(buffer_a[offset_a + i] > 1023 || buffer_b[offset_b + i] > 1023) + uint8_t block[4]; + bool crc_ok = true; + do { - std::cout << PRE << "Schlechte Werte gefunden" << std::endl; - throw DriverException("Schlechte Werte gefunden"); + crc_ok = readBlock(&block[0], 0); + if(!crc_ok) + std::cout << "fordere neu an" << std::endl; } + while(!crc_ok); + + buffer_a[offset_a + i] = ((uint16_t) block[0]) | (((uint16_t) block[1]) << 8); + buffer_b[offset_b + i] = ((uint16_t) block[2]) | (((uint16_t) block[3]) << 8); } aw = readByte(); @@ -356,6 +363,74 @@ uint16_t B15F::readInt() return readByte() | readByte() << 8; } +bool B15F::readBlock(uint8_t* buffer, uint16_t offset) +{ + uint8_t len = readByte(); + uint8_t crc = 0; + buffer += offset; + + // wait for block + int n_ready; + uint16_t elapsed = 0; + auto start = std::chrono::steady_clock::now(); + auto end = start; + while(elapsed < block_timeout) + { + int code = ioctl(usart, FIONREAD, &n_ready); + if(code != 0) + { + std::cout << PRE << "n_ready code: " << code << std::endl; + return false; + } + if(n_ready >= len + 1) + break; + end = std::chrono::steady_clock::now(); + elapsed = std::chrono::duration_cast(end - start).count(); + } + if(elapsed >= timeout) + { + std::cout << PRE << "block timeout: " << std::endl; + return false; + } + + while(len--) + { + int code = read(usart, buffer, 1); + if(code != 1) + { + std::cout << PRE << "read code: " << code << std::endl; + return false; + } + + crc ^= *buffer++; + for (uint8_t i = 0; i < 8; i++) + { + if (crc & 1) + crc ^= CRC7_POLY; + crc >>= 1; + } + } + + crc ^= readByte(); + for (uint8_t i = 0; i < 8; i++) + { + if (crc & 1) + crc ^= CRC7_POLY; + crc >>= 1; + } + + if (crc == 0) + { + writeByte(MSG_OK); + return true; + } + else + { + writeByte(MSG_FAIL); + return false; + } +} + void B15F::delay(uint16_t ms) { std::this_thread::sleep_for(std::chrono::milliseconds(ms)); diff --git a/drv/b15f.h b/drv/b15f.h index c8cf7ca..e88e0a1 100644 --- a/drv/b15f.h +++ b/drv/b15f.h @@ -1,5 +1,5 @@ -#ifndef B15F_h -#define B15F_h +#ifndef B15F_H +#define B15F_H #include #include @@ -44,6 +44,7 @@ public: inline void writeInt(uint16_t); inline uint8_t readByte(void); inline uint16_t readInt(void); + inline bool readBlock(uint8_t* buffer, uint16_t offset); void delay(uint16_t); @@ -52,18 +53,20 @@ public: private: int usart = -1; - uint16_t timeout = 100; // ms + uint16_t timeout = 200; // ms + uint16_t block_timeout = 1; // ms static B15F* instance; // CONSTANTS const std::string PRE = "[B15F] "; const std::string SERIAL_DEVICE = "/dev/ttyUSB0"; - constexpr static uint8_t MSG_OK = 0xFF; - constexpr static uint8_t MSG_FAIL = 0xFE; - constexpr static uint16_t RECONNECT_TIMEOUT = 32; // ms - constexpr static uint8_t RECONNECT_TRIES = 3; + constexpr static uint8_t MSG_OK = 0xFF; + constexpr static uint8_t MSG_FAIL = 0xFE; + constexpr static uint16_t RECONNECT_TIMEOUT = 64; // ms + constexpr static uint8_t RECONNECT_TRIES = 3; constexpr static uint32_t BAUDRATE = 115200; + constexpr static uint8_t CRC7_POLY = 0x91; // REQUESTS constexpr static uint8_t RQ_DISC = 0; @@ -80,4 +83,4 @@ private: constexpr static uint8_t RQ_ADC_DAC_STROKE = 12; }; -#endif // B15F_h +#endif // B15F_H diff --git a/drv/b15f.o b/drv/b15f.o index 1a9b773..b909c96 100644 Binary files a/drv/b15f.o and b/drv/b15f.o differ diff --git a/gnuplotscript.gp b/gnuplotscript.gp index 9a5aaa5..055440a 100644 --- a/gnuplotscript.gp +++ b/gnuplotscript.gp @@ -8,28 +8,29 @@ set xrange [5:0] set x2range [5:0] set y2range [0:50] set yrange [0:50] -set label at 2,38 'U_{DS} [V] = 300' right -set label at 2,29 'U_{DS} [V] = 325' right -set label at 2,31 'U_{DS} [V] = 350' right -set label at 2,32 'U_{DS} [V] = 375' right -set label at 2,33 'U_{DS} [V] = 400' right -set label at 2,20 'U_{DS} [V] = 425' right -set label at 2,20 'U_{DS} [V] = 450' right -set label at 2,11 'U_{DS} [V] = 475' right -set label at 2,21 'U_{DS} [V] = 500' right -set label at 2,21 'U_{DS} [V] = 525' right -set label at 2,11 'U_{DS} [V] = 550' right -set label at 2,11 'U_{DS} [V] = 575' right -set label at 2,11 'U_{DS} [V] = 600' right -set label at 2,11 'U_{DS} [V] = 600' right -set label at 2,11 'U_{DS} [V] = 600' right -set label at 2,11 'U_{DS} [V] = 600' right -set label at 2,11 'U_{DS} [V] = 600' right -set label at 2,11 'U_{DS} [V] = 600' right +set label at 2,32 'U_{DS} [V] = 300' right +set label at 2,30 'U_{DS} [V] = 325' right +set label at 2,33 'U_{DS} [V] = 350' right +set label at 2,21 'U_{DS} [V] = 375' right +set label at 2,21 'U_{DS} [V] = 400' right +set label at 2,5 'U_{DS} [V] = 425' right +set label at 2,22 'U_{DS} [V] = 450' right +set label at 2,12 'U_{DS} [V] = 475' right +set label at 2,23 'U_{DS} [V] = 500' right +set label at 2,23 'U_{DS} [V] = 525' right +set label at 2,2 'U_{DS} [V] = 550' right +set label at 2,12 'U_{DS} [V] = 575' right +set label at 2,13 'U_{DS} [V] = 600' right +set label at 2,5 'U_{DS} [V] = 625' right +set label at 2,6 'U_{DS} [V] = 650' right +set label at 2,6 'U_{DS} [V] = 675' right +set label at 2,2 'U_{DS} [V] = 700' right +set label at 2,2 'U_{DS} [V] = 700' right +set label at 2,2 'U_{DS} [V] = 700' right set y2tics unset ytics set ytics format '' unset output set terminal qt unset output -plot "/tmp/tempfile1" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 300' w l,"/tmp/tempfile2" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 325' w l,"/tmp/tempfile3" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 350' w l,"/tmp/tempfile4" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 375' w l,"/tmp/tempfile5" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 400' w l,"/tmp/tempfile6" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 425' w l,"/tmp/tempfile7" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 450' w l,"/tmp/tempfile8" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 475' w l,"/tmp/tempfile9" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 500' w l,"/tmp/tempfile10" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 525' w l,"/tmp/tempfile11" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 550' w l,"/tmp/tempfile12" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 575' w l,"/tmp/tempfile13" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 600' w l,"/tmp/tempfile14" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 625' w l,"/tmp/tempfile15" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 650' w l,"/tmp/tempfile16" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 675' w l,"/tmp/tempfile17" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 700' w l,"/tmp/tempfile18" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 725' w l +plot "/tmp/tempfile1" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 300' w l,"/tmp/tempfile2" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 325' w l,"/tmp/tempfile3" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 350' w l,"/tmp/tempfile4" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 375' w l,"/tmp/tempfile5" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 400' w l,"/tmp/tempfile6" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 425' w l,"/tmp/tempfile7" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 450' w l,"/tmp/tempfile8" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 475' w l,"/tmp/tempfile9" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 500' w l,"/tmp/tempfile10" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 525' w l,"/tmp/tempfile11" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 550' w l,"/tmp/tempfile12" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 575' w l,"/tmp/tempfile13" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 600' w l,"/tmp/tempfile14" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 625' w l,"/tmp/tempfile15" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 650' w l,"/tmp/tempfile16" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 675' w l,"/tmp/tempfile17" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 700' w l,"/tmp/tempfile18" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 725' w l,"/tmp/tempfile19" using ($1*0.004888):($2*0.048876) binary format="%int16%int16" endian=big title 'U_{DS} [V] = 750' w l diff --git a/main b/main index 6522fb2..ba3e7f8 100755 Binary files a/main and b/main differ diff --git a/test_plot b/test_plot index afde276..2e5d10e 100644 Binary files a/test_plot and b/test_plot differ