Program Listing for File SerialIO.h

Return to documentation for file (/tmp/ws/src/sicks300_2/include/common/SerialIO.h)

/*
 * 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 _SerialIO_H
#define _SerialIO_H

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

#include <string>
#include <string.h>

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 changeBaudRate(int 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 //