mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "win_qextserialport.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
Win_QextSerialPort *myCom;//声明对象
private slots:
void readMycom();
void on_openMyComBtn_clicked();
void on_closeMyComBtn_clicked();
void on_sendMsgBtn_clicked();
char ConvertHexChar(char ch);
void StringToHex(QString str, QByteArray & senddata);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#pragma execution_character_set("utf-8")
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->closeMyComBtn->setEnabled(false);//开始“关闭串口”按钮不可用
ui->sendMsgBtn->setEnabled(false);//开始“发送数据”按钮不可用
}
void MainWindow::readMycom()//读串口函数
{
//QByteArray temp = myCom->readAll();
//读取串口的数据显示在窗口的文本浏览器
//ui->textBrowser->insertPlainText(temp);
//将串口的数据显示在窗口文本浏览器中
int isHexRead=ui->isHexRead->isChecked();
if(!isHexRead)
{
QByteArray temp = myCom->readAll();
ui->textBrowser->insertPlainText(temp);
/*QByteArray buf;
buf = myCom->readAll();
if(buf!=NULL)
{
QString str = ui->textBrowser->toPlainText();
str+=tr(buf);//累加接收的数据
ui->textBrowser->clear();
ui->textBrowser->append(str);//附加str字符串值
}*/
}
if(isHexRead)
{
QByteArray temp = myCom->readAll();
QDataStream out(&temp,QIODevice::ReadWrite); //将字节数组读入
while(!out.atEnd())
{
qint8 outChar = 0;
out>>outChar; //每字节填充一次,直到结束
//十六进制的转换
QString str = QString("%1").arg(outChar & 0xFF,2,16,QLatin1Char('0'));
ui->textBrowser->insertPlainText(str.toUpper());//大写
ui->textBrowser->insertPlainText(" ");//每发送两个字符后添加一个空格
ui->textBrowser->moveCursor(QTextCursor::End);
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_openMyComBtn_clicked()
{
QString portName = ui->portNameComboBox->currentText();//获取串口名
myCom = new Win_QextSerialPort(portName,QextSerialBase::EventDriven);
//定义串口对象,并传递参数,在构造函数里对其进行初始化
myCom->open(QIODevice::ReadWrite);
//以可读写方式打开串口
if(ui->baudRateComboBox->currentText()==tr("9600"))//根据组合框内容对串口内容进行设置
myCom->setBaudRate(BAUD9600);
else if(ui->baudRateComboBox->currentText()==tr("115200"))
myCom->setBaudRate(BAUD115200);
if(ui->dataBitsComboBox->currentText()==tr("8"))
myCom->setDataBits(DATA_8);
else if(ui->dataBitsComboBox->currentText()==tr("7"))
myCom->setDataBits(DATA_7);
if(ui->parityComboBox->currentText()==tr("无"))
myCom->setParity(PAR_NONE);
else if(ui->parityComboBox->currentData()==tr("奇"))
myCom->setParity(PAR_ODD);
else if(ui->parityComboBox->currentData()==tr("偶"))
myCom->setParity(PAR_EVEN);
if(ui->stopBitsComboBox->currentText()==tr("1"))
myCom->setStopBits(STOP_1);
if(ui->stopBitsComboBox->currentText()==tr("2"))
myCom->setStopBits(STOP_2);
myCom->setFlowControl(FLOW_OFF);
myCom->setTimeout(500);
connect(myCom,SIGNAL(readyRead()),this,SLOT(readMycom()));
//信号和槽函数关联,当串口缓冲区有数据时,进行串口操作
ui->openMyComBtn->setEnabled(false);//打开串口后“打开串口”按钮不可用
ui->closeMyComBtn->setEnabled(true);//打开串口后“关闭串口”按钮不可用
ui->sendMsgBtn->setEnabled(true);//打开串口后“发送数据”按钮可用
ui->baudRateComboBox->setEnabled(false);//设置组合框不能用
ui->dataBitsComboBox->setEnabled(false);//设置组合框不能用
ui->parityComboBox->setEnabled(false);//设置组合框不能用
ui->stopBitsComboBox->setEnabled(false);//设置组合框不能用
ui->portNameComboBox->setEnabled(false);//设置组合框不能用
}
void MainWindow::on_closeMyComBtn_clicked()
{
myCom->close();//关闭串口,在函数在win_qextserialport.cpp文件中定义
ui->openMyComBtn->setEnabled(true);//关闭串口后,“打开串口”按钮可用
ui->closeMyComBtn->setEnabled(false); //关闭串口后,“发送按钮”按钮不可用
ui->sendMsgBtn->setEnabled(false); //关闭串口后,“发送按钮”按钮不可用
ui->baudRateComboBox->setEnabled(true);//设置组合框能用
ui->dataBitsComboBox->setEnabled(true);//设置组合框能用
ui->parityComboBox->setEnabled(true);//设置组合框能用
ui->stopBitsComboBox->setEnabled(true);//设置组合框能用
ui->portNameComboBox->setEnabled(true);//设置组合框能用
}
void MainWindow::on_sendMsgBtn_clicked()
{
//myCom->write(ui->sendMsgLineEdit->text().toLatin1());
QString str =ui->sendMsgLineEdit->text().toLatin1();//从LineEdit得到字符串
if(!str.isEmpty())
{
int isHexSend=ui->isHexSend->isChecked();
int len =str.length();
if(len%2 == 1) //如果发送的数据个数为奇数的,则在前面最后落单的字符前添加一个字符0
{
str = str.insert(len-1,'0'); //insert(int position, const QString & str)
}
QByteArray senddata;
if(isHexSend)
{
StringToHex(str,senddata);//将str字符串转换为16进制的形式
myCom->write(senddata);//发送到串口
}
else
{
myCom->write(ui->sendMsgLineEdit->text().toLatin1());//发送到串口
}
}
}
void MainWindow::StringToHex(QString str, QByteArray & senddata) //字符串转换成16进制数据0-F
{
int hexdata,lowhexdata;
int hexdatalen = 0;
int len = str.length();
senddata.resize(len/2);
char lstr,hstr;
for(int i=0; i<len; )
{
//char lstr,
hstr=str[i].toLatin1();
if(hstr == ' ')
{
i++;
continue;
}
i++;
if(i >= len)
break;
lstr = str[i].toLatin1();
hexdata = ConvertHexChar(hstr);
lowhexdata = ConvertHexChar(lstr);
if((hexdata == 16) || (lowhexdata == 16))
break;
else
hexdata = hexdata*16+lowhexdata;
i++;
senddata[hexdatalen] = (char)hexdata;
hexdatalen++;
}
senddata.resize(hexdatalen);
}
char MainWindow::ConvertHexChar(char ch)
{
if((ch >= '0') && (ch <= '9'))
return ch-0x30;
else if((ch >= 'A') && (ch <= 'F'))
return ch-'A'+10;
else if((ch >= 'a') && (ch <= 'f'))
return ch-'a'+10;
// else return (-1);
else return ch-ch;//不在0-f范围内的会发送成0
}