Skip to main content

Serial Port Programming with Qt on VxWorks 6.8

·781 words·4 mins
VxWorks Qt UART Serial Port Programming
Table of Contents

πŸ’Ύ Serial Port Programming with Qt on VxWorks 6.8
#

This article demonstrates how to implement serial port communication (UART) in VxWorks 6.8 using the Qt framework, combining the strengths of real-time embedded systems with modern UI-based application design.

For related topics, see Serial Test Program Design and Source Code for VxWorks.

🧠 Introduction to VxWorks
#

VxWorks is an embedded Real-Time Operating System (RTOS) developed by Wind River Systems since 1983. It has become one of the most trusted RTOS platforms, known for its:

  • Deterministic performance
  • High reliability
  • Rich development tools

VxWorks is used in mission-critical systems, including:

  • Aerospace and Defense: F-16, FA-18, B-2, Patriot Missiles
  • Space Exploration: Mars Rovers (Sojourner, Phoenix, Curiosity)
  • Industrial and Medical Applications

Its robust kernel and proven performance make it an ideal platform for systems that demand precision, safety, and real-time responsiveness.

πŸ”Œ Serial Port Overview
#

A serial port (often referred to as a COM port) is a communication interface that transfers data bit by bit over a single channel. It’s widely used in embedded systems for connecting sensors, controllers, and peripheral devices.

Advantages:

  • Simple wiring (only TX/RX lines required)
  • Cost-effective and reliable
  • Ideal for long-distance communication

Common standards:

  • RS-232 β€” general PC communication (9-pin or 25-pin)
  • RS-422 / RS-485 β€” industrial applications, supporting long-distance and multi-drop connections

Quick Loopback Test Tip:
If using a 9-pin RS-232 connector, connect Pin 2 and Pin 3 together for a simple loopback test. Also connect Pin 5 (ground) to ensure accurate data transmission.

🧰 RS-232 Wiring Methods
#

RS-232 cables can be configured in two ways:

  • Straight-through: used between a PC and a device
  • Crossover: used between two devices

Choose the cable type based on your communication setup.

πŸ’» VxWorks Serial Programming Essentials
#

Include Headers
#

#include "vxWorks.h"
#include "stdIo.h"
#include "ioLib.h"
#include "sysLib.h"
#include "string.h"
#include "taskLib.h"

Serial Port Configuration
#

ioctl(m_SeriPort, SIO_HW_OPTS_SET, CLOCAL | CS8 | PARODD | PARENB);  // 8 data bits | 1 stop bit | even parity
ioctl(m_SeriPort, FIOBAUDRATE, 9600);                                // Baud rate 9600
ioctl(m_SeriPort, FIOSETOPTIONS, OPT_RAW);                           // Raw mode
ioctl(m_SeriPort, FIOFLUSH, 0);                                      // Flush buffers

Opening the Serial Port
#

#define SERI_NAME "/tyCo/0"
int m_SeriPort = open(SERI_NAME, O_RDWR, 0);

Writing Data
#

char* sendData;
int writeCom = write(m_SeriPort, sendData, strlen(sendData));

Reading Data
#

char data;
int readCom = read(m_SeriPort, &data, 1);

🧩 Integrating Qt with VxWorks UART
#

Thread Class β€” Serial Port Handler
#

#ifndef THREAD_H
#define THREAD_H

#include <QThread>
#include <QDebug>
#include "vxWorks.h"
#include "stdIo.h"
#include "ioLib.h"
#include "sysLib.h"
#include "string.h"
#include "taskLib.h"

class Thread : public QThread
{
    Q_OBJECT
public:
    explicit Thread(QObject *parent = 0);
    ~Thread();
    void run();
    bool openSeri(QString comPort, int baudRate);
    void closeSeri();
    void writeSeri(char* sendData);
    void setFlag(bool flag = true);

signals:
    void RecvData(char data);

private:
    bool seriStop;
    int m_SeriPort;
    QString m_SeriName;
    int m_baud;
};
#endif // THREAD_H

Thread Implementation
#

#include "thread.h"

Thread::Thread(QObject *parent) : QThread(parent) {}
Thread::~Thread() {}

void Thread::run()
{
    sysClkRateSet(1000);
    char rData;
    while (1)
    {
        int readCom = read(m_SeriPort, &rData, 1);
        if (readCom > 0)
        {
            printf("%c\n", rData);
            emit RecvData(rData);
            if (!seriStop)
                break;
        }
        else
        {
            taskDelay(10);
        }
    }
}

bool Thread::openSeri(QString comPort, int baudRate)
{
    this->m_SeriName = comPort;
    this->m_baud = baudRate;
    m_SeriPort = open(comPort.toUtf8().data(), O_RDWR, 0);

    if (m_SeriPort == ERROR)
    {
        qDebug() << "Open failed:" << comPort;
        return false;
    }

    ioctl(m_SeriPort, SIO_HW_OPTS_SET, CLOCAL | CS8 | PARODD | PARENB);
    ioctl(m_SeriPort, FIOBAUDRATE, baudRate);
    ioctl(m_SeriPort, FIOSETOPTIONS, OPT_RAW);
    ioctl(m_SeriPort, FIOFLUSH, 0);

    qDebug() << "Open succeeded:" << comPort;
    return true;
}

void Thread::closeSeri()
{
    if (!seriStop)
    {
        close(m_SeriPort);
    }
}

void Thread::writeSeri(char* sendData)
{
    if (m_SeriPort == ERROR)
        openSeri(m_SeriName, m_baud);

    write(m_SeriPort, sendData, strlen(sendData));
}

void Thread::setFlag(bool flag)
{
    seriStop = flag;
}

Seri Class β€” User Interface Layer
#

#ifndef SERI_H
#define SERI_H

#include <QObject>
#include <QDebug>
#include "thread.h"

class Seri : public QObject
{
    Q_OBJECT
public:
    explicit Seri(QObject *parent = 0);
    ~Seri();
    bool open_Seri(QString comName, int comBaud);
    void write_Seri(QByteArray comData);
    void close_Seri();

signals:
    void send_Seri(char data);

private:
    Thread* m_pThread;
};
#endif // SERI_H
#include "Seri.h"

Seri::Seri(QObject *parent) : QObject(parent)
{
    m_pThread = new Thread;
}

Seri::~Seri()
{
    delete m_pThread;
}

bool Seri::open_Seri(QString comName, int comBaud)
{
    if (m_pThread->openSeri(comName, comBaud))
    {
        m_pThread->setFlag(true);
        m_pThread->start();
        return true;
    }
    return false;
}

void Seri::write_Seri(QByteArray comData)
{
    m_pThread->writeSeri(comData.data());
}

void Seri::close_Seri()
{
    if (m_pThread->isRunning())
    {
        m_pThread->setFlag(false);
        m_pThread->closeSeri();
        m_pThread->quit();
        m_pThread->wait();
    }
}

🧩 Code Structure Summary
#

Class Purpose Description
Thread Serial Communication Thread Handles UART configuration, data transmission, and reception.
Seri Application Interface Layer Manages thread lifecycle and integrates with Qt’s signals and slots.

🧭 Conclusion
#

This example shows how Qt and VxWorks can be combined to handle UART serial communication in embedded environments. By separating the serial logic (Thread) from the user interface (Seri), developers can build responsive, multi-threaded, and reliable communication applications on real-time systems.

Related

Qt 5.15.10 Released for VxWorks
·297 words·2 mins
VxWorks Qt UI Framework
RTOS Containers for the Intelligent Edge
·567 words·3 mins
VxWorks Container
Meet the VxWorks RTOS
·14 words·1 min
VxWorks VxWorks 7