ISSerialPort.cpp
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright (c) 2014-2021 Inertial Sense, Inc. - http://inertialsense.com
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #include "ISSerialPort.h"
14 #include "ISLogger.h"
15 #include "ISFileManager.h"
16 
18 {
19  if (serial != NULLPTR)
20  {
21  m_serial = *serial;
22  }
23  else
24  {
26  }
27  Close();
28 }
29 
31 {
32  Close();
33 }
34 
35 bool cISSerialPort::Open(const std::string& portName, int baudRate, int timeout, bool blocking)
36 {
37  m_timeout = timeout;
38  m_blocking = blocking;
39  return (serialPortOpen(&m_serial, portName.c_str(), baudRate, (int)m_blocking) != 0);
40 }
41 
43 {
44  return serialPortClose(&m_serial);
45 }
46 
47 int cISSerialPort::Read(void* data, int dataLength)
48 {
49  return serialPortReadTimeout(&m_serial, (unsigned char*)data, dataLength, m_timeout);
50 }
51 
52 int cISSerialPort::Write(const void* data, int dataLength)
53 {
54  return serialPortWrite(&m_serial, (const unsigned char*)data, dataLength);
55 }
56 
57 void cISSerialPort:: GetComPorts(vector<string>& ports)
58 {
59  ports.clear();
60 
61 #if PLATFORM_IS_WINDOWS
62 
63  char comPort[64];
64  char targetPath[256];
65 
66  for (int i = 0; i < 256; i++) // checking ports from COM0 to COM255
67  {
68  snprintf(comPort, sizeof(comPort), "COM%d", i);
69  if (QueryDosDeviceA(comPort, targetPath, 256))
70  {
71  ports.push_back(comPort);
72  }
73  }
74 
75 #else
76 
77  ISFileManager::GetAllFilesInDirectory("/dev", false, "^/dev/ttyUSB", ports);
78  ISFileManager::GetAllFilesInDirectory("/dev", false, "^/dev/ttyACM", ports);
79 
80 #endif
81 
82 }
int serialPortReadTimeout(serial_port_t *serialPort, unsigned char *buffer, int readCount, int timeoutMilliseconds)
Definition: serialPort.c:89
int serialPortOpen(serial_port_t *serialPort, const char *port, int baudRate, int blocking)
Definition: serialPort.c:28
int Write(const void *data, int dataLength) OVERRIDE
static void GetComPorts(vector< string > &ports)
int serialPortPlatformInit(serial_port_t *serialPort)
#define NULLPTR
Definition: ISConstants.h:426
serial_port_t m_serial
Definition: ISSerialPort.h:30
bool GetAllFilesInDirectory(const std::string &directory, bool recursive, const std::string &regexPattern, std::vector< std::string > &files)
int serialPortClose(serial_port_t *serialPort)
Definition: serialPort.c:66
int serialPortWrite(serial_port_t *serialPort, const unsigned char *buffer, int writeCount)
Definition: serialPort.c:201
USBInterfaceDescriptor data
int Read(void *data, int dataLength) OVERRIDE
cISSerialPort(const cISSerialPort &copy)
int Close() OVERRIDE
#define snprintf
Definition: test_suite.cpp:86
bool Open(const std::string &portName, int baudRate=BAUDRATE_921600, int timeout=0, bool blocking=false)
virtual ~cISSerialPort()


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:57