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