Linux QT串口通信遇到的问题

问题:遇到device can not open 
解决:sudo chown young /dev/ttyUSB0

程序如下:

uart.h:

#ifndef UART_H
#define UART_H

#include
#include
#include
class Uart : public QObject
{
    Q_OBJECT
public:
    explicit Uart(QObject *parent = 0);
    ~Uart();
    void InitUart();
    short roll;
    short pitch;
    short yaw;
    short m_throttle;
    float m_altitude;
    short gl_Angle;
private:
    QSerialPort *my_serialport;
    QByteArray requestData;
    unsigned char *buffer;
signals:
    void changeAngle(short,short,short);
    void changeThrottle(short);
    void changeAltitude(float);
    void changeYawGL(short);
    void changeAngleToUI(short,short,short);
    void isOpenUart(bool);
    void IsConnectControl(int);
public slots:
    void readUartSlot();
    void connectUart();
    void disconnectUart();
};

#endif // UART_H

 

uat.cpp:

#include "uart.h"
#include
#include
Uart::Uart(QObject *parent) :
    QObject(parent)
{
    roll = 0;
    pitch = 0;
    yaw = 0;
    m_throttle = 0;
    gl_Angle = 0;
    my_serialport = new QSerialPort();
    QString portName = "ttyUSB0";
    my_serialport->setPortName(portName);
    if(my_serialport->open(QIODevice::ReadOnly))
    {
        my_serialport->setBaudRate(115200);
        my_serialport->setDataBits(QSerialPort::Data8);
        my_serialport->setParity(QSerialPort::NoParity);
        my_serialport->setStopBits(QSerialPort::OneStop);
        my_serialport->setFlowControl(QSerialPort::NoFlowControl);
        buffer = (unsigned char*)malloc(12);
        emit isOpenUart(true);
    }
    else
    {
        emit isOpenUart(false);
    }

}
 Uart::~Uart()
 {
     free(buffer);
     my_serialport->close();
     delete my_serialport;
 }
void Uart::connectUart()
{
    connect(this->my_serialport,SIGNAL(readyRead()),this,SLOT(readUartSlot()));
}
void Uart::disconnectUart()
{
    disconnect(this->my_serialport,SIGNAL(readyRead()),this,SLOT(readUartSlot()));
}
void Uart::readUartSlot()
{
    short temp_roll,temp_pitch,temp_yaw;
    if(my_serialport->bytesAvailable() >3)
    {
        requestData  =  my_serialport->readAll();
        if(!requestData.isEmpty())
        {
            if(requestData.length()==10)
            {
                if(requestData.at(0)=='e' && requestData.at(1)=='f' && requestData.at(4)=='g' && requestData.at(7) =='h')
                {
                    buffer = (unsigned char*)requestData.data();
                    temp_roll = (short)((buffer[2]<<8) + buffer[3]);
                    temp_pitch= (short)((buffer[5]<<8) +buffer[6]);
                    temp_yaw = (short)((buffer[8]<<8) + buffer[9]);
                    roll = temp_roll;
                    pitch = temp_pitch;
                    yaw = temp_yaw;
                    emit changeAngle(roll,pitch,yaw);
                    emit changeAngleToUI(roll,pitch,yaw);
                }
            }
            if(requestData.length() == 4)
            {
                //qDebug()<                 if(requestData.at(0) == 'a' && requestData.at(1) == 'b')
                {
                    unsigned char *thro_buff;
                    thro_buff =(unsigned char*)requestData.data();
                    m_throttle = (short)(((thro_buff[2]<<8 )+ thro_buff[3])/10-100);
                    emit changeThrottle(m_throttle);
                }
                if(requestData.at(0) == 'c' && (requestData.at(1)) == 'd')
                {
                    unsigned char *altitudeBuf;
                    altitudeBuf = (unsigned char*)requestData.data();
                    m_altitude = (float)(((altitudeBuf[2]<<8) + altitudeBuf[3]));
                    if(m_altitude)
                    emit changeAltitude(m_altitude);
                }
                if(requestData.at(0)=='i'&& requestData.at(1) == 'j')
                {
                    unsigned char *glBuffer;
                    glBuffer = (unsigned char*)requestData.data();
                   short yaw = (short)((glBuffer[2]<<8) + glBuffer[3]);
                   // qDebug()<<"gl_Angle"<                    short temp = gl_Angle - yaw;
                   emit changeYawGL(temp);
                    gl_Angle =  yaw;
                }
            }
            //if(requestData.length()==4)
            //{
              //  if(requestData.at(0) == 'b' && requestData.at(1) == 'l' && requestData.at(2) == 'u' && requestData.at(3) == 'e')
               // {
                 //   emit IsConnectControl(1);
               // }
           // }
      /* if(requestData.size() == 4)
       {
            if(requestData.at(0) == 'c' && (requestData.at(1)) == 'd')
            {
                unsigned char H_value = (unsigned char)requestData.at(2);
                unsigned char L_value = (unsigned char)requestData.at(3);
                m_altitude = (short)((H_value<<8) + L_value);
                qDebug()<

            }
       }*/
    }
   requestData.clear();
    }
  // my_serialport->flush();
}

 

你可能感兴趣的:(qt程序)