📄 xcom.cpp
字号:
#include"xcom.h"
#include <QtGui/QSpinBox>
#include<QString>
#include<QMessageBox>
Login::Login()
{
setupUi(this);
this->setWindowTitle(QString::fromLocal8Bit("串口通讯"));
init_com(); //载入com口信息
port = new Win_QextSerialPort();
lineEdit->setReadOnly(TRUE);
lineEdit_2->setReadOnly(TRUE);
bytesRced = bytesSend = 0;
looptime = 1000;
thread1 = new comrecover(looptime);
thread2 = new comtest(port);
lineEdit->setText(QString::number(bytesSend));
lineEdit_2->setText(QString::number(bytesRced));
lineEdit_3->setText(QString::number(looptime));
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
connect(pushButton, SIGNAL(clicked()), this, SLOT(opencom_port()));
connect(pushButton_4, SIGNAL(clicked()), this, SLOT(reset_number()));
connect(pushButton_2, SIGNAL(clicked()), this, SLOT(reset_recever()));
connect(pushButton_5, SIGNAL(clicked()), this, SLOT(sendmessage()));
connect(pushButton_6, SIGNAL(clicked()), this, SLOT(loopsend()));
QObject::connect(thread1, SIGNAL(pp()),this,SLOT(sendmessage()));
connect(lineEdit_3, SIGNAL(textChanged(const QString &)), thread1, SLOT(lptime(const QString &)));
QObject::connect(thread2, SIGNAL(readsignal()),this,SLOT(updatemessage()));
connect(comboBox_2, SIGNAL(currentIndexChanged ( const QString & )), this, SLOT(comboBox_BaudRate_currentIndexChanged(const QString &)));
connect(comboBox_3, SIGNAL(currentIndexChanged ( const QString & )), this, SLOT(comboBox_Parity_currentIndexChanged(const QString &)));
connect(comboBox_4, SIGNAL(currentIndexChanged ( const QString & )), this, SLOT(comboBox_DataBits_currentIndexChanged(const QString &)));
connect(comboBox_5, SIGNAL(currentIndexChanged ( const QString & )), this, SLOT(comboBox_StopBits_currentIndexChanged(const QString &)));
connect(comboBox_6, SIGNAL(currentIndexChanged ( const QString & )), this, SLOT(comboBox_ControlFlow_currentIndexChanged(const QString &)));
}
void Login::init_com()
{
QString path="HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOMM";
QSettings *settings=new QSettings(path,QSettings::NativeFormat);
QStringList key=settings->allKeys();
QStringList comlist ; //comX 表
QStringList Baudlist ;
QStringList Paritylist ;
QStringList DataBitslist;
QStringList StopBitslist;
QStringList ControlFlowlist;
int kk = key.size();
int i;
comlist.clear();
for(i=0;i<kk;i++)
{
comlist << getcomm(i,"value");
}
comboBox->addItems(comlist);
Baudlist.clear();
Baudlist<< "300" << "600" << "1200" << "2400" << "4800" << "9600"
<< "19200" << "38400" << "56000" << "57600" << "115200";
comboBox_2->addItems(Baudlist);
Paritylist.clear();
Paritylist<< "NONE" << "ODD" << "EVEN";
comboBox_3->addItems(Paritylist);
DataBitslist.clear();
DataBitslist<< "8" << "7" << "6";
comboBox_4->addItems(DataBitslist);
StopBitslist.clear();
StopBitslist<< "1" << "2";
comboBox_5->addItems(StopBitslist);
ControlFlowlist.clear();
ControlFlowlist<< "NONE" << "XON/XOFF" << "Hard";
comboBox_6->addItems(ControlFlowlist);
qDebug("com list init!\n");
}
QString Login::getcomm(int index,QString keyorvalue)
{
QString commresult="";
QString strkey="HARDWARE\\DEVICEMAP\\SERIALCOMM";//子键路径
int a=strkey.toWCharArray(subkey);//类型转换
subkey[a]=L'\0';
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,subkey,0,KEY_READ|KEY_QUERY_VALUE,&hKey)!=0)
{
QString error="Cannot open regedit!";//无法打开注册表时返回error
}
QString keymessage="";//键名
QString message="";
QString valuemessage="";//键值
indexnum=index;//要读取键值的索引号
keysize=sizeof(keyname);
valuesize=sizeof(keyvalue);
if(::RegEnumValue(hKey,indexnum,keyname,&keysize,0,&type,(BYTE*)keyvalue,&valuesize)==0)//列举键名和值
{
for(int i=0;i<keysize;i++)
{
message=QString::fromStdWString(keyname);
keymessage.append(message);
}// for(int i=0;i<=keysize;i++) 读取键名
for(int j=0;j<valuesize;j++)
{
if(keyvalue[j]!=0x00)
{ valuemessage.append(keyvalue[j]);}
}//for(int j=0;j<valuesize;j++) 读取键值
if(keyorvalue=="key")
{
commresult=keymessage;
}
if(keyorvalue=="value")
{
commresult=valuemessage;
}
}
else
{
commresult="nokey";
}
::RegCloseKey(hKey);//关闭注册表
return commresult;
}
void Login::opencom_port()
{
if (port->isOpen())
{
pushButton->setText(QString::fromLocal8Bit("打开串口"));
port->close();
if(thread2->isRunning())
{
thread2->terminate();
qDebug("thread2 is terminate!\n");
}
}
else
{
pushButton->setText(QString::fromLocal8Bit("关闭串口"));
QString port_name = comboBox->currentText();
QString kk = port_name.right(port_name.length()-3);
if(kk.toInt()>9)
{
QString xx = "\\\\.\\";
xx.append(port_name);
port->setPortName(xx);
}
else
{
port->setPortName(port_name);
}
port->open(QIODevice::ReadWrite);
if (!port->isOpen())
{
QMessageBox::critical(this, QString::fromLocal8Bit("错误"),
QString::fromLocal8Bit("无法打开串口: ") + port->portName()
+ QString::fromLocal8Bit("\n指定的串口不存在或者被占用。"));
pushButton->setText(QString::fromLocal8Bit("打开串口"));
}
else
{
pushButton->setText(QString::fromLocal8Bit("关闭串口"));
thread2->start();
}
}
bytesRced = bytesSend = 0;
lineEdit->setText(QString::number(bytesSend));
lineEdit_2->setText(QString::number(bytesRced));
}
void Login::comboBox_BaudRate_currentIndexChanged(const QString &baud)
{
qDebug("BaudRate is change!\n");
int i = baud.toInt();
switch(i)
{
case 300:
port->setBaudRate(BAUD300);
break;
case 600:
port->setBaudRate(BAUD600);
break;
case 1200:
port->setBaudRate(BAUD1200);
break;
case 2400:
port->setBaudRate(BAUD2400);
break;
case 4800:
port->setBaudRate(BAUD4800);
break;
case 9600:
port->setBaudRate(BAUD9600);
break;
case 19200:
port->setBaudRate(BAUD19200);
break;
case 38400:
port->setBaudRate(BAUD38400);
break;
case 56000:
port->setBaudRate(BAUD56000);
break;
case 57600:
port->setBaudRate(BAUD57600);
break;
case 115200:
port->setBaudRate(BAUD115200);
break;
}
}
void Login::comboBox_Parity_currentIndexChanged(const QString & strPar)
{
qDebug("Parity is change!\n");
if (strPar == "NONE")
port->setParity(PAR_NONE);
else
if (strPar == "ODD")
port->setParity(PAR_ODD);
else
if (strPar == "EVEN")
port->setParity(PAR_EVEN);
}
void Login::comboBox_DataBits_currentIndexChanged(const QString &strDBits)
{
qDebug("DataBits is change!\n");
if (strDBits == "8")
port->setDataBits(DATA_8);
else
if (strDBits == "7")
port->setDataBits(DATA_7);
else
if (strDBits == "6")
port->setDataBits(DATA_6);
}
void Login::comboBox_StopBits_currentIndexChanged(const QString &strStop)
{
qDebug("StopBits is change!\n");
if (strStop == "1")
port->setStopBits(STOP_1);
else
if (strStop == "2")
port->setStopBits(STOP_2);
}
void Login::comboBox_ControlFlow_currentIndexChanged(const QString &strFlow)
{
qDebug("ControlFlow is change!\n");
if (strFlow == "NONE")
port->setFlowControl(FLOW_OFF);
else
if (strFlow == "Hard")
port->setFlowControl(FLOW_HARDWARE);
else
if (strFlow == "XON/XOFF")
port->setFlowControl(FLOW_XONXOFF);
}
void Login::reset_number()
{
bytesRced = bytesSend = 0;
lineEdit->setText(QString::number(bytesSend));
lineEdit_2->setText(QString::number(bytesRced));
}
void Login::reset_recever()
{
textBrowser->clear();
}
void Login::updatemessage()
{
char buff[1024];
int numBytes;
numBytes = port->bytesAvailable();
if(numBytes > 0)
{
if(numBytes > 1024) numBytes = 1024;
int i = port->read(buff, numBytes);
buff[i] = '\0';
QString msg = buff;
textBrowser->insertPlainText(msg);
textBrowser->moveCursor((QTextCursor::End));
}
bytesRced++;
lineEdit_2->setText(QString::number(bytesRced));
}
void Login::sendmessage()
{
QString textmessage;
if (!port->isOpen())
{
QMessageBox::critical(this, QString::fromLocal8Bit("错误"),
QString::fromLocal8Bit("串口没有打开: ") + comboBox->currentText());
return;
}
textmessage = textEdit->toPlainText();
port->write(textmessage.toAscii(),textmessage.length());
bytesSend++;
lineEdit->setText(QString::number(bytesSend));
}
void Login::loopsend()
{
// looptime = lineEdit_3->text().toInt();
if(!thread1->isRunning())
{
thread1->start();
}
else
{
thread1->terminate();
qDebug("thread1 is terminate!\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -