bereite vereinigung vor

This commit is contained in:
Tristan Krause 2019-04-01 15:15:46 +02:00
parent f4ca17346c
commit cc1e5e927c
6 changed files with 44 additions and 49 deletions

View file

@ -83,13 +83,13 @@ void B15F::reconnect()
void B15F::discard(void) void B15F::discard(void)
{ {
usart.clearOutputBuffer(); usart.flushOutputBuffer();
for(uint8_t i = 0; i < 8; i++) for(uint8_t i = 0; i < 8; i++)
{ {
usart.writeByte(RQ_DISC); // sende discard Befehl (verwerfe input) usart.writeByte(RQ_DISC); // sende discard Befehl (verwerfe input)
delay_ms((16000 / BAUDRATE) + 1); // warte mindestens eine Millisekunde, gegebenenfalls mehr delay_ms((16000 / BAUDRATE) + 1); // warte mindestens eine Millisekunde, gegebenenfalls mehr
} }
usart.clearInputBuffer(); usart.flushInputBuffer();
} }
bool B15F::testConnection() bool B15F::testConnection()
@ -147,7 +147,7 @@ std::vector<std::string> B15F::getBoardInfo(void)
bool B15F::digitalWrite0(uint8_t port) bool B15F::digitalWrite0(uint8_t port)
{ {
usart.clearInputBuffer(); usart.flushInputBuffer();
usart.writeByte(RQ_BA0); usart.writeByte(RQ_BA0);
usart.writeByte(port); usart.writeByte(port);
@ -157,7 +157,7 @@ bool B15F::digitalWrite0(uint8_t port)
bool B15F::digitalWrite1(uint8_t port) bool B15F::digitalWrite1(uint8_t port)
{ {
usart.clearInputBuffer(); usart.flushInputBuffer();
usart.writeByte(RQ_BA1); usart.writeByte(RQ_BA1);
usart.writeByte(port); usart.writeByte(port);
@ -167,21 +167,21 @@ bool B15F::digitalWrite1(uint8_t port)
uint8_t B15F::digitalRead0() uint8_t B15F::digitalRead0()
{ {
usart.clearInputBuffer(); usart.flushInputBuffer();
usart.writeByte(RQ_BE0); usart.writeByte(RQ_BE0);
return usart.readByte(); return usart.readByte();
} }
uint8_t B15F::digitalRead1() uint8_t B15F::digitalRead1()
{ {
usart.clearInputBuffer(); usart.flushInputBuffer();
usart.writeByte(RQ_BE1); usart.writeByte(RQ_BE1);
return usart.readByte(); return usart.readByte();
} }
bool B15F::analogWrite0(uint16_t value) bool B15F::analogWrite0(uint16_t value)
{ {
usart.clearInputBuffer(); usart.flushInputBuffer();
usart.writeByte(RQ_AA0); usart.writeByte(RQ_AA0);
usart.writeInt(value); usart.writeInt(value);
@ -191,7 +191,7 @@ bool B15F::analogWrite0(uint16_t value)
bool B15F::analogWrite1(uint16_t value) bool B15F::analogWrite1(uint16_t value)
{ {
usart.clearInputBuffer(); usart.flushInputBuffer();
usart.writeByte(RQ_AA1); usart.writeByte(RQ_AA1);
usart.writeInt(value); usart.writeInt(value);
@ -201,7 +201,7 @@ bool B15F::analogWrite1(uint16_t value)
uint16_t B15F::analogRead(uint8_t channel) uint16_t B15F::analogRead(uint8_t channel)
{ {
usart.clearInputBuffer(); usart.flushInputBuffer();
usart.writeByte(RQ_ADC); usart.writeByte(RQ_ADC);
usart.writeByte(channel); usart.writeByte(channel);
return usart.readInt(); return usart.readInt();
@ -212,7 +212,7 @@ bool B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset
buffer_a += offset_a; buffer_a += offset_a;
buffer_b += offset_b; buffer_b += offset_b;
usart.clearInputBuffer(); usart.flushInputBuffer();
usart.writeByte(RQ_ADC_DAC_STROKE); usart.writeByte(RQ_ADC_DAC_STROKE);
usart.writeByte(channel_a); usart.writeByte(channel_a);
usart.writeByte(channel_b); usart.writeByte(channel_b);

Binary file not shown.

View file

@ -24,8 +24,8 @@ void USART::openDevice(std::string device)
if(code) if(code)
throw USARTException("Fehler beim Setzen der Geräteparameter"); throw USARTException("Fehler beim Setzen der Geräteparameter");
clearOutputBuffer(); flushOutputBuffer();
clearInputBuffer(); flushInputBuffer();
} }
void USART::closeDevice() void USART::closeDevice()
@ -35,14 +35,14 @@ void USART::closeDevice()
throw USARTException("Fehler beim Schließen des Gerätes"); throw USARTException("Fehler beim Schließen des Gerätes");
} }
void USART::clearInputBuffer() void USART::flushInputBuffer()
{ {
int code = tcflush(file_desc, TCIFLUSH); int code = tcflush(file_desc, TCIFLUSH);
if(code) if(code)
throw USARTException("Fehler beim Leeren des Eingangspuffers"); throw USARTException("Fehler beim Leeren des Eingangspuffers");
} }
void USART::clearOutputBuffer() void USART::flushOutputBuffer()
{ {
int code = tcflush(file_desc, TCOFLUSH); int code = tcflush(file_desc, TCOFLUSH);
if(code) if(code)
@ -112,31 +112,17 @@ int USART::write_timeout(uint8_t* buffer, uint16_t offset, uint8_t len, uint32_t
void USART::writeBlock(uint8_t* buffer, uint16_t offset, uint8_t len) void USART::writeBlock(uint8_t* buffer, uint16_t offset, uint8_t len)
{ {
buffer += offset;
uint8_t crc; uint8_t crc;
uint8_t aw; uint8_t aw;
uint16_t us_per_bit = (1000000 / baudrate) * 16; const uint16_t us_per_bit = (1000000 / baudrate) * 16;
uint8_t block_end = BLOCK_END; const uint16_t n_total = len + 3;
do do
{ {
crc = 0;
// send block length
int n_sent = write_timeout(&len, 0, 1, us_per_bit);
if(n_sent != 1)
throw std::runtime_error("fatal (send): " + std::to_string(n_sent));
// send block
n_sent = write_timeout(buffer, 0, len, us_per_bit * len);
if(n_sent != len)
throw std::runtime_error("fatal (send #2): " + std::to_string(n_sent));
// calc crc // calc crc
crc = 0;
for(uint8_t i = 0; i < len; i++) for(uint8_t i = 0; i < len; i++)
{ {
usleep(us_per_bit);
crc ^= buffer[i]; crc ^= buffer[i];
for (uint8_t k = 0; k < 8; k++) for (uint8_t k = 0; k < 8; k++)
{ {
@ -145,22 +131,30 @@ void USART::writeBlock(uint8_t* buffer, uint16_t offset, uint8_t len)
crc >>= 1; crc >>= 1;
} }
} }
// construct block
block_buffer[0] = len;
std::memcpy(&block_buffer[1], buffer + offset, len);
block_buffer[len + 1] = crc;
block_buffer[len + 2] = BLOCK_END;
n_sent = write_timeout(&crc, 0, 1, us_per_bit); // send block
if(n_sent != 1) int n_sent = write_timeout(&block_buffer[0], 0, len + 3, us_per_bit * n_total);
if(n_sent != n_total)
throw std::runtime_error("fatal (send): " + std::to_string(n_sent)); throw std::runtime_error("fatal (send): " + std::to_string(n_sent));
usleep(us_per_bit); flushOutputBuffer();
n_sent = write_timeout(&block_end, 0, 1, us_per_bit);
if(n_sent != 1)
throw std::runtime_error("fatal (send): " + std::to_string(n_sent));
usleep(us_per_bit);
usleep(1000);
// check response
int n_read = read_timeout(&aw, 0, 1, us_per_bit); int n_read = read_timeout(&aw, 0, 1, us_per_bit);
if(n_read < 0) for(uint8_t i = 0; i < 10 && n_read != 1; i++)
{ {
std::cout << "WARNING: read error, retry..." << std::endl; flushOutputBuffer();
n_read = read_timeout(&aw, 0, 1, us_per_bit); flushInputBuffer();
std::cout << "WARNING: read error (" << n_read << "), retry #" << (int) i << std::endl;
usleep(1000000);
n_read = read_timeout(&aw, 0, 1, us_per_bit * 2);
} }
if(n_read == 0) if(n_read == 0)
@ -178,11 +172,11 @@ void USART::writeBlock(uint8_t* buffer, uint16_t offset, uint8_t len)
else if(n_read != 1) else if(n_read != 1)
throw std::runtime_error("fatal: " + std::to_string(n_read)); throw std::runtime_error("fatal: " + std::to_string(n_read));
clearInputBuffer(); flushInputBuffer();
} }
while(aw != 0xFF); while(aw != 0xFF);
std::cout << "OK" << std::endl; //std::cout << "OK" << std::endl;
} }
uint8_t USART::readByte(void) uint8_t USART::readByte(void)

View file

@ -5,6 +5,7 @@
#include <cstdint> #include <cstdint>
#include <chrono> #include <chrono>
#include <unistd.h> #include <unistd.h>
#include <cstring>
#include <fcntl.h> #include <fcntl.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <termios.h> #include <termios.h>
@ -36,13 +37,13 @@ public:
* Verwirft Daten, die bereits im Puffer liegen, aber noch nicht gelesen wurden * Verwirft Daten, die bereits im Puffer liegen, aber noch nicht gelesen wurden
* \throws USARTException * \throws USARTException
*/ */
void clearInputBuffer(void); void flushInputBuffer(void);
/** /**
* Verwirft Daten, die bereits im Puffer liegen, aber noch nicht gesendet wurden * Verwirft Daten, die bereits im Puffer liegen, aber noch nicht gesendet wurden
* \throws USARTException * \throws USARTException
*/ */
void clearOutputBuffer(void); void flushOutputBuffer(void);
/*************************************************/ /*************************************************/
@ -126,7 +127,7 @@ private:
uint32_t baudrate = 9600; uint32_t baudrate = 9600;
int TEST = 0; int TEST = 0;
uint8_t timeout = 10; // in Dezisekunden uint8_t timeout = 10; // in Dezisekunden
uint8_t buffer[MAX_BLOCK_SIZE + 3]; uint8_t block_buffer[MAX_BLOCK_SIZE + 3];
}; };

Binary file not shown.

BIN
main

Binary file not shown.