QT开发中文网

基于qt写一个串口通信的电子开关控制线路界面

很高兴为您提供帮助。下面是一个基于Qt的串口通信的电子开关控制线路界面的示例代码:

#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QHBoxLayout>
#include <QSerialPort>
#include <QSerialPortInfo>

class SerialSwitch : public QWidget
{
    Q_OBJECT

public:
    SerialSwitch(QWidget *parent = nullptr);
    ~SerialSwitch();

private slots:
    void onConnectClicked();
    void onDisconnectClicked();
    void onSendClicked();

private:
    QPushButton *m_connectButton;
    QPushButton *m_disconnectButton;
    QPushButton *m_sendButton;

    QSerialPort *m_serialPort;

};

SerialSwitch::SerialSwitch(QWidget *parent)
    : QWidget(parent)
{
    m_connectButton = new QPushButton("Connect");
    connect(m_connectButton, SIGNAL(clicked()), this, SLOT(onConnectClicked()));

    m_disconnectButton = new QPushButton("Disconnect");
    m_disconnectButton->setEnabled(false);
    connect(m_disconnectButton, SIGNAL(clicked()), this, SLOT(onDisconnectClicked()));

    m_sendButton = new QPushButton("Send");
    m_sendButton->setEnabled(false);
    connect(m_sendButton, SIGNAL(clicked()), this, SLOT(onSendClicked()));

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(m_connectButton);
    layout->addWidget(m_disconnectButton);
    layout->addWidget(m_sendButton);

}

void SerialSwitch::onConnectClicked()
{
// Get a list of available serial ports
QList<QSerialPortInfo> portInfos = QSerialPortInfo::availablePorts();

if (portInfos.isEmpty()) {
        qDebug() << "No serial ports found";
        return;
}

// Open the first available port with the following settings
QSerialPortInfo portInfo = portInfos.first();
qDebug() << "Using port: " << portInfo.portName();
m_serialPort = new QSerialPort(portInfo);
m_serialPort->setBaudRate(QSerialPort::Baud9600);
m_serialPort->setDataBits(QSerialPort::Data8);
m_serialPort->setParity(QSerialPort::NoParity);
m_serialPort->setStopBits(QSerialPort::OneStop);

if (!m_serialPort->open(QIODevice::ReadWrite)) {
        qDebug() << "Failed to open port";
        return;
}

// Enable/disable buttons
m_connectButton->setEnabled(false);
m_disconnectButton->setEnabled(true);
m_sendButton->setEnabled(true);

qDebug() << "Connected to serial port";

}

void SerialSwitch::onDisconnectClicked()
{
    m_serialPort->close();
    delete m_serialPort;

    // Enable/disable buttons
    m_connectButton->setEnabled(true);
    m_disconnectButton->setEnabled(false);
    m_sendButton->setEnabled(false);

    qDebug() << "Disconnected from serial port";
}

void SerialSwitch::onSendClicked()
{
// Send the command over serial
QString command = "ON\r\n";
qDebug() << "Sending command: " << command;
qint64 bytesWritten = m_serialPort->write(command.toUtf8());

if (bytesWritten == -1) {
        qDebug() << "Failed to write data to serial port";
        return;
}

qDebug() << bytesWritten << " bytes written to serial port";
}

SerialSwitch::~SerialSwitch()
{

}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

SerialSwitch w;
w.show();

return a.exec();
}

这个示例代码实现了一个简单的串口通信电子开关控制线路界面,它包括了连接、断开和发送命令三个按钮。当用户点击“Connect”按钮时,程序会打开第一个可用的串口,并将其设置为9600波特率、8数据位、无奇偶校验和1停止位。当串口连接成功后,“Connect”按钮将变为不可用状态,而“Disconnect”和“Send”按钮则变为可用状态。

当用户点击“Disconnect”按钮时,程序将关闭当前打开的串口,并将所有按钮设置为默认状态。

最后,当用户点击“Send”按钮时,程序会向串口发送一条带有“ON\r\n”命令的字符串。如果发送成功,则在控制台输出所写入的字节数。

这只是一个基础示例代码,您可以按照您自己的需要进行修改和扩展。希望对您有所帮助!

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=1924

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?