Example: comms_serial_port_example

C++ example source code:

/*                    _
                     | |    Mobile Robot Programming Toolkit (MRPT)
 _ __ ___  _ __ _ __ | |_
| '_ ` _ \| '__| '_ \| __|          https://www.mrpt.org/
| | | | | | |  | |_) | |_
|_| |_| |_|_|  | .__/ \__|     https://github.com/MRPT/mrpt/
               | |
               |_|

 Copyright (c) 2005-2025, Individual contributors, see AUTHORS file
 See: https://www.mrpt.org/Authors - All rights reserved.
 SPDX-License-Identifier: BSD-3-Clause
*/

#include <mrpt/comms/CSerialPort.h>

#include <iostream>
#include <memory>

using namespace mrpt::comms;
using namespace std;

int main()
{
  try
  {
    std::unique_ptr<CSerialPort> serPort;

    string serName;

    cout << "Serial port test application: Use it with a loopback serial "
            "port (pins 2-3 connected)"
         << endl;

    cout << "Enter the serial port name (e.g. COM1, ttyS0, ttyUSB0): ";
    getline(cin, serName);

    cout << endl;
    cout << "Opening serial port...";
    serPort = std::make_unique<CSerialPort>(serName);
    cout << "OK" << endl;

    cout << "Setting timeouts...";
    serPort->setTimeouts(100, 1, 100, 1, 100);
    cout << "OK" << endl;

    cout << "Setting baud rate...";
    serPort->setConfig(500000);
    cout << "OK" << endl;

    for (int i = 0; i < 10; i++)
    {
      // Test write:
      cout << "Writing test data...";
      const char buf1[] = "Hello world!";
      size_t written = serPort->Write(buf1, sizeof(buf1));
      cout << written << " bytes written." << endl;

      // Read:
      cout << "Reading data...";
      char buf2[100];
      size_t nRead = serPort->Read(buf2, sizeof(buf2));
      cout << nRead << " bytes read: '";

      buf2[nRead] = 0;
      cout << buf2 << "'" << endl;
    }
  }
  catch (const std::exception& e)
  {
    cerr << e.what() << endl;
    return -1;
  }

  return 0;
}