Main MRPT website > C++ reference for MRPT 1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <mrpt/comms/CSerialPort.h>
11 #include <iostream>
12 
13 using namespace mrpt::comms;
14 using namespace std;
15 
16 int main()
17 {
18  try
19  {
20  CSerialPort* serPort;
21 
22  string serName;
23 
24  cout << "Serial port test application: Use it with a loopback serial "
25  "port (pins 2-3 connected)"
26  << endl;
27 
28  cout << "Enter the serial port name (e.g. COM1, ttyS0, ttyUSB0): ";
29  getline(cin, serName);
30 
31  cout << endl;
32  cout << "Opening serial port...";
33  serPort = new CSerialPort(serName);
34  cout << "OK" << endl;
35 
36  cout << "Setting timeouts...";
37  serPort->setTimeouts(100, 1, 100, 1, 100);
38  cout << "OK" << endl;
39 
40  cout << "Setting baud rate...";
41  serPort->setConfig(500000);
42  cout << "OK" << endl;
43 
44  for (int i = 0; i < 10; i++)
45  {
46  // Test write:
47  cout << "Writing test data...";
48  const char buf1[] = "Hello world!";
49  size_t written = serPort->Write(buf1, sizeof(buf1));
50  cout << written << " bytes written." << endl;
51 
52  // Read:
53  cout << "Reading data...";
54  char buf2[100];
55  size_t nRead = serPort->Read(buf2, sizeof(buf2));
56  cout << nRead << " bytes read: '";
57 
58  buf2[nRead] = 0;
59  cout << buf2 << "'" << endl;
60  }
61 
62  delete serPort;
63  }
64  catch (std::exception& e)
65  {
66  cerr << e.what() << endl;
67  return -1;
68  }
69 
70  return 0;
71 }
CSerialPort.h
mrpt::comms::CSerialPort::Read
size_t Read(void *Buffer, size_t Count)
Implements the virtual method responsible for reading from the stream - Unlike CStream::ReadBuffer,...
Definition: CSerialPort.cpp:661
mrpt::comms::CSerialPort
A communications serial port built as an implementation of a utils::CStream.
Definition: CSerialPort.h:43
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:847
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:215
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:577



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST