MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow), Serial(NULL)
{
ui->setupUi(this);
connect(ui->ledBut, SIGNAL(clicked(bool)), this, SLOT(ledOnOff(bool)));
setup();
}
MainWindow::~MainWindow()
{
delete ui;
Serial->close();
delete Serial;
Serial = NULL;
}
void MainWindow::ledOnOff(bool on)
{
int write = -1;
if(on)
{
write = 1;
ui->ledBut->setText("OFF");
}
else
{
write = 0;
ui->ledBut->setText("OFF");
}
if(Serial
->isOpen
() || this
->Serial
->open
(QIODevice::ReadWrite)) {
this->Serial->setBaudRate(QSerialPort::Baud9600);
this->Serial->setParity(QSerialPort::NoParity);
this->Serial->setStopBits(QSerialPort::OneStop);
this->Serial->setFlowControl(QSerialPort::NoFlowControl);
this->Serial->setDataBits(QSerialPort::Data8);
dayArray[0]= write;
qDebug() << "Written Bytes : " << this->Serial->write(dayArray);
this->Serial->waitForBytesWritten(-1);
}
else
{
qDebug() << "Failled open com port";
}
}
void MainWindow::onReadyRead()
{
int a = Serial->bytesAvailable();
bytes.resize(a);
Serial->read(bytes.data(), bytes.size());
qDebug() << "bytes read:" << bytes.size() << " Written data = " << bytes.data();
}
void MainWindow::errorReport(QSerialPort::SerialPortError error)
{
if(error!=0)
qDebug()<<"ERROR:"<<endl<<error;
}
void MainWindow::setup()
{
if(!Serial)
{
this->Serial=new QSerialPort(this);
this->Serial->setPortName("COM6");
this->Serial->setBaudRate(QSerialPort::Baud9600);
connect(this->Serial,SIGNAL(error(QSerialPort::SerialPortError)),this,SLOT(errorReport(QSerialPort::SerialPortError)));
connect(Serial, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
}
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow), Serial(NULL)
{
ui->setupUi(this);
connect(ui->ledBut, SIGNAL(clicked(bool)), this, SLOT(ledOnOff(bool)));
setup();
}
MainWindow::~MainWindow()
{
delete ui;
Serial->close();
delete Serial;
Serial = NULL;
}
void MainWindow::ledOnOff(bool on)
{
int write = -1;
if(on)
{
write = 1;
ui->ledBut->setText("OFF");
}
else
{
write = 0;
ui->ledBut->setText("OFF");
}
if(Serial->isOpen() || this->Serial->open(QIODevice::ReadWrite))
{
this->Serial->setBaudRate(QSerialPort::Baud9600);
this->Serial->setParity(QSerialPort::NoParity);
this->Serial->setStopBits(QSerialPort::OneStop);
this->Serial->setFlowControl(QSerialPort::NoFlowControl);
this->Serial->setDataBits(QSerialPort::Data8);
QByteArray dayArray;
dayArray[0]= write;
qDebug() << "Written Bytes : " << this->Serial->write(dayArray);
this->Serial->waitForBytesWritten(-1);
}
else
{
qDebug() << "Failled open com port";
}
}
void MainWindow::onReadyRead()
{
QByteArray bytes;
int a = Serial->bytesAvailable();
bytes.resize(a);
Serial->read(bytes.data(), bytes.size());
qDebug() << "bytes read:" << bytes.size() << " Written data = " << bytes.data();
}
void MainWindow::errorReport(QSerialPort::SerialPortError error)
{
if(error!=0)
qDebug()<<"ERROR:"<<endl<<error;
}
void MainWindow::setup()
{
if(!Serial)
{
this->Serial=new QSerialPort(this);
this->Serial->setPortName("COM6");
this->Serial->setBaudRate(QSerialPort::Baud9600);
connect(this->Serial,SIGNAL(error(QSerialPort::SerialPortError)),this,SLOT(errorReport(QSerialPort::SerialPortError)));
connect(Serial, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks