-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from eProsima/feature/baudrate
Improved baud rate support [4465]
- Loading branch information
Showing
5 changed files
with
301 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
include/uxr/agent/transport/serial/baud_rate_table_linux.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef UXR_AGENT_TRANSPORT_SERIAL_BAUD_RATE_TABLE_H | ||
#define UXR_AGENT_TRANSPORT_SERIAL_BAUD_RATE_TABLE_H | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <termios.h> | ||
|
||
speed_t getBaudRate(const char* baudrate_str) | ||
{ | ||
speed_t rv; | ||
if (0 == strcmp(baudrate_str, "0")) | ||
{ | ||
rv = B0; | ||
} | ||
else if (0 == strcmp(baudrate_str, "50")) | ||
{ | ||
rv = B50; | ||
} | ||
else if (0 == strcmp(baudrate_str, "75")) | ||
{ | ||
rv = B75; | ||
} | ||
else if (0 == strcmp(baudrate_str, "110")) | ||
{ | ||
rv = B110; | ||
} | ||
else if (0 == strcmp(baudrate_str, "134")) | ||
{ | ||
rv = B134; | ||
} | ||
else if (0 == strcmp(baudrate_str, "150")) | ||
{ | ||
rv = B150; | ||
} | ||
else if (0 == strcmp(baudrate_str, "200")) | ||
{ | ||
rv = B200; | ||
} | ||
else if (0 == strcmp(baudrate_str, "300")) | ||
{ | ||
rv = B300; | ||
} | ||
else if (0 == strcmp(baudrate_str, "600")) | ||
{ | ||
rv = B600; | ||
} | ||
else if (0 == strcmp(baudrate_str, "1200")) | ||
{ | ||
rv = B1200; | ||
} | ||
else if (0 == strcmp(baudrate_str, "1800")) | ||
{ | ||
rv = B1800; | ||
} | ||
else if (0 == strcmp(baudrate_str, "2400")) | ||
{ | ||
rv = B2400; | ||
} | ||
else if (0 == strcmp(baudrate_str, "4800")) | ||
{ | ||
rv = B4800; | ||
} | ||
else if (0 == strcmp(baudrate_str, "9600")) | ||
{ | ||
rv = B9600; | ||
} | ||
else if (0 == strcmp(baudrate_str, "19200")) | ||
{ | ||
rv = B19200; | ||
} | ||
else if (0 == strcmp(baudrate_str, "38400")) | ||
{ | ||
rv = B38400; | ||
} | ||
else if (0 == strcmp(baudrate_str, "57600")) | ||
{ | ||
rv = B57600; | ||
} | ||
else if (0 == strcmp(baudrate_str, "115200")) | ||
{ | ||
rv = B115200; | ||
} | ||
else if (0 == strcmp(baudrate_str, "230400")) | ||
{ | ||
rv = B230400; | ||
} | ||
else if (0 == strcmp(baudrate_str, "460800")) | ||
{ | ||
rv = B460800; | ||
} | ||
else if (0 == strcmp(baudrate_str, "500000")) | ||
{ | ||
rv = B500000; | ||
} | ||
else if (0 == strcmp(baudrate_str, "576000")) | ||
{ | ||
rv = B576000; | ||
} | ||
else if (0 == strcmp(baudrate_str, "921600")) | ||
{ | ||
rv = B921600; | ||
} | ||
else if (0 == strcmp(baudrate_str, "1000000")) | ||
{ | ||
rv = B1000000; | ||
} | ||
else if (0 == strcmp(baudrate_str, "1152000")) | ||
{ | ||
rv = B1152000; | ||
} | ||
else if (0 == strcmp(baudrate_str, "1500000")) | ||
{ | ||
rv = B1500000; | ||
} | ||
else if (0 == strcmp(baudrate_str, "2000000")) | ||
{ | ||
rv = B2000000; | ||
} | ||
else if (0 == strcmp(baudrate_str, "2500000")) | ||
{ | ||
rv = B2500000; | ||
} | ||
else if (0 == strcmp(baudrate_str, "3000000")) | ||
{ | ||
rv = B3000000; | ||
} | ||
else if (0 == strcmp(baudrate_str, "3500000")) | ||
{ | ||
rv = B3500000; | ||
} | ||
else if (0 == strcmp(baudrate_str, "4000000")) | ||
{ | ||
rv = B4000000; | ||
} | ||
else | ||
{ | ||
speed_t custom_baud_rate = (speed_t)atoi(baudrate_str); | ||
printf("Warning: Custom baud rate set to: %d\n", custom_baud_rate); | ||
rv = custom_baud_rate; | ||
} | ||
return rv; | ||
} | ||
|
||
#endif // UXR_AGENT_TRANSPORT_SERIAL_BAUD_RATE_TABLE_H | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2018 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <uxr/agent/transport/serial/baud_rate_table_linux.h> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace eprosima { | ||
namespace uxr { | ||
namespace testing { | ||
|
||
|
||
class BaudRateTableUnitTests : public ::testing::Test | ||
{ | ||
public: | ||
BaudRateTableUnitTests() : table{} | ||
{ | ||
table.push_back(std::make_tuple("0", B0)); | ||
table.push_back(std::make_tuple("50", B50)); | ||
table.push_back(std::make_tuple("75", B75)); | ||
table.push_back(std::make_tuple("110", B110)); | ||
table.push_back(std::make_tuple("134", B134)); | ||
table.push_back(std::make_tuple("150", B150)); | ||
table.push_back(std::make_tuple("200", B200)); | ||
table.push_back(std::make_tuple("300", B300)); | ||
table.push_back(std::make_tuple("600", B600)); | ||
table.push_back(std::make_tuple("1200", B1200)); | ||
table.push_back(std::make_tuple("1800", B1800)); | ||
table.push_back(std::make_tuple("2400", B2400)); | ||
table.push_back(std::make_tuple("4800", B4800)); | ||
table.push_back(std::make_tuple("9600", B9600)); | ||
table.push_back(std::make_tuple("19200", B19200)); | ||
table.push_back(std::make_tuple("38400", B38400)); | ||
table.push_back(std::make_tuple("57600", B57600)); | ||
table.push_back(std::make_tuple("115200", B115200)); | ||
table.push_back(std::make_tuple("230400", B230400)); | ||
table.push_back(std::make_tuple("460800", B460800)); | ||
table.push_back(std::make_tuple("500000", B500000)); | ||
table.push_back(std::make_tuple("576000", B576000)); | ||
table.push_back(std::make_tuple("921600", B921600)); | ||
table.push_back(std::make_tuple("1000000", B1000000)); | ||
table.push_back(std::make_tuple("1152000", B1152000)); | ||
table.push_back(std::make_tuple("1500000", B1500000)); | ||
table.push_back(std::make_tuple("2000000", B2000000)); | ||
table.push_back(std::make_tuple("2500000", B2500000)); | ||
table.push_back(std::make_tuple("3000000", B3000000)); | ||
table.push_back(std::make_tuple("3500000", B3500000)); | ||
table.push_back(std::make_tuple("4000000", B4000000)); | ||
table.push_back(std::make_tuple("11", speed_t(11))); | ||
} | ||
|
||
virtual ~BaudRateTableUnitTests() = default; | ||
|
||
public: | ||
std::vector<std::tuple<const char*, speed_t>> table; | ||
}; | ||
|
||
TEST_F(BaudRateTableUnitTests, CreateClientOk) | ||
{ | ||
for (auto& b : table) | ||
{ | ||
ASSERT_EQ(getBaudRate(std::get<0>(b)), std::get<1>(b)); | ||
} | ||
} | ||
|
||
} // namespace testing | ||
} // namespace uxr | ||
} // namespace eprosima | ||
|
||
int main(int args, char** argv) | ||
{ | ||
::testing::InitGoogleTest(&args, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
Oops, something went wrong.