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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

guangshui516

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值