General and Desktop
2019.05.21 21:55

QtSerialPort를 사용한 시리얼(Serial)통신

조회 수 16307 추천 수 0 댓글 3
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

이 글에서는 Qt SerialPort를 사용해서 라즈베리파이3와 PC(윈도우10)간 시리얼(Serial)통신을 설명한다. Qt SerialPort모듈은 시리얼 통신을 쉽게 할 수 있도록 일련의 편의기능들을 제공한다.

 

다음 운영체제에서 모든 기능을 사용할 수 있으며 비공식적으로 안드로이드 장치에서도 사용할 수 있다.

Windows XP/Vista/7/8/10

Gnu/Linux

MacOSX

 

라즈베리파이3는 욕토(yocto)프로젝트를 이용한 Qt 및 임베디드 리눅스 환경이다. (먼저 간단한 테스트를 통해 시리얼통신이 가능한지 확인한다.)

 

Qt SerialPort 모듈을 이용하려면 Qt프로젝트 파일에 다음과 같이 명시 해줘야한다.

QT += serialport

 

소스코드에는 다음과 같이 헤더파일을 include 해준다.

#include <QSerialPort>

 

QSerialPortInfo 클래스는 사용 가능한 시리얼 포트에 대한 정보를 제공한다.

#include <QSerialPortInfo>

 

라즈베리파이3에서는 QSerialPort클래스의 인스턴스를 생성하고 시리얼 장치와 속도를 설정한다.

m_serialPort = new QSerialPort();
m_serialPort->setPortName("/dev/ttyAMA0");
m_serialPort->setBaudRate(QSerialPort::Baud115200);

 

그리고 수신된 데이터가 있으면 발생하는 QSerialPort::readyRead 시그널을 슬롯에 연결하여 수신된 데이터를 읽을 수 있다. 예를 들면 다음과 같다.

connect(m_serialPort, &QSerialPort::readyRead, this, &SerialPortReader::handleReadyRead);

 

수신된 데이터를 읽는 슬롯 함수를 구현한다.

void SerialPortReader::handleReadyRead(){
    m_serialPort->readAll();
}

 

open을 호출하여 시리얼 장치를 연다.

if(m_serialPort->open(QIODevice::ReadWrite) == false)
    qDebug() << m_serialPort->errorString();

 

 

PC에서는 COMx장치를 사용할 수 있어야한다.

m_serialPort = new QSerialPort();
m_serialPort->setPortName("COM3");
m_serialPort->setBaudRate(QSerialPort::Baud115200);

 

PC에서 라즈베리파이에 데이터를 전송하기위해 write를 사용한다.

m_serialPort->write(QByteArray());

 

다음은 QString을 QByteArray로 변환하여 전송하거나,

m_serialPort->write(QString("makersweb.net").toLocal8Bit());

 

다음과 같은 구조체처럼 약속된 형태를

struct Format{
    unsigned char data1 : 1;
    unsigned char data2 : 1;
    unsigned char data3 : 1;
    unsigned char data4 : 1;
    unsigned char data5 : 1;
    unsigned char data6 : 1;
    unsigned char data7 : 1;
    unsigned char data8 : 1;
};

 

char * 타입으로 변환하여 데이터를 전송하는 방법이 될 수도있다.

void sendData(){
    Format *data = new Format;

    data->data1 = 1;
    data->data2 = 0;
    data->data3 = 0;
    data->data4 = 0;

    data->data5 = 1;
    data->data6 = 1;
    data->data7 = 1;
    data->data8 = 1;

    auto packet = reinterpret_cast<char*>(data);

    // send
    m_serialPort->write(packet, sizeof (Format));

    // delete
    delete data;
}

 

  • ?
    데미안 2020.10.22 10:19

    잘 봤습니다.

  • ?
    김상연 2021.03.28 14:09

    SerialPortReader라는 선언자가 없다고 에러가 뜨는게 어떻게 해결하면 되나요??

     

  • ?
    makersweb 2021.03.28 14:37

    SerialPortReader 는 설명을 위한 예제 클래스일 뿐입니다. 주목할 점은 QSerialPort 유형의 멤버(m_serialPort)자료입니다.

    적절한 클래스를 선언하면 됩니다.


  1. No Image notice

    Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서

    Date2019.01.05 CategoryQML and Qt Quick By운영자 Views109984
    read more
  2. No Image

    컨테이너에 적재된 객체를 편리하게 삭제하기

    Date2019.09.18 CategoryGeneral and Desktop Bymakersweb Views3439
    Read More
  3. C++로 구현된 모델을 QML의 ListView에서 참조

    Date2019.09.07 CategoryQML and Qt Quick Bymakersweb Views7814
    Read More
  4. No Image

    QSocketNotifier로 파일 디스크립터의 활동감지

    Date2019.08.28 CategoryQML and Qt Quick Bymakersweb Views4520
    Read More
  5. No Image

    MCU용 Qt에 대해서

    Date2019.08.22 CategoryMobile and Embedded Bymakersweb Views4089
    Read More
  6. [Qt News] Qt for Python을 위한 기술 비전

    Date2019.08.20 CategoryGeneral and Desktop Byj2doll Views4348
    Read More
  7. No Image

    열거형(enum)을 QML에서 사용하는 방법과 문자열(QString)로 얻기

    Date2019.08.20 CategoryQML and Qt Quick Bymakersweb Views6777
    Read More
  8. No Image

    [Qt News] Qt 6 기술 비전 (Technical vision for Qt 6)

    Date2019.08.08 CategoryGeneral and Desktop Byj2doll Views4587
    Read More
  9. No Image

    [Qt News] Qt6 Git 개발 초기 단계 시작하기

    Date2019.08.02 CategoryInstallation and Deployment Byj2doll Views4449
    Read More
  10. No Image

    [Qt] Google Play의 향후 요구 사항을 준수하는 방법

    Date2019.07.29 CategoryQML and Qt Quick Byj2doll Views3405
    Read More
  11. No Image

    Qt기반의 오픈소스 프로젝트들 - 2

    Date2019.07.21 CategoryGeneral and Desktop By운영자 Views7051
    Read More
  12. No Image

    QML, 이미지, 폰트등을 바이너리 리소스로 만들기

    Date2019.06.24 CategoryInstallation and Deployment Bymakersweb Views6179
    Read More
  13. Qt Creator에서 임베디드 장치로 deploy설정(Custom Process Step)

    Date2019.06.15 CategoryInstallation and Deployment Bymakersweb Views4962
    Read More
  14. Qt Quick Controls 2사용 및 스타일 설정

    Date2019.06.07 CategoryQML and Qt Quick Bymakersweb Views8922
    Read More
  15. No Image

    QML 강좌 - 동적 Listing (ListView)

    Date2019.06.01 CategoryQML and Qt Quick Bymakersweb Views12546
    Read More
  16. QtInstallerFramework로 온라인 설치프로그램(Online Installer)만드는 방법

    Date2019.05.28 CategoryInstallation and Deployment Bymakersweb Views8604
    Read More
  17. QtCreator Design으로 GUI만들기 (QML로 만드는 Hello World -2)

    Date2019.05.26 CategoryQML and Qt Quick Bymakersweb Views18770
    Read More
  18. No Image

    QML에서 멀티 스레드(multithreading) 프로그래밍

    Date2019.05.25 CategoryQML and Qt Quick Bymakersweb Views4880
    Read More
  19. No Image

    QtSerialPort를 사용한 시리얼(Serial)통신

    Date2019.05.21 CategoryGeneral and Desktop Bymakersweb Views16307
    Read More
  20. No Image

    Qt기반의 오픈소스 프로젝트들

    Date2019.05.15 CategoryGeneral and Desktop Bymakersweb Views7801
    Read More
  21. No Image

    Q_D매크로와 d-pointer

    Date2019.05.07 CategoryGeneral and Desktop Bymakersweb Views3620
    Read More
Board Pagination Prev 1 4 5 6 7 8 9 Next
/ 9