qt写linux上位机,用qt写的一个简单到不能在简单的上位机

#include "mainwindow.h"

#include "ui_mainwindow.h"

#include

MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),

ui(new Ui::MainWindow)

{

ui->setupUi(this);

Pic = tr("://Resource/关灯.jpg"); //放置一张图片

ui->lightLabel->setPixmap(Pic);

ui->RTextEdit->setReadOnly(false);

ui->STextEdit->setReadOnly(false);

ui->onButton->setEnabled(true);//默认关灯

ui->offButton->setEnabled(false);

ui->lightDial->setEnabled(false);

serial =new QSerialPort(this);

OpenFlag =false;//串口未打开

connect(ui->lightDial, SIGNAL(valueChanged(int)),this, SLOT(on_light_chance()));

UsartUi();

}

MainWindow::~MainWindow()

{

delete ui;

}

void MainWindow::UsartUi()//完善串口配置UI

{

ui->parityBox->addItem(QStringLiteral("None"),QSerialPort::NoParity);

ui->parityBox->addItem(QStringLiteral("偶校验"),QSerialPort::EvenParity);

ui->parityBox->addItem(QStringLiteral("奇校验"),QSerialPort::OddParity);

ui->parityBox->addItem(QStringLiteral("Mark  1"),QSerialPort::MarkParity);

ui->parityBox->addItem(QStringLiteral("Space 0"),QSerialPort::SpaceParity);

ui->baundRateBox->setItemData(0,QSerialPort::Baud4800);

ui->baundRateBox->setItemData(1,QSerialPort::Baud9600);

ui->baundRateBox->setItemData(2,QSerialPort::Baud19200);

ui->baundRateBox->setItemData(3,QSerialPort::Baud38400);

ui->baundRateBox->setItemData(4,QSerialPort::Baud57600);

ui->baundRateBox->setItemData(5,QSerialPort::Baud115200);

ui->dataBitBox->setItemData(0,QSerialPort::Data5);

ui->dataBitBox->setItemData(1,QSerialPort::Data6);

ui->dataBitBox->setItemData(2,QSerialPort::Data7);

ui->dataBitBox->setItemData(3,QSerialPort::Data8);

ui->stopBitBox->setItemData(0,QSerialPort::OneStop);

ui->stopBitBox->setItemData(1,QSerialPort::OneAndHalfStop);

ui->stopBitBox->setItemData(2,QSerialPort::TwoStop);

PortInfo();

}

void MainWindow::PortInfo()

{

ui->uNameBox->clear();

foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {

QStringList list;

list << info.portName()

<< info.description()

<< info.manufacturer()

<< info.systemLocation()

<< (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString())

<< (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : QString());

ui->uNameBox->addItem(list.first(), list);

}

}

//槽函数

void MainWindow::on_startButton_clicked()//打开串口

{

if(!OpenFlag)//执行打开串口

{

currentConfig.name = ui->uNameBox->currentText();

currentConfig.baudRate =static_cast

(ui->baundRateBox->itemData(ui->baundRateBox->currentIndex()).toInt());

currentConfig.dataBits =static_cast

(ui->dataBitBox->itemData(ui->dataBitBox->currentIndex()).toInt());

currentConfig.stopBits =static_cast

(ui->stopBitBox->itemData(ui->stopBitBox->currentIndex()).toInt());

currentConfig.parity =static_cast

(ui->parityBox->itemData(ui->parityBox->currentIndex()).toInt());

serial->setPortName(currentConfig.name);

if(serial->open(QIODevice::ReadWrite))

{

if( serial->setBaudRate(currentConfig.baudRate)

&& serial->setDataBits(currentConfig.dataBits)

&& serial->setStopBits(currentConfig.stopBits)

&& serial->setParity(currentConfig.parity) )

{

OpenFlag =true;

ui->startButton->setText(tr("关闭串口"));

}

else

{

serial->close();

OpenFlag =false;

ui->startButton->setText(tr("打开串口"));

}

}

else

{

OpenFlag =false;

ui->startButton->setText(tr("打开串口"));

}

}

else

{

OpenFlag =false;

ui->startButton->setText(tr("打开串口"));

serial->close();

}

/*    ui->STextEdit->setText(tr("校验位:%1 \n波特率:%2 \n数据位:%3 \n停止位:%4")

.arg(ui->parityBox->itemData(ui->parityBox->currentIndex()).toString())

.arg(ui->baundRateBox->itemData(ui->baundRateBox->currentIndex()).toString())

.arg(ui->dataBitBox->itemData(ui->dataBitBox->currentIndex()).toString())

.arg(ui->stopBitBox->itemData(ui->stopBitBox->currentIndex()).toString()));

*/

}

void MainWindow::on_offButton_clicked()//关灯

{

QByteArray data;

QString Pdata;

data.resize(3);

data[0] = 0xF0;

data[1] = 0X0F;

data[2] = 0x80;

ui->offButton->setEnabled(false);

ui->onButton->setEnabled(true);

ui->lightDial->setValue(1);

ui->lightDial->setEnabled(false);

Pdata = data.toHex().toUpper();

int Size = Pdata.size();

for(int i = Size; i > 0; i--)

{

Pdata.insert(i * 2," ");

}

ui->STextEdit->insertPlainText(Pdata);

//发送数据

serial->write(data);

}

void MainWindow::on_onButton_clicked()//开灯  0XF0 0X0F 0X81

{

QByteArray data;

QString Pdata;

data.resize(3);

data[0] = 0xF0;

data[1] = 0X0F;

data[2] = 0X81;

ui->onButton->setEnabled(false);

ui->offButton->setEnabled(true);

ui->lightDial->setEnabled(true);

Pdata = data.toHex().toUpper();

int Size = Pdata.size() / 2;

for(int i = Size; i > 0; i--)

{

Pdata.insert(i * 2," ");

}

ui->STextEdit->insertPlainText(Pdata);

//发送数据

serial->write(data);

}

void MainWindow::on_light_chance()

{

QByteArray data;

QString Pdata;

data.resize(5);

data[0] = 0xF0;

data[1] = 0x0F;

data[2] = 0xC0;

data[3] = 0x01;

data[4] = ui->lightDial->value();

Pdata = data.toHex().toUpper();

int Size = Pdata.size() / 2;

for(int i = Size; i > 0; i--)

{

Pdata.insert(i * 2," ");

}

ui->STextEdit->insertPlainText(Pdata);

serial->write(data);

}

void MainWindow::on_freshButton_clicked()//刷新状态

{

QByteArray Rdata = serial->readAll();

QString Pdata = Rdata.toHex().toUpper();//转换成HEX格式且大写

int Size = Pdata.size()/2;

for(int i = Size; i > 0; i--)//插入空格 使格式如 xx xx xx

{

Pdata.insert(i * 2," ");

}

ui->RTextEdit->insertPlainText(Pdata);

int i = 0;

if(Rdata.at(i) && 0xF0 == 0xF0)

{

i++;

if(Rdata.at(i) && 0x0F == 0x0F)

{

while(Rdata.at(i) !='\0')//解析指令

{

if(Rdata.at(i) && 0x80 == 0x80)currentStatus.lightOnOff =false;

if(Rdata.at(i) && 0x81 == 0x81)currentStatus.lightOnOff =true;

if(Rdata.at(i) && 0xc0 == 0xc0)

{

i = i + 2;

currentStatus.lightValue = Rdata.at(i);

}

i++;

}

}

}

if(currentStatus.lightOnOff)Pic = tr("://Resource/开灯.jpg");

else Pic = tr("://Resource/关灯.jpg");

ui->lightLabel->setPixmap(Pic);

ui->lightLcdNumber->display(currentStatus.lightValue);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值