Arm Qt 实战一:简单使用Arm设备的串口

Posted 秦晓宇

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Arm Qt 实战一:简单使用Arm设备的串口相关的知识,希望对你有一定的参考价值。

1、Arm串口驱动程序

创建Serial类:serial.h、serial.cpp

#include "serial.h"
#include <QDebug>

#define printf qDebug

Serial::Serial()


bool Serial::opend(QString dev,BAUD_RATE buad,char nEvent)

    fd = open(dev.toLocal8Bit().data(), O_RDWR);
    if(fd < 0)
    
        printf("open device = %s false!!",dev.toLatin1().data());
        return false;
    
    else
    
        printf("open device = %s ok!!",dev.toLatin1().data());
        switch(buad)
        
            case BAUD115200:
                printf("open set baud rate = 115200");
                init(fd, B115200, &sendFd, &reciveFd);
            break;
            case BAUD38400:
                printf("open set baud rate = 38400");
                init(fd, B38400, &sendFd, &reciveFd);
            break;
            case BAUD19200:
                printf("open set baud rate = 19200");
                init(fd, B19200, &sendFd, &reciveFd);
            break;
            case BAUD9600:
                printf("open set baud rate = 9600");
                init(fd, B9600, &sendFd, &reciveFd);
            break;
        
        return true;
    

void Serial::closed()

    close(fd);


bool Serial::init(int i_fd, speed_t i_baud, fd_set* i_sendSet, fd_set* i_rcvSet)

    int i;

    struct termios newtio;

    printf("init");

    FD_ZERO(i_rcvSet);									// Çå¿Õ´®¿Ú½ÓÊն˿ڼ¯
    FD_ZERO(i_sendSet);									// Çå¿Õ´®¿Ú·¢ËͶ˿ڼ¯
    FD_SET(i_fd,i_rcvSet);								// ÉèÖô®¿Ú½ÓÊն˿ڼ¯
    FD_SET(i_fd,i_sendSet);								// ÉèÖô®¿Ú·¢ËͶ˿ڼ¯

    bzero(&newtio, sizeof(newtio));
    tcgetattr(i_fd, &newtio);							// µÃµ½µ±Ç°´®¿ÚµÄ²ÎÊý
    cfsetispeed(&newtio, i_baud);						// ÉèÖÃÊäÈ벨ÌØÂÊÉè
    cfsetospeed(&newtio, i_baud);						// ÉèÖÃÊä³ö²¨ÌØÂÊÉè
    newtio.c_cflag |= (CLOCAL | CREAD);					// ʹÄܽÓÊÕ²¢Ê¹Äܱ¾µØ״̬
    newtio.c_cflag &= ~PARENB;							// ÎÞУÑé 8λÊý¾Ýλ1λֹͣλ
    newtio.c_cflag &= ~CSTOPB;
    newtio.c_cflag &= ~CSIZE;
    newtio.c_iflag &= ~(ICRNL | IXON);
    newtio.c_cflag |= CS8;
    newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);	// ԭʼÊý¾ÝÊäÈë
    newtio.c_oflag &= ~(OPOST);

    newtio.c_cc[VTIME]    = 0;   						// ÉèÖõȴýʱ¼äºÍ×îС½ÓÊÕ×Ö·ûÊý
    newtio.c_cc[VMIN]     = 0;

    tcflush(i_fd, TCIFLUSH);							// ´¦Àíδ½ÓÊÕµÄ×Ö·û
    tcsetattr(i_fd,TCSANOW,&newtio); 					// ¼¤»îÐÂÅäÖÃ

    //this->start();


bool Serial::writed(char *data,int len)

    write(fd, data, len);

bool Serial::writed(char *data)

    write(fd, data, sizeof(data));


int Serial::readData(unsigned char *data,int len)

    return read(fd,data,len);


int Serial::readData(char *data,int len)

    int ret = read(fd,data,len);
    return ret;



#ifndef SERIAL_H
#define SERIAL_H

#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <linux/types.h>
#include <linux/fb.h>
#include <stdio.h>
#include <sys/wait.h>
#include <pthread.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <time.h>
#include <termios.h>
#include <sys/select.h>

#include <QString>
#include <QThread>

class Serial

public:
    Serial();

    enum BAUD_RATE
    
        BAUD115200,
        BAUD38400,
        BAUD19200,
        BAUD9600,
    ;


    bool opend(QString dev,BAUD_RATE buad,char nEvent);
    bool init(int i_fd, speed_t i_baud, fd_set* i_sendSet, fd_set* i_rcvSet);
    void closed();
    bool writed(char *data,int len);
    bool writed(char *data);
    int readData(unsigned char *data,int len);
    int readData(char *data,int len);

private:
    int fd;
    char writeBuf[50];
    char reciveBuf[50];
    fd_set sendFd,reciveFd;
    void debug(QString str);
;

#endif // SERIAL_H

2、使用Serial操作串口

创建一个Rs485sender线程类,操作Serial进行数据的发送

rs485sender.cpp

#include "rs485sender.h"
#include <stdio.h>
#include <stdlib.h>

Rs485sender::Rs485sender(QObject *parent) :
    QThread(parent)

    com = new Serial();
    com->opend("/dev/ttyO1",Serial::BAUD115200,'n');

    moveToThread(this);
    this->start();

void Rs485sender::run()

    printf("Rs485sender::run\\r\\n");
    char comInput[100];

    while(true)
    
        this->msleep(1000);
        memset(comInput,0,sizeof(comInput));
        QString comCmd = "send com2";
        com->writed(comCmd.toLatin1().data(),comCmd.length());
       printf("Rs485sender::send ----- \\r\\n");
    


rs485sender.h

#ifndef RS485SENDER_H
#define RS485SENDER_H


#include <QThread>
#include <QtCore>
#include <QString>
#include <QMap>
#include <QMutex>
#include <QTimer>

#include "../../util/serial.h"

class Rs485sender : public QThread

    Q_OBJECT
public:
    explicit Rs485sender(QObject *parent = 0);

private:
    QMutex qmutex;
    Serial *com;

protected:
    void run();
    QString getValue(QString in,QString cut);
    void deal(char *in,int len);

signals:

public slots:

;

#endif // RS485SENDER_H

创建Rs485resver1进行数据的接收

rs485resver1.cpp

#include "rs485resver1.h"
#include <stdio.h>
#include <stdlib.h>

Rs485resver1::Rs485resver1(QObject *parent) :
    QThread(parent)

    com = new Serial();
    com->opend("/dev/ttyO3",Serial::BAUD115200,'n');

    moveToThread(this);
    this->start();

void Rs485resver1::run()

    printf("Rs485resver1::run\\r\\n");
    char comInput[100];

    while(true)
    
        this->msleep(1000);
        memset(comInput,0,sizeof(comInput));
//        String com1Cmd = "read com2";
        //com1Input->writed(com1Cmd.toLatin1().data(),com1Cmd.length());
        int len = com->readData(comInput,sizeof(comInput));
        printf("Rs485resver1::%s \\r\\n",comInput);
    


rs485resver1.h

#ifndef RS485RESVER1_H
#define RS485RESVER1_H


#include <QThread>
#include <QtCore>
#include <QString>
#include <QMap>
#include <QMutex>
#include <QTimer>

#include "../../util/serial.h"

class Rs485resver1 : public QThread

    Q_OBJECT
public:
    explicit Rs485resver1(QObject *parent = 0);

private:
    QMutex qmutex;
    Serial *com;

protected:
    void run();
    QString getValue(QString in,QString cut);
    void deal(char *in,int len);

signals:

public slots:

;

#endif // RS485RESVER1_H

Main方法中调用

    Rs485resver1 *rs485resver1;
    Rs485resver2 *rs485resver2;
    Rs485sender *rs485sender;
    
    /*485总线测试*/
    rs485resver1 = new Rs485resver1();
    rs485resver2 = new Rs485resver2();
    rs485sender = new Rs485sender();

3、ARM设备进行线缆连接

1、将板卡上的485线缆进行连接,串口1的A和串口2的A连接,串口1的B和串口2的B连接,485不是AB交叉连接的,A对A,B对B
2、我的板卡有4个485口,我使用了1个发送,2个接收。

4、运行代码测试:

可以看到,Rs485sender发送数据Rs485resver1、Rs485resver2收到数据。

以上是关于Arm Qt 实战一:简单使用Arm设备的串口的主要内容,如果未能解决你的问题,请参考以下文章

C与ARM汇编结合实现mini2440串口uart简单程序

Arm Qt 实战二:使用libmodbus创建MobusRtu-modbus_new_rtu为NULL的问题

一个基于Qt的截屏程序

ARM裸机开发:串口通信

并行接口的LCD怎样和ARM进行串口连接

《设备树 — 基础知识》