bool Volkslogger::SendCommand(Port &port, OperationEnvironment &env, Command cmd, uint8_t param1, uint8_t param2) { static constexpr unsigned delay = 2; /* flush buffers */ if (!port.FullFlush(env, 20, 100)) return false; /* reset command interpreter */ if (!Reset(port, env, 6)) return false; /* send command packet */ const uint8_t cmdarray[8] = { (uint8_t)cmd, param1, param2, 0, 0, 0, 0, 0, }; if (!port.Write(ENQ)) return false; env.Sleep(delay); if (!SendWithCRC(port, cmdarray, sizeof(cmdarray), env)) return false; /* wait for confirmation */ return port.WaitRead(env, 4000) == Port::WaitResult::READY && port.GetChar() == 0; }
static bool TryConnect(Port &port, char *user_data) { int retries=10; while (--retries){ port.Write('\x02'); // send IO Mode command unsigned user_size = 0; bool started = false; PeriodClock clock; clock.update(); int i; while ((i = port.GetChar()) != EOF && !clock.check(8000)) { char ch = (char)i; if (!started && ch == '-') started = true; if (started) { if (ch == 0x13) { port.Write('\x16'); user_data[user_size] = 0; // found end of file return true; } else { if (user_size < sizeof(user_data) - 1) { user_data[user_size] = ch; user_size++; } } } } } return false; }
bool EWMicroRecorderDevice::TryConnect() { int retries=10; while (--retries){ port->Write('\x02'); // send IO Mode command unsigned user_size = 0; bool started = false; int i; while ((i = port->GetChar()) != _TEOF) { char ch = (char)i; if (!started) { if (ch == _T('-')) { started = true; } } if (started) { if (ch == 0x13) { port->Write('\x16'); user_data[user_size] = 0; // found end of file return true; } else { if (user_size < sizeof(user_data) - 1) { user_data[user_size] = ch; user_size++; } } } } } return false; }
bool AltairProDevice::PropertySetGet(Port *port, char *Buffer, size_t size) { assert(port != NULL); assert(Buffer != NULL); // eg $PDVSC,S,FOO,BAR*<cr>\r\n PortWriteNMEA(port, Buffer); Buffer[6] = _T('A'); char *comma = strchr(&Buffer[8], ','); if (comma != NULL){ comma[1] = '\0'; // expect eg $PDVSC,A,FOO, if (port->ExpectString(Buffer)){ // read value eg bar while(--size){ int ch = port->GetChar(); if (ch == -1) break; if (ch == '*'){ Buffer = '\0'; return true; } *Buffer++ = (char) ch; } } } *Buffer = '\0'; return false; }
static bool TryConnect(Port &port, char *user_data, size_t max_user_data, OperationEnvironment &env) { port.Write('\x02'); // send IO Mode command unsigned user_size = 0; bool started = false; PeriodClock clock; clock.Update(); int i; while ((i = port.GetChar()) != EOF && !clock.Check(8000) && !env.IsCancelled()) { char ch = (char)i; if (!started && ch == '-') started = true; if (started) { if (ch == 0x13) { port.Write('\x16'); user_data[user_size] = 0; // found end of file return true; } else { if (user_size < max_user_data - 1) { user_data[user_size] = ch; user_size++; } } } } return false; }
static bool ReceiveLine(Port &port, char *buffer, size_t length, unsigned timeout_ms) { TimeoutClock timeout(timeout_ms); char *p = (char *)buffer, *end = p + length; while (p < end) { if (timeout.HasExpired()) return false; // Read single character from port int c = port.GetChar(); // On failure try again until timed out if (c == -1) continue; // Break on XOn if (c == 0x11) { *p = '\0'; break; } // Write received character to buffer *p = c; p++; // Break on line break if (c == '\n') { *p = '\0'; break; } } return true; }
int Volkslogger::ReadBulk(Port &port, OperationEnvironment &env, void *buffer, size_t max_length, unsigned timeout_firstchar_ms) { unsigned nbytes = 0; bool dle_r = false; uint16_t crc16 = 0; bool start = false, ende = false; memset(buffer, 0xff, max_length); uint8_t *p = (uint8_t *)buffer; constexpr unsigned TIMEOUT_NORMAL_MS = 2000; /** * We need to wait longer for the first char to * give the logger time to calculate security * when downloading a log-file. * Therefore timeout_firstchar is configurable. * If the timeout parameter is not specified or 0, * set standard timeout */ if (timeout_firstchar_ms == 0) timeout_firstchar_ms = TIMEOUT_NORMAL_MS; while (!ende) { // Zeichen anfordern und darauf warten if (!port.Write(ACK)) return -1; // Set longer timeout on first char unsigned timeout = start ? TIMEOUT_NORMAL_MS : timeout_firstchar_ms; if (port.WaitRead(env, timeout) != Port::WaitResult::READY) return -1; int ch = port.GetChar(); if (ch < 0) return -1; // dabei ist Benutzerabbruch jederzeit möglich if (env.IsCancelled()) { env.Sleep(10); port.Write(CAN); port.Write(CAN); port.Write(CAN); return -1; } // oder aber das empfangene Zeichen wird ausgewertet switch (ch) { case DLE: if (!dle_r) { //!DLE, DLE -> Achtung! dle_r = true; } else { // DLE, DLE -> DLE-Zeichen dle_r = false; if (start) { if(nbytes < max_length) *p++ = ch; nbytes++; crc16 = UpdateCRC16CCITT(ch, crc16); } } break; case ETX: if (!dle_r) { //!DLE, ETX -> Zeichen if (start) { if(nbytes < max_length) { *p++ = ch; } nbytes++; crc16 = UpdateCRC16CCITT(ch, crc16); }; } else { if (start) { ende = true; // DLE, ETX -> Blockende dle_r = false; } } break; case STX: if (!dle_r) { //!DLE, STX -> Zeichen if (start) { if(nbytes < max_length) *p++ = ch; nbytes++; crc16 = UpdateCRC16CCITT(ch, crc16); } } else { start = true; // DLE, STX -> Blockstart dle_r = false; crc16 = 0; } break; default: if (start) { if(nbytes < max_length) *p++ = ch; nbytes++; crc16 = UpdateCRC16CCITT(ch, crc16); } break; } } env.Sleep(100); if (crc16 != 0) return -1; if (nbytes < 2) return 0; // CRC am Ende abschneiden return nbytes - 2; }