MRPT  2.0.3
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <mrpt/comms/CSerialPort.h>
11 #include <iostream>
12 #include <memory>
13 
14 using namespace mrpt::comms;
15 using namespace std;
16 
17 int main()
18 {
19  try
20  {
21  std::unique_ptr<CSerialPort> serPort;
22 
23  string serName;
24 
25  cout << "Serial port test application: Use it with a loopback serial "
26  "port (pins 2-3 connected)"
27  << endl;
28 
29  cout << "Enter the serial port name (e.g. COM1, ttyS0, ttyUSB0): ";
30  getline(cin, serName);
31 
32  cout << endl;
33  cout << "Opening serial port...";
34  serPort = std::make_unique<CSerialPort>(serName);
35  cout << "OK" << endl;
36 
37  cout << "Setting timeouts...";
38  serPort->setTimeouts(100, 1, 100, 1, 100);
39  cout << "OK" << endl;
40 
41  cout << "Setting baud rate...";
42  serPort->setConfig(500000);
43  cout << "OK" << endl;
44 
45  for (int i = 0; i < 10; i++)
46  {
47  // Test write:
48  cout << "Writing test data...";
49  const char buf1[] = "Hello world!";
50  size_t written = serPort->Write(buf1, sizeof(buf1));
51  cout << written << " bytes written." << endl;
52 
53  // Read:
54  cout << "Reading data...";
55  char buf2[100];
56  size_t nRead = serPort->Read(buf2, sizeof(buf2));
57  cout << nRead << " bytes read: '";
58 
59  buf2[nRead] = 0;
60  cout << buf2 << "'" << endl;
61  }
62  }
63  catch (const std::exception& e)
64  {
65  cerr << e.what() << endl;
66  return -1;
67  }
68 
69  return 0;
70 }
CSerialPort.h
mrpt::comms
Serial and networking devices and utilities.
Definition: CClientTCPSocket.h:21
mrpt::comms::CSerialPort::Write
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
Definition: CSerialPort.cpp:834
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::comms::CSerialPort::setConfig
void setConfig(int baudRate, int parity=0, int bits=8, int nStopBits=1, bool enableFlowControl=false)
Changes the configuration of the port.
Definition: CSerialPort.cpp:202
mrpt::comms::CSerialPort::setTimeouts
void setTimeouts(int ReadIntervalTimeout, int ReadTotalTimeoutMultiplier, int ReadTotalTimeoutConstant, int WriteTotalTimeoutMultiplier, int WriteTotalTimeoutConstant)
Changes the timeouts of the port, in milliseconds.
Definition: CSerialPort.cpp:564
mrpt::comms::CSerialPort::Read
size_t Read(void *Buffer, size_t Count) override
Implements the virtual method responsible for reading from the stream - Unlike CStream::ReadBuffer,...
Definition: CSerialPort.cpp:648



Page generated by Doxygen 1.8.17 for MRPT 2.0.3 at Fri May 15 15:49:54 UTC 2020