Program Listing for File SerialIO.hpp

Return to documentation for file (include/sicks300_ros2/common/SerialIO.hpp)

/*
 * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SICKS300_ROS2__COMMON__SERIALIO_HPP_
#define SICKS300_ROS2__COMMON__SERIALIO_HPP_

#include <termios.h>
#include <sys/select.h>
#include <string.h>

#include <string>

class SerialIO
{
public:
  // ---------------------- Constants
  enum HandshakeFlags
  {
    HS_NONE,
    HS_HARDWARE,
    HS_XONXOFF
  };

  enum ParityFlags
  {
    PA_NONE,
    PA_EVEN,
    PA_ODD,
// UNIX serial drivers only support even, odd, and no parity bit generation.
    PA_MARK,
    PA_SPACE
  };

  enum StopBits
  {
    SB_ONE,
    SB_ONE_5,             // ????? returns an error ?????
    SB_TWO
  };

  SerialIO();

  virtual ~SerialIO();

  void setDeviceName(const char * Name) {m_DeviceName = Name;}

  void setBaudRate(int BaudRate) {m_BaudRate = BaudRate;}

  void setMultiplier(double Multiplier = 1) {m_Multiplier = Multiplier;}

  void SetFormat(int ByteSize, ParityFlags Parity, int stopBits)
  {m_ByteSize = ByteSize; m_Parity = Parity; m_StopBits = stopBits;}

  void setHandshake(HandshakeFlags Handshake) {m_Handshake = Handshake;}

  void setBufferSize(int ReadBufSize, int WriteBufSize)
  {m_ReadBufSize = ReadBufSize; m_WriteBufSize = WriteBufSize;}

  void setTimeout(double Timeout);

  void setBytePeriod(double Period);

  int openIO();

  void closeIO();

  int readBlocking(char * Buffer, int Length);


  int readNonBlocking(char * Buffer, int Length);

  int writeIO(const char * Buffer, int Length);

  int getSizeRXQueue();


  void purge()
  {
    ::tcflush(m_Device, TCIOFLUSH);
  }

  void purgeRx()
  {
    tcflush(m_Device, TCIFLUSH);
  }

  void purgeTx()
  {
    tcflush(m_Device, TCOFLUSH);
  }

  void flushTx()
  {
    tcdrain(m_Device);
  }

protected:
  ::termios m_tio;
  std::string m_DeviceName;
  int m_Device;
  int m_BaudRate;
  double m_Multiplier;
  int m_ByteSize, m_StopBits;
  ParityFlags m_Parity;
  HandshakeFlags m_Handshake;
  int m_ReadBufSize, m_WriteBufSize;
  double m_Timeout;
  ::timeval m_BytePeriod;
  bool m_ShortBytePeriod;
};


#endif  // SICKS300_ROS2__COMMON__SERIALIO_HPP_