serialinterface.cpp
Go to the documentation of this file.
1 
9 
11 {
12  baudRate = 921600;
13  deviceTag = "usb-FTDI_FT232R_USB_UART";
14  serialTimeout = 1000;
15  maxLineLength = 65536;
16  serialDevicesFolder = "/dev/serial/by-id/";
17 }
18 
20  this->baudRate = baudRate;
21 }
22 
23 void SerialInterface::setDeviceTag(const std::string& deviceTag){
24  this->deviceTag = deviceTag;
25 }
26 
28  this->serialTimeout = serialTimeout;
29 }
30 
32  this->maxLineLength = maxLineLength;
33 }
34 
36  this->serialDevicesFolder = serialDevicesFolder;
37 }
38 
40 {
41  if (serialConnection.isOpen())
42  return;
43  std::string deviceName;
44  findDeviceName(deviceName);
45  serialConnection.setPort(deviceName);
46  serialConnection.setBaudrate(baudRate);
47  serial::Timeout timeout = serial::Timeout::simpleTimeout(serialTimeout);
48  serialConnection.setTimeout(timeout);
49  serialConnection.open();
50 }
51 
52 void SerialInterface::send(std::string& message)
53 {
54  if (!serialConnection.isOpen())
55  throw std::runtime_error("UC-board connection not established/lost.");
56  serialConnection.write(message);
57 }
58 
59 // be careful when using this method, it will block the calling thread until
60 // something has been
61 // received or the request took longer than a certain timeout threshold
62 void SerialInterface::read(std::string& message, std::string& delimiter)
63 {
64  if (!serialConnection.isOpen())
65  throw std::runtime_error("UC-board connection not established/lost.");
66  if (serialConnection.waitReadable())
67  serialConnection.readline(message, maxLineLength, delimiter);
68 }
69 
71 {
72  if (!serialConnection.isOpen())
73  return;
74  serialConnection.close();
75 }
76 
77 void SerialInterface::findDeviceName(std::string& deviceName)
78 {
79  std::string serialDevices = serialDevicesFolder;
80  std::string devicePath;
81 
82  struct dirent** fileList;
83  int numOfFiles = scandir(serialDevices.c_str(), &fileList, NULL, alphasort);
84  if (numOfFiles < 3)
85  {
86  throw std::runtime_error("No serial devices found.");
87  }
88 
89  for (int i = 0; i < numOfFiles; i++)
90  {
91 
92  if (std::string(fileList[i]->d_name).find(deviceTag) != std::string::npos)
93  {
94  devicePath.append(serialDevices);
95  devicePath.append(std::string(fileList[i]->d_name));
96  break;
97  }
98  }
99 
100  if (devicePath.length() <= 0)
101  {
102  throw std::runtime_error("UC-board is not connected.");
103  }
104 
105  char buffer1[PATH_MAX + 1];
106  size_t len = readlink(devicePath.c_str(), buffer1, sizeof(buffer1) - 1);
107  if (len != -1)
108  {
109  buffer1[len] = '\0';
110  }
111  char buffer2[PATH_MAX + 1];
112  char* rp = realpath(serialDevices.append(std::string(buffer1)).c_str(), buffer2);
113  deviceName = std::string(buffer2);
114 }
serial::Serial serialConnection
unsigned int serialTimeout
void setSerialTimeout(unsigned int serialTimeout)
Setter for the serial timeout.
void setMaxLineLength(unsigned int maxLineLength)
Setter for the maximum line length.
unsigned int baudRate
void disconnect()
try to close the connection to the serial device
std::string serialDevicesFolder
unsigned int maxLineLength
void setBaudRate(unsigned int baudRate)
Setter for the baudrate, which defines the bandwith of the communication.
void findDeviceName(std::string &deviceName)
Header file for the SerialInterface class.
SerialInterface()
SerialInterface default constructor.
void setDeviceTag(const std::string &deviceTag)
Setter for the device tag, which is an identifier a the connected serial device.
void connect()
try to open a connection to the serial device
void setSerialDevicesFolder(const std::string &serialDevicesFolder)
Setter for the path to the devices folder.
void send(std::string &message)
try to send a message to the serial device
void read(std::string &message, std::string &delimiter)
read a message from the serial input buffer
std::string deviceTag


pses_ucbridge
Author(s): Sebastian Ehmes
autogenerated on Sat Oct 28 2017 19:16:13