make serial handling more robust to hw errors
This commit is contained in:
parent
2ec88114c6
commit
229932b274
51
main.cpp
51
main.cpp
@ -77,7 +77,6 @@ static int parseCmdArgs(int argc, char** argv, Config *config)
|
||||
{
|
||||
config->noSerial=true;
|
||||
}
|
||||
|
||||
else if (std::string(argv[i]) == "-r" || std::string(argv[i]) == "--rates")
|
||||
{
|
||||
printRates();
|
||||
@ -135,12 +134,32 @@ int openSerialPort(const Config& config)
|
||||
return serial;
|
||||
}
|
||||
|
||||
void serialConnect(const Config& config, int* serial)
|
||||
int serialPortReconnect(Config& config, std::string err)
|
||||
{
|
||||
if(*serial != -1)
|
||||
close(*serial);
|
||||
std::cout<<"Using serial port: "<<config.portFileName<<" at "<<config.baud<<" baud\n";
|
||||
*serial = serialport_init(config.portFileName.c_str(), config.baud);
|
||||
std::cout<<"serial port execption "<<err<<"\ntrying to reopen\n";
|
||||
|
||||
sleep(10);
|
||||
int serial = openSerialPort(config);
|
||||
if(serial == -1)
|
||||
{
|
||||
|
||||
if(config.portFileName == "/dev/ttyUSB0")
|
||||
{
|
||||
std::cout<<"Could not reopen "<<config.portFileName<<" trying /dev/ttyUSB1\n";
|
||||
config.portFileName = "/dev/ttyUSB1";
|
||||
serial = openSerialPort(config);
|
||||
}
|
||||
else if(config.portFileName == "/dev/ttyUSB1")
|
||||
{
|
||||
std::cout<<"Could not reopen "<<config.portFileName<<" trying /dev/ttyUSB0\n";
|
||||
config.portFileName = "/dev/ttyUSB0";
|
||||
serial = openSerialPort(config);
|
||||
}
|
||||
|
||||
if(serial == -1)
|
||||
std::cout<<"Could not reopen "<<config.portFileName<<'\n';
|
||||
}
|
||||
return serial;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
@ -160,8 +179,8 @@ int main(int argc, char* argv[])
|
||||
int serial = openSerialPort(config);
|
||||
if(!config.noSerial)
|
||||
{
|
||||
serialConnect(config, &serial);
|
||||
if(serial == -1) return 1;
|
||||
if(serial == -1)
|
||||
return 1;
|
||||
else
|
||||
{
|
||||
struct epoll_event ev = {};
|
||||
@ -212,7 +231,17 @@ int main(int argc, char* argv[])
|
||||
clientsMutex.lock();
|
||||
std::cout<<"client poll\n";
|
||||
if(ev.events & EPOLLIN)
|
||||
client->run(&clients, serial, config.verbose);
|
||||
{
|
||||
try
|
||||
{
|
||||
client->run(&clients, serial, config.verbose);
|
||||
}
|
||||
catch(serialIoException& ex)
|
||||
{
|
||||
close(serial);
|
||||
serialPortReconnect(config, ex.what());
|
||||
}
|
||||
}
|
||||
if((ev.events & (EPOLLHUP | EPOLLERR)) || client->isDisconnected())
|
||||
{
|
||||
client->cleanUp();
|
||||
@ -227,8 +256,8 @@ int main(int argc, char* argv[])
|
||||
ssize_t readlen = sRead(serial, buffer, 4096);
|
||||
if(readlen < 0 && (errno != EAGAIN || errno != EWOULDBLOCK))
|
||||
{
|
||||
std::cout<<"Serial port error reconnecting\n";
|
||||
serialConnect(config, &serial);
|
||||
close(serial);
|
||||
serialPortReconnect(config, strerror(errno));
|
||||
}
|
||||
if(config.verbose)
|
||||
{
|
||||
|
@ -7,6 +7,7 @@
|
||||
#ifdef __cplusplus
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#define BAUDRATE B38400
|
||||
@ -32,7 +33,7 @@ class serialIoException: public std::runtime_error
|
||||
int fd;
|
||||
int errorNumber;
|
||||
serialIoException(int fd_, int errorNumber_):
|
||||
std::runtime_error("file descriptor error, fd: " + std::to_string(fd_) + " errno: " + std::to_string(errorNumber_) + "\n"), fd(fd_), errorNumber(errorNumber_)
|
||||
std::runtime_error("file descriptor error, fd: " + std::to_string(fd_) + " error: " + strerror(errorNumber_) + "\n"), fd(fd_), errorNumber(errorNumber_)
|
||||
{}
|
||||
};
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user