5003.c++定義hpp文件

c++定義hpp文件

 c++ 定義hpp文件,注意事項
 方法定義實現必須放在類中,否則會出現重複定義情況。
/********************************************************************************************************
 * SerialPort.hpp 自定義出串口類,運行平臺linux操作系統.
 * 描述:實現串口的初始化.
 * 作者:xhome
 * 時間:2020/1/10
 *******************************************************************************************************/
#ifndef MAVLINK_DATALINK_SERIALPORT_HPP
#define MAVLINK_DATALINK_SERIALPORT_HPP

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include "mavlink.h"

using namespace std;

//串口操作類.
class SerialPort {

public:
    SerialPort(string uart_name, speed_t baudrate){
            uartName = uart_name;
            speedBaud = baudrate;
    }

public:
    /**
     * 功能: 打開設備文件
     * 參數: 無
     * 返回值: 文件描述符.
     * 作者:xhome
     * 時間:2020/1/10
     */
    int open_serial_dev() {
        int fd_ = 0;
        if(uartName.length() == 0){
            cout << "devName is NULL." << endl;
            return -1;
        }
        fd_ = open(uartName.c_str(), O_RDWR);
        if(fd_ < 0){
            cout << uartName << " is opened failed." << endl;
            return -1;
        }else{
            cout << uartName << " is opened ok." << endl;
        }

        fd = fd_;

        return fd_;
    }
    int  close_serial()//關閉串口設備文件.
    {
        close(fd);
    }

    /**
     * 功能: 串口初始化
     * 參數: fd  文件描述符
     * 作者:  xhome
     * 時間:  2020/1/10
     */
    void serial_init() //串口初始化.
    {
        if(fd < 0){
            cout << "serial_init failed ." << endl;
        }

        struct termios options;
        tcgetattr(fd, &options);                //讀取終端參數
        options.c_cflag |= ( CLOCAL | CREAD );  //忽略調試解調器線路狀態,使用接受器
        options.c_cflag &= ~CSIZE;              //清目前字符長度
        options.c_cflag &= ~CRTSCTS;            //不實用RTS/CTS控制流
        options.c_cflag |= CS8;                 //字符長度設置爲8
        options.c_cflag &= ~CSTOPB;             //設置一個標誌位
        options.c_iflag |= IGNPAR;              //允許輸入奇偶校驗
        options.c_iflag &= ~(ICRNL | IXON);     //回車不轉換爲換行,不允許輸入時對XON/XOFF>
        options.c_oflag = 0;
        options.c_lflag = 0;

        options.c_cflag |= CBAUDEX; //設置特定波特率的標誌位.

        cfsetispeed(&options, speedBaud);         //設置波特率爲115200
        cfsetospeed(&options, speedBaud);
        tcsetattr(fd,TCSANOW,&options);         //設置終端參數

        cout << "serial_init ok." << endl;

    }
    /**
    * 讀取串口消息.
    * 參數:message  mavlink 標準消息結構體.
    * 作者:xhome
    * 時間:2020/1/15
    */
    int  read_message(mavlink_message_t &message){
        int result = 0;
        uint8_t ch;
        mavlink_status_t status;
        uint8_t msgReceived = false;

        result = read(fd, &ch, 1);
        if(result > 0){
            msgReceived = mavlink_parse_char(MAVLINK_COMM_0, ch, &message, &status);
            // check for dropped packets
            if ( (lastStatus.packet_rx_drop_count != status.packet_rx_drop_count) )
            {
                printf("ERROR: DROPPED %d PACKETS\n", status.packet_rx_drop_count);
                unsigned char v= ch;
                fprintf(stderr,"%02x ", v);
            }
            lastStatus = status;
        }

        return msgReceived;
    }

public:
    string  uartName; //設備名稱
    speed_t speedBaud; //設備波特率
    int fd; //文件描述符.
    mavlink_status_t lastStatus;

};

#endif //MAVLINK_EXAMPLE_SERIALPORT_HPP

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章