很高兴为您提供帮助。下面是一个基于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”命令的字符串。如果发送成功,则在控制台输出所写入的字节数。
这只是一个基础示例代码,您可以按照您自己的需要进行修改和扩展。希望对您有所帮助!