한국어
Qt
 

Qt 로 작성된 iOS 에서 앱이 외부장치와 Serial 통신을 해본다.

 

일반적으로 크로스 플랫폼 프레임워크 애플리케이션은 iOS에서 외부장치와 시리얼 통신이 불가능하다. Apple 디바이스에서는 Apple에서 인증하고 MFi 배지가 있는 액세서리만 사용하고 독점 프로토콜(iAP2)과 소프트웨어로만 작동되게 했기 때문이다.

 

Redpark 의 Lightning Serial Cable은 RS-232 통신이 가능한 애플의 MFi 액세서리중 하나다. 함께 제공되는 SDK(라이브러리)는 External Accessory Framework의 관련된 세부 구현을 추상화하고 단순화하여 쉽게 앱을 개발할 수 있게 해준다. 따라서 Redpark 시리얼 케이블을 iPhone에 연결하고 Qt/C++로 작성하는 애플리케이션에서 Redpark 라이브러리를 링크하여 최대 115.2Kbps의 전송 속도로 다른 직렬 장치에 연결할 수 있다.

 

시리얼 케이블 구매와 SDK 및 샘플 프로젝트는 Redpark 웹사이트에서 얻을 수 있다. SDK는 Objective-C를 위해서도 제공되며 Objective-C코드에서 우리의 C++ 코드를 혼합할 수 있다. qmake는 “mm” 확장자를 컴파일러에게 이것이 Objective-C++ 파일임을 알려준다.

QMAKE_LFLAGS += -ObjC


....
OBJECTIVE_HEADERS += \
    IOSSerialPort.h \
...


OBJECTIVE_SOURCES += \
    ios/IOSSerialPort.mm \
...


기본적인 iOS 용 Qt 앱을 구축하는 방법에 대해서 다음 블로그를 참고하면 많은 도움이 된다.
https://appbus.wordpress.com/

 

테스트를 위해 아주 단순한 사용자 인터페이스를 구현한다.

스크린샷, 2022-04-29 오후 6.17.41.png

 

main.qml

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    Connections {
        target: serial

        function onDebugMessage(message){
            info.append(message)
        }
    }

    Rectangle {
        anchors.fill: parent

        TextEdit {
            id: info
            anchors.top: parent.top
            anchors.topMargin: 20
            anchors.left: parent.left
            anchors.right: parent.right

            horizontalAlignment: Text.AlignLeft

            font.pixelSize: 20
        }

        Column {
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 15
            spacing: 10

            Button{
                width: 200
                height: 50
                anchors.horizontalCenter: parent.horizontalCenter

                text: "Open"

                onClicked: {
                    serial.open()
                }
            }

            Button{
                width: 200
                height: 50
                anchors.horizontalCenter: parent.horizontalCenter

                text: "Send"

                onClicked: {
                    serial.send("Hello Makersweb.net")
                }
            }
        }
    }
}


serial 인스턴스의 클래스 구현은 Redpark 의 샘플 프로젝트 코드에서 많은 부분을 사용하며 실제 sendData 및 recvData를 호출하여 시리얼 포트를 통해 바이트를 보내거나 받을 수 있다.

#include "Serialdevice.h"
#include "IOSSerialPort.h"

#include <QDebug>

SerialDevice::SerialDevice(QObject *parent)
    : QObject{parent}
    , backend(new IOSSerialPort())
{
    connect(backend, &IOSSerialPort::receivedMessage, this, [&](QString msg){
        emit debugMessage(msg);
    });
}

SerialDevice::~SerialDevice()
{
    if(backend){
        backend->close();
        delete backend;
        backend = nullptr;
    }
}

void SerialDevice::open()
{
    backend->open();
}

void SerialDevice::send(const QString &text)
{
    if(text.isEmpty())
        return;

    backend->sendMessage(text);
}

 

데이터를 수신하면 바이트를 화면에 표시한다. Send 버튼을 누르면 문자열을 송신한다.

스크린샷, 2022-04-29 오후 6.18.39.png

 

Windows 터미널 에뮬레이터 측

rs232.png

번호 제목 글쓴이 날짜 조회 수
공지 Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 85359
176 가상키보드(Qt Virtual Keyboard)를 사용하는 방법 [32] file makersweb 2019.05.03 219974
175 콘솔에서 사용자 입력받기 file makersweb 2020.03.22 51791
174 QString 문자열 다루기 예제 운영자 2019.01.26 39608
173 Windows에서 Qt 설치 따라하기 file makersweb 2019.10.14 30791
172 Qt의 시그널 슬롯 시스템 file makersweb 2015.10.20 23435
171 QThread 소개 및 예제 makersweb 2019.12.25 19232
170 QtCreator Design으로 GUI만들기 (QML로 만드는 Hello World -2) [1] file makersweb 2019.05.26 14772
169 초보자를 위한 첫번째 프로젝트 - QML로 만드는 Hello World file makersweb 2018.03.16 14360
168 Qt 프로그래밍의 시작 makersweb 2015.10.25 14326
167 Qml과 C++로 구현하는 GUI어플리케이션 file makersweb 2018.12.25 13831
166 QML과 QtQuick 모듈 개념과 기본 타입들 makersweb 2019.04.26 13325
165 Windows에서 라즈베리파이3용 Qt5.10.0 크로스컴파일 [20] file makersweb 2018.02.23 12719
164 Qt의 오픈소스 라이센스 소개 file makersweb 2019.12.15 12384
163 Qml 기본 컴포넌트 강좌 (1) file makersweb 2019.01.03 11978
162 QtSerialPort를 사용한 시리얼(Serial)통신 [3] makersweb 2019.05.21 11759
161 Qt Installer Framework - 패키징, 설치프로그램 제작 file makersweb 2018.10.14 11601
160 Qt 응용프로그램 배포(windows) file makersweb 2018.10.10 11172
159 Ubuntu Linux에서 Qt Creator 설치 file makersweb 2016.03.06 10647
158 Qt의 스레드간 시그널 슬롯의 커넥션타입 [1] makersweb 2015.10.24 10106
157 QML 강좌 - 동적 Listing (ListView) file makersweb 2019.06.01 10029