LaunchAPPL/Serial: resend reset after timeout

This commit is contained in:
Wolfgang Thaller 2018-05-05 18:41:38 +02:00
parent fc87b33fb4
commit ca17e9ff0c

View File

@ -8,10 +8,11 @@
#include <fcntl.h> #include <fcntl.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <poll.h> #include <poll.h>
#include <chrono>
#include <iostream> #include <iostream>
namespace po = boost::program_options;
namespace po = boost::program_options;
using namespace std::literals::chrono_literals;
class SerialStream : public Stream class SerialStream : public Stream
{ {
@ -19,6 +20,7 @@ class SerialStream : public Stream
uint8_t readBuffer[kReadBufferSize]; uint8_t readBuffer[kReadBufferSize];
public: public:
int fd; int fd;
int baud;
virtual void write(const void* p, size_t n) override; virtual void write(const void* p, size_t n) override;
@ -49,7 +51,7 @@ private:
SerialStream::SerialStream(po::variables_map &options) SerialStream::SerialStream(po::variables_map &options)
{ {
std::string port = options["serial-port"].as<std::string>(); std::string port = options["serial-port"].as<std::string>();
int baud = options["serial-baud"].as<int>(); baud = options["serial-baud"].as<int>();
fd = open(port.c_str(), O_RDWR | O_NOCTTY | O_NDELAY ); fd = open(port.c_str(), O_RDWR | O_NOCTTY | O_NDELAY );
if(fd < 0) if(fd < 0)
throw std::runtime_error("Cannot open serial port.\n"); throw std::runtime_error("Cannot open serial port.\n");
@ -154,17 +156,21 @@ void SerialLauncher::write(const void *p, size_t n)
rStream.write(p, n); rStream.write(p, n);
} }
bool SerialLauncher::Go(int timeout) bool SerialLauncher::Go(int timeout)
{ {
uint32_t tmp; uint32_t tmp;
do
{
rStream.reset(1); rStream.reset(1);
std::cout << "reset send.\n"; std::cerr << "Connecting... (" << stream.baud << " baud)" << std::endl;
while(!rStream.resetResponseArrived()) using clock = std::chrono::steady_clock;
auto startTime = clock::now();
while(!rStream.resetResponseArrived() && clock::now() - startTime < 5s)
stream.wait(); stream.wait();
} while(!rStream.resetResponseArrived());
std::cout << "reset response received.\n"; std::cerr << "Connected." << std::endl;
{ {
std::ostringstream rsrcOut; std::ostringstream rsrcOut;
@ -182,9 +188,10 @@ bool SerialLauncher::Go(int timeout)
} }
while(!rStream.allDataArrived()) while(!rStream.allDataArrived())
stream.wait(); stream.wait();
std::cerr << "Running Appliation..." << std::endl;
read(&tmp, 4); read(&tmp, 4);
uint32_t result = ntohl(tmp); uint32_t result = ntohl(tmp);
std::cerr << "Finished." << std::endl;
if(result == 0) if(result == 0)
{ {