Changed the default baudrate handling around a little to make it cleaner

This commit is contained in:
Eric Katzfey 2023-07-19 17:21:59 -07:00
parent 83f3bc4dff
commit 14a3df8b60
3 changed files with 29 additions and 20 deletions

View File

@ -7,14 +7,10 @@ Serial::Serial(const char *port, uint32_t baudrate, ByteSize bytesize, Parity pa
FlowControl flowcontrol) : FlowControl flowcontrol) :
_impl(port, baudrate, bytesize, parity, stopbits, flowcontrol) _impl(port, baudrate, bytesize, parity, stopbits, flowcontrol)
{ {
// If no baudrate was specified then set it to a reasonable default value
if (baudrate == 0) {
// TODO: Device (void) _impl.setBaudrate(9600);
}
// set_device_bus_type(device::Device::DeviceBusType::DeviceBusType_SERIAL);
// char c = _port[strlen(_port) - 1]; // last digit of path (eg /dev/ttyS2)
// set_device_bus(c - 48); // sub 48 to convert char to integer
} }
Serial::~Serial() Serial::~Serial()

View File

@ -23,10 +23,6 @@ SerialImpl::SerialImpl(const char *port, uint32_t baudrate, ByteSize bytesize, P
} else { } else {
_port[0] = 0; _port[0] = 0;
} }
// Start off with a valid bitrate to make sure open can succeed
if (_baudrate == 0) { _baudrate = 9600; }
} }
SerialImpl::~SerialImpl() SerialImpl::~SerialImpl()

View File

@ -472,12 +472,12 @@ int GPS::pollOrRead(uint8_t *buf, size_t buf_length, int timeout)
int ret = 0; int ret = 0;
const unsigned character_count = 32; // minimum bytes that we want to read const unsigned character_count = 32; // minimum bytes that we want to read
const int max_timeout = 50; const int max_timeout = 50;
int _timeout = math::min(max_timeout, timeout); int timeout_adjusted = math::min(max_timeout, timeout);
handleInjectDataTopic(); handleInjectDataTopic();
if ((_interface == GPSHelper::Interface::UART) && (_uart)) { if ((_interface == GPSHelper::Interface::UART) && (_uart)) {
ret = _uart->readAtLeast(buf, buf_length, character_count, _timeout); ret = _uart->readAtLeast(buf, buf_length, character_count, timeout_adjusted);
// SPI is only supported on LInux // SPI is only supported on LInux
#if defined(__PX4_LINUX) #if defined(__PX4_LINUX)
@ -495,7 +495,7 @@ int GPS::pollOrRead(uint8_t *buf, size_t buf_length, int timeout)
fds[0].fd = _spi_fd; fds[0].fd = _spi_fd;
fds[0].events = POLLIN; fds[0].events = POLLIN;
ret = poll(fds, sizeof(fds) / sizeof(fds[0]), _timeout); ret = poll(fds, sizeof(fds) / sizeof(fds[0]), timeout_adjusted);
if (ret > 0) { if (ret > 0) {
/* if we have new data from GPS, go handle it */ /* if we have new data from GPS, go handle it */
@ -614,8 +614,8 @@ bool GPS::injectData(uint8_t *data, size_t len)
int GPS::setBaudrate(unsigned baud) int GPS::setBaudrate(unsigned baud)
{ {
if (_interface == GPSHelper::Interface::UART) { if (_interface == GPSHelper::Interface::UART) {
if (_uart) { if ((_uart) && (_uart->setBaudrate(baud))) {
return _uart->setBaudrate(baud); return 0;
} }
#ifdef __PX4_LINUX #ifdef __PX4_LINUX
@ -786,7 +786,9 @@ GPS::run()
} }
if ((_interface == GPSHelper::Interface::UART) && (_uart == nullptr)) { if ((_interface == GPSHelper::Interface::UART) && (_uart == nullptr)) {
_uart = new Serial(_port, _configured_baudrate);
// Create the UART port instance
_uart = new Serial(_port);
if (_uart == nullptr) { if (_uart == nullptr) {
PX4_ERR("Error creating serial device %s", _port); PX4_ERR("Error creating serial device %s", _port);
@ -794,7 +796,22 @@ GPS::run()
continue; continue;
} }
_uart->open(); // Configure the desired baudrate if one was specified by the user.
// Otherwise the default baudrate will be used.
if (_configured_baudrate) {
if (! _uart->setBaudrate(_configured_baudrate)) {
PX4_ERR("Error setting baudrate to %u on %s", _configured_baudrate, _port);
px4_sleep(1);
continue;
}
}
// Open the UART. If this is successful then the UART is ready to use.
if (! _uart->open()) {
PX4_ERR("Error opening serial device %s", _port);
px4_sleep(1);
continue;
}
#ifdef __PX4_LINUX #ifdef __PX4_LINUX
@ -1010,7 +1027,7 @@ GPS::run()
} }
if ((_interface == GPSHelper::Interface::UART) && (_uart)) { if ((_interface == GPSHelper::Interface::UART) && (_uart)) {
_uart->close(); (void) _uart->close();
delete _uart; delete _uart;
#ifdef __PX4_LINUX #ifdef __PX4_LINUX