//功能函数
#include "Widget.h"
#include "ui_Widget.h"
#include
#include
//QStringToHex
void Widget::ToHex(QString &str, QByteArray &byteData)
{
int hexdata,lowhexdata;
int hexdatalen = 0;
int len = str.length();
byteData.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 = convertCharToHex(hstr);
lowhexdata = convertCharToHex(lstr);
if((hexdata == 16) || (lowhexdata == 16))
break;
else
hexdata = hexdata*16+lowhexdata;
i++;
byteData[hexdatalen] = (char)hexdata;
hexdatalen++;
}
byteData.resize(hexdatalen);
}
char Widget::convertCharToHex(char ch)
{
/*
0x30等于十进制的48,48也是0的ASCII值,,
1-9的ASCII值是49-57,,所以某一个值-0x30,,
就是将字符0-9转换为0-9
*/
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);
}
void Widget::insertSpaceDataHandle(QString &strHexRcvData)
{
int strCount =strHexRcvData.length();
if(strHexRcvData.length()>2)
{
for(int i = 0 ; i < strCount/2-1 ; i++ )
{
strHexRcvData.insert(3*i+2," ");
}
}
}
//功能函数
#include "Widget.h"
#include "ui_Widget.h"
#include
#include
//QStringToHex
void Widget::ToHex(QString &str, QByteArray &byteData)
{
int hexdata,lowhexdata;
int hexdatalen = 0;
int len = str.length();
byteData.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 = convertCharToHex(hstr);
lowhexdata = convertCharToHex(lstr);
if((hexdata == 16) || (lowhexdata == 16))
break;
else
hexdata = hexdata*16+lowhexdata;
i++;
byteData[hexdatalen] = (char)hexdata;
hexdatalen++;
}
byteData.resize(hexdatalen);
}
char Widget::convertCharToHex(char ch)
{
/*
0x30等于十进制的48,48也是0的ASCII值,,
1-9的ASCII值是49-57,,所以某一个值-0x30,,
就是将字符0-9转换为0-9
*/
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);
}
void Widget::insertSpaceDataHandle(QString &strHexRcvData)
{
int strCount =strHexRcvData.length();
if(strHexRcvData.length()>2)
{
for(int i = 0 ; i < strCount/2-1 ; i++ )
{
strHexRcvData.insert(3*i+2," ");
}
}
}
//
//亮度检测
//
#include "Widget.h"
#include "ui_Widget.h"
#include
#include
//点亮度数据信息提取(接收数据,提取点信息,当前操作点)
void Widget::rcvDataHandle(QString &rcvData_S, QString &rcvPoint, int rcv_Point)
{
int rcv_Red,rcv_Green,rcv_Blue;
QString str;
rcvPoint = rcvData_S.remove(" ").mid((rcv_Point-1)*RCV_COUNTPOINTDATA,RCV_COUNTPOINTDATA);
//ui->textEditRXD->append("测试"+rcvPoint);
int count = pointSize * pointSize ;
rcv_Red = rcvPoint.mid(RCV_RED,RCV_COUNTCOLORINFOMA).toInt(&ok,16)/count;
rcv_Green = rcvPoint.mid(RCV_GREEN,RCV_COUNTCOLORINFOMA).toInt(&ok,16)/count;
//ui->textEditRXD->append("测试"+QString::number(rcv_Green));
rcv_Blue = rcvPoint.mid(RCV_BLUE,RCV_COUNTCOLORINFOMA).toInt(&ok,16)/count;
str = QString::number(rcv_Red,16);
addSpace(str,COUNTLEDCOLORDATA-str.length());
rcvPoint += str;
str = QString::number(rcv_Green,16);
addSpace(str,COUNTLEDCOLORDATA-str.length());
rcvPoint += str;
str = QString::number(rcv_Blue,16);
addSpace(str,COUNTLEDCOLORDATA-str.length());
rcvPoint += str;
//ui->textEditRXD->append(rcvPoint);
}
//接收的点信息,亮度标准(宏标准),误差
int Widget::judgementStandard(QString &pointInfom, long long macro, long long errorRange,int count)
{
QString str = QString::number(macro-errorRange,16);
addSpace(str,RCV_COUNTPOINTDATA-str.length());
//ui->textEditRXD->append(str);
if(QString::compare(pointInfom,str)>=0)
{
sendData_Str = sendData_Str.remove(" ");
insertSendPointInfom(sendData_Str,count,QString::number(GREENPOINT,16));
//ui->textEditTXD->setText(sendData_Str);
return QUALIF;
}else
{
insertSendPointInfom(sendData_Str,count,QString::number(REDPOINT,16));
return DISQUALIF;
}
}
void Widget::addSpace(QString &str,int num)
{
for(int i = 0 ; i < num ; i++)
{
str = "0" + str;
}
//ui->textEditRXD->append(str);
}
void Widget::changePointColor()
{
QString pointInfom;
if(rcvData_QStr.length()!=0)
{
rcvDataHandle(rcvData_QStr,pointInfom,points);
judgementStandard(pointInfom,R_QUALIF_LUMINANCE,R_QUALIF_LUMINANCE_ERROR,points);
}
}
#include "Widget.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
//
//对点的操作:
//1.上下左右移动
//2.点的颜色
//3.点的大小
#include "Widget.h"
#include "ui_Widget.h"
#include
#include
//上
void Widget::on_btnUp_clicked()
{
ctrlPoint(1,ui->sbPoints->value(),ui->hsldPointSize->value());
}
//下
void Widget::on_btnDown_clicked()
{
ctrlPoint(2,ui->sbPoints->value(),ui->hsldPointSize->value());
}
//左
void Widget::on_btnLeft_clicked()
{
ctrlPoint(3,ui->sbPoints->value(),ui->hsldPointSize->value());
}
//右
void Widget::on_btnRight_clicked()
{
ctrlPoint(4,ui->sbPoints->value(),ui->hsldPointSize->value());
}
void Widget::ctrlPoint(int num,int num1,int pointSize)//移动方向,第几个点,点的大小
{
if(!sendData_Str.isEmpty())
{
int n_crd = 0;
QString str,str1;
QString strTextEdit = sendData_Str.remove(" "). //strTextEdit包含 第num1点的 颜色 坐标
mid(COORDINATESDATASITE+(num1-1)*COUNTCOORDINATESDATA,COUNTCOORDINATESDATA);
QString strPointSize = QString::number(pointSize);
if(pointSize<10)
{
strPointSize = "0"+strPointSize;
}
//ui->textEditRXD->setText(strTextEdit);
switch (num)
{
case 1:
{
n_crd = (strTextEdit.mid(Y_SITE,3)).toInt(&ok,16);//点的Y坐标
n_crd --;
break;
}
case 2://下
{
n_crd = (strTextEdit.mid(Y_SITE,3)).toInt(&ok,16);//点的Y坐标
n_crd ++;//下
break;
}
case 3:
{
n_crd = (strTextEdit.mid(X_SITE,3)).toInt(&ok,16);//点的Y坐标
n_crd --;//下
break;
}
case 4:
{
n_crd = (strTextEdit.mid(X_SITE,3)).toInt(&ok,16);//点的Y坐标
n_crd ++;//下
break;
}
default:
break;
}
//判断QString类型的n_crd长度为几位
str1 = QString::number(n_crd,16);
if(str1.length()<3 && str1.length()>=2)
{
str1 = "0"+str1;
}
else if(str1.length()<2&& str1.length()>=1)
{
str1 = "00"+str1;
}
else if(str1.length()>=3)
{
str1 = str1.mid(str1.length()- 3, 3);
}
else
{
str1 = "000";
}
switch (num)
{
case 1:
str = sendData_Str.remove(" ")
.replace(COORDINATESDATASITE+(num1-1)*COUNTCOORDINATESDATA,COUNTCOORDINATESDATA,strTextEdit.replace(Y_SITE,3,str1));
break;
case 2:
str = sendData_Str
.replace(COORDINATESDATASITE+(num1-1)*COUNTCOORDINATESDATA,COUNTCOORDINATESDATA,strTextEdit.replace(Y_SITE,3,str1));
break;
case 3:
str = sendData_Str
.replace(COORDINATESDATASITE+(num1-1)*COUNTCOORDINATESDATA,COUNTCOORDINATESDATA,strTextEdit.replace(X_SITE,3,str1));
break;
case 4:
str = sendData_Str
.replace(COORDINATESDATASITE+(num1-1)*COUNTCOORDINATESDATA,COUNTCOORDINATESDATA,strTextEdit.replace(X_SITE,3,str1));
break;
default:
break;
}
str = str.replace(POINTSIZEDETASITE,COUNTPOINTSIZEDATA,strPointSize);//点的大小
sendData_Str = str;
on_btnTXD_clicked();
}
else
{
//
}
}
#include "Widget.h"
#include "ui_Widget.h"
#include
#include
//串口发送
void Widget::sendInfo(QString &info)
{
info = info.remove(" ");
QByteArray sendBuf;
insertSendPointInfom(info,pointSize);
ToHex(info, sendBuf);//
serial->write(sendBuf);
// insertSpaceDataHandle(info);
// ui->textEditRXD->append("#发送#:\t"+info);
}
//串口接收
void Widget::receiveInfo()
{
rcvData_Byt = serial->readAll();
}
void Widget::initTimer(int count)
{
fTimer = new QTimer(this);
fTimer->stop();
fTimer->setInterval(count);
connect(fTimer,SIGNAL(timeout()),this,SLOT(on_timer_timeout()));
}
void Widget::on_timer_timeout()
{
updataUiData();
changePointColor();
receiveInfo();//串口接收数据
}
//1.串口初始化
//2.初始化combox控件选项
//3.检测串口初始是打开还是关闭状态,如果串口打开了,就给它关掉
#include "Widget.h"
#include "ui_Widget.h"
#include
#include
#include
void Widget::portInit()
{
//查找可用串口
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
ui->cmbPortNum->addItem(info.portName());
// QSerialPort serial;
// serial.setPort(info);
// if(serial.open(QIODevice::ReadWrite))
// {
// ui->comboBoxPortNum->addItem(serial.portName());
// serial.close();
// }
}
//设置波特率选项
QMetaEnum baudRate = QMetaEnum::fromType<QSerialPort::BaudRate>();
for(int i = 0;i < baudRate.keyCount()-1;i++)
{
ui->cmbBaud->addItem(baudRate.key(i));
}
//设置默认波特率为115200
ui->cmbBaud->setCurrentText(baudRate.valueToKey(QSerialPort::BaudRate::Baud115200));
//设置数据位选项
QMetaEnum dataBits =QMetaEnum::fromType<QSerialPort::DataBits>();
for(int i = 0;i< dataBits.keyCount()-1;i++)
{
ui->cmbDataBit->addItem(dataBits.key(i));
}
//设置默认数据位为8位
ui->cmbDataBit->setCurrentText(dataBits.valueToKey(QSerialPort::DataBits::Data8));
//设置校验位
QMetaEnum parity = QMetaEnum::fromType<QSerialPort::Parity>();
for (int i = 0; i < parity.keyCount(); ++i)
{
ui->cmbParityBit->addItem(parity.key(i));
}
//设置默认校验位为0
ui->cmbParityBit->setCurrentText(parity.valueToKey(QSerialPort::Parity::NoParity));
//设置停止位
QMetaEnum stopBits = QMetaEnum::fromType<QSerialPort::StopBits>();
for (int i = 0; i < stopBits.keyCount()-1; ++i)
{
ui->cmbStopBit->addItem(stopBits.key(i));
}
//设置默认停止位为1
ui->cmbStopBit->setCurrentText(stopBits.valueToKey(QSerialPort::StopBits::OneStop));
if(serial->isOpen())
{
serial->clear();
serial->close();
}
}
//
//1.串口打开
//2.串口关闭
#include "Widget.h"
#include "ui_Widget.h"
#include
#include
void Widget::on_btnPortStatus_clicked()
{
serial->clear();
serial->close();
if(!serial->isOpen())
{
serial->setPortName(ui->cmbPortNum->currentText());
if(!serial->open(QIODevice::ReadWrite))
{
ui->textEditRXD->append("串口打开失败!\n无此串口或者串口被占用.\n");
}
else
{
ui->textEditRXD->append("串口打开成功!\n");
serial->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);
serial->setDataBits(QSerialPort::Data8);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
//
connect(serial,SIGNAL(readyRead()),this,SLOT(receiveInfo()));
ui->btnPortStatus->setEnabled(false);
ui->btnClosePort->setDisabled(false);
}
}
fTimer->start();//串口数据接收定时器开启
}
//关闭串口
void Widget::on_btnClosePort_clicked()
{
serial->clear();
serial->close();
ui->btnClosePort->setEnabled(false);
ui->btnPortStatus->setDisabled(false);
fTimer->stop();//串口数据接收定时器关闭
}
#include "Widget.h"
#include "ui_Widget.h"
#include
#include
//?
void Widget::insertSendPointInfom(QString &m_sendDataStr, int m_count, QString m_color, int m_size, QString m_site)
//发送指令, 第几个点, 颜色, 大小, 坐标
{
QString m_pointInfom;
m_sendDataStr = m_sendDataStr.remove(" ").remove(POINTSIZEDETASITE,COUNTPOINTSIZEDATA)
.insert(POINTSIZEDETASITE,"0"+QString::number(m_size,16));
m_pointInfom = m_sendDataStr.mid(COORDINATESDATASITE+COUNTCOORDINATESDATA*(m_count-1),COUNTCOORDINATESDATA);
m_pointInfom = m_pointInfom.remove(COLORSITE,1).insert(COLORSITE,m_color);
m_pointInfom = m_pointInfom.remove(X_SITE,COUNTCOORDINATESDATA-1).insert(X_SITE,m_site);
}
//ok
void Widget::insertSendPointInfom(QString &m_sendDataStr,int m_size)
//发送指令, 大小
{
if( m_size >= 15 )
{
m_size = 15;
}
m_sendDataStr = m_sendDataStr.remove(" ").remove(POINTSIZEDETASITE,COUNTPOINTSIZEDATA)
.insert(POINTSIZEDETASITE,"0"+QString::number(m_size,16));
}
//ok
void Widget::insertSendPointInfom(QString &m_sendDataStr, int m_count, QString m_color)
//发送指令, 第几个点, 颜色
{
m_sendDataStr = m_sendDataStr.remove(" ").remove(COORDINATESDATASITE+COUNTCOORDINATESDATA*(m_count-1),COLORCOUNT)
.insert(COORDINATESDATASITE+COUNTCOORDINATESDATA*(m_count-1),m_color);
}
//?
void Widget::insertSendPointInfom(QString &m_sendDataStr,QString m_site , int m_count)
//发送指令, 坐标, 第几个点
{
QString m_pointInfom;
m_site.insert(SPACESITE,1);
m_sendDataStr = m_sendDataStr.remove(" ").remove(COORDINATESDATASITE+COUNTCOORDINATESDATA*(m_count-1)+1,COUNTCOORDINATESDATA-1);
m_pointInfom.remove(X_SITE,COUNTCOORDINATESDATA-1).insert(X_SITE,m_site);
}
include "Widget.h"
#include "ui_Widget.h"
#include
#include
//测试模块
void Widget::on_btnTest_clicked()
{
//ctrlPoint(3,1,ui->hsldPointSize->value());
//QString str =ui->textEditTXD->toPlainText();
//(str);
//ui->textEditRXD->append(str);
// QString pointInfom;
// points = ui->sbPoints->value();
// pointSize = ui->sbHsldPointSize->value();
// //ui->textEditRXD->append("测试"+rcvData_QStr+"point:"+QString::number(points));
// rcvDataHandle(rcvData_QStr,pointInfom,points);
// if(judgementStandard(pointInfom,R_QUALIF_LUMINANCE,R_QUALIF_LUMINANCE_ERROR) == QUALIF)
// {
// ui->textEditRXD->append(pointInfom+"绿灯");
// }
// else if(judgementStandard(pointInfom,R_QUALIF_LUMINANCE,R_QUALIF_LUMINANCE_ERROR) == DISQUALIF)
// {
// ui->textEditRXD->append(pointInfom+"红灯");
// }
// else
// {
// ui->textEditRXD->append("未知");
// }
// sendData_Str = sendData_Str.remove(" ");
// insertSendPointInfom(sendData_Str,1,QString::number(GREENPOINT,16));
// ui->textEditTXD->setText(sendData_Str);
// QString str = "000100010001";
// int i = judgementStandard(str,R_QUALIF_LUMINANCE,R_QUALIF_LUMINANCE_ERROR,1);
// ui->textEditRXD->append(QString::number(i));
// ui->textEditRXD->append(QString::number(points));
QString pointInfom;
if(rcvData_QStr.length()!=0)
{
rcvDataHandle(rcvData_QStr,pointInfom,points);
judgementStandard(pointInfom,R_QUALIF_LUMINANCE,R_QUALIF_LUMINANCE_ERROR,points);
}
}
#include "Widget.h"
#include "ui_Widget.h"
#include
#include
void Widget::updataUiData()
{
points = ui->sbPoints->value();
pointSize = ui->sbHsldPointSize->value();
if(QString::compare(sendData_Str.remove(" "),ui->textEditTXD->toPlainText().remove(" ")) != 0)
{
insertSpaceDataHandle(sendData_Str);
ui->textEditTXD->setText(sendData_Str);
}
if(rcvData_Byt.length()!=0)
{
rcvData_QStr = rcvData_Byt.toHex().toUpper().mid(8);
insertSpaceDataHandle(rcvData_QStr.remove(" "));
ui->textEditRXD->append("#接收#:"+rcvData_QStr+"\n");
//rcvData_QStr = rcvData_QStr.remove(" ");
}
}
void Widget::on_hsldPoints_sliderReleased()
{
points = ui->hsldPoints->value();
}
void Widget::on_hsldPointSize_sliderReleased()
{
pointSize = ui->hsldPointSize->value();
}
//当输入文本框发生变化时sendData_Str的值发生变化
void Widget::on_textEditTXD_textChanged()
{
sendData_Str = ui->textEditTXD->toPlainText();
}
//当点的大小发生变化时成员变量pointSize的值随之变化
void Widget::on_sbHsldPointSize_valueChanged(int arg1)
{
pointSize = ui->sbHsldPointSize->value();
}
//当选中点序号发生变化时成员变量points的值随之变化
void Widget::on_sbPoints_valueChanged(int arg1)
{
points = ui->sbPoints->value();
}
#include "Widget.h"
#include "ui_Widget.h"
#include
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
serial = new QSerialPort();
QString instructData = "00 11 22 33 44 08 f0 00 20 ee f0 ee 40 a4 f0 db 81 a4 00 d9 81 06 01 06 81 08 01 08 81 0a 01 0a 81 0c 01 0c 81 0e 01 0e 81 10 01 10 81 12 01 12 81 14 01 14 81 16 01 16 81 18 01 18 81 1a 01 1a 81 1c 01 1c 81 1e 01 1e 81 20 01 20 81 22 01 22 ee";
sendData_Str = instructData;
portInit();//串口初始化
initTimer(100);//创建定时器,周期为100
/*************初始化控件*************/
//ui->btnTXD->setEnabled(false);
//ui->btnTest->setVisible(false);
ui->textEditTXD->setText(sendData_Str);
ui->widget_6->setVisible(false);
ui->widget_5->setVisible(false);
ui->widget_3->setVisible(false);
ui->widget_2->setVisible(false);
ui->widget_6->setVisible(false);
ui->sbPoints->setRange(1,18);
ui->hsldPoints->setRange(1,18);
ui->hsldPointSize->setRange(1,15);
//ui->lblHsldPointSize->setNum(ui->hsldPointSize->value());
/**********************************/
}
Widget::~Widget()
{
delete ui;
delete serial;
}
void Widget::on_btnClear_clicked()
{
ui->textEditRXD->clear();
}
胡乱写的东西