forked from ccsens_hardware/qt_qcp_show
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
233 lines
6.2 KiB
233 lines
6.2 KiB
#include "commonutil.h"
|
|
#include <QNetworkInterface>
|
|
#include <QList>
|
|
#include <QDataStream>
|
|
|
|
CommonUtil::CommonUtil()
|
|
{
|
|
|
|
}
|
|
|
|
QByteArray CommonUtil::HexStringToByteArray(QString HexString)
|
|
{
|
|
bool ok;
|
|
QByteArray ret;
|
|
HexString = HexString.trimmed();
|
|
HexString = HexString.simplified();
|
|
QStringList sl = HexString.split(" ");
|
|
|
|
foreach (QString s, sl) {
|
|
if(!s.isEmpty()) {
|
|
char c = s.toInt(&ok,16)&0xFF;
|
|
if(ok){
|
|
ret.append(c);
|
|
}else{
|
|
qDebug()<<"非法的16进制字符:"<<s;
|
|
}
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
QString CommonUtil::ByteArrayToHexString(const QByteArray &ba)
|
|
{
|
|
//QDataStream out(&ba,QIODevice::ReadWrite); //将str的数据 读到out里面去
|
|
QDataStream out(ba); //将str的数据 读到out里面去
|
|
QString buf;
|
|
while(!out.atEnd())
|
|
{
|
|
qint8 outChar = 0;
|
|
out >> outChar; //每次一个字节的填充到 outchar
|
|
QString str = QString("%1").arg(outChar & 0xFF,2,16,QLatin1Char('0')).toUpper() + QString(" "); //2 字符宽度
|
|
buf += str;
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
QString CommonUtil::ByteArrayToNonZeroString(const QByteArray &ba)
|
|
{
|
|
QDataStream out(ba); //将str的数据 读到out里面去
|
|
QString buf;
|
|
while(!out.atEnd())
|
|
{
|
|
qint8 outChar = 0;
|
|
out >> outChar; //每次一个字节的填充到 outchar
|
|
QString str = outChar != 0 ? QString(QChar(outChar)) : "\\0";
|
|
buf += str;
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
|
|
QByteArray CommonUtil::quint8ArrToQByteArray(quint8 *arr,int lengths)
|
|
{
|
|
QByteArray ba;
|
|
for(int i=0;i<lengths;i++)
|
|
{
|
|
ba[i] = arr[i];
|
|
}
|
|
return ba;
|
|
}
|
|
|
|
#define I_1pA (1)
|
|
#define I_1nA (I_1pA * 1000)
|
|
#define I_1uA (I_1nA * 1000)
|
|
#define I_1mA (I_1uA * 1000)
|
|
|
|
QString CommonUtil::i_pA_2_text(unsigned int iPA)
|
|
{
|
|
qint64 ipa = (qint64)iPA;
|
|
if(ipa >= I_1mA)
|
|
{
|
|
return QString("%1mA").arg(ipa * 100 / I_1mA / 100.0);
|
|
}
|
|
else if(ipa >= I_1uA)
|
|
{
|
|
return QString("%1uA").arg(ipa * 100 / I_1uA / 100.0);
|
|
}
|
|
else if(ipa >= I_1nA)
|
|
{
|
|
return QString("%1nA").arg(ipa * 100 / I_1nA / 100.0);
|
|
}
|
|
else if(ipa >= I_1pA)
|
|
{
|
|
return QString("%1pA").arg(ipa * 100 / I_1pA / 100.0);
|
|
}
|
|
return "0";
|
|
}
|
|
|
|
void CommonUtil::crc16(quint8 crc[], quint8 data[],int pos, int len)
|
|
{
|
|
int wcrc = 0xFFFF;// 预置16位crc寄存器,初值全部为1
|
|
int temp;// 定义中间变量
|
|
int i = 0, j = 0, k = pos;// 定义计数
|
|
|
|
for (i = 0; i < len; i++)// 循环计算每个数据
|
|
{
|
|
temp = (data[k] & 0xFF);// 将八位数据与crc寄存器亦或
|
|
k++;// 指针地址增加,指向下个数据
|
|
wcrc ^= temp;// 将数据存入crc寄存器
|
|
for (j = 0; j < 8; j++)// 循环计算数据的
|
|
{
|
|
if ((wcrc & 0x0001) == 1)// 判断右移出的是不是1,如果是1则与多项式进行异或。
|
|
{
|
|
wcrc >>= 1;// 先将数据右移一位
|
|
wcrc ^= 0XA001;// 与上面的多项式进行异或
|
|
} else// 如果不是1,则直接移出
|
|
{
|
|
wcrc >>= 1;// 直接移出
|
|
}
|
|
}
|
|
}
|
|
temp = wcrc;// crc的值
|
|
crc[0] = wcrc & 0xFF; // crc的低八位
|
|
crc[1] = (wcrc >> 8) & 0xFF; // crc的高八位
|
|
}
|
|
|
|
|
|
//判断是否大端字节序
|
|
int CommonUtil::isBigEndianOrder()
|
|
{
|
|
int iVal = 1;
|
|
char *pChar = (char*)(&iVal);
|
|
if(*pChar == 1) //低字节在低地址(小端)
|
|
return false;
|
|
return true; //(0x00000001) 低字节在高地址(大端) Aix采用的是大端法
|
|
}
|
|
|
|
#ifdef Q_OS_WIN
|
|
quint32 CommonUtil::Lc_ntohl(quint32 data)
|
|
{
|
|
quint32 res = 0;
|
|
if(data == 0 || isBigEndianOrder()){
|
|
res = data;
|
|
}else{
|
|
res = (((data >> 0) & 0xFF) << 24) | (((data >> 8) & 0xFF) << 16) | (((data >> 16) & 0xFF) << 8) | (((data >> 24) & 0xFF) << 0);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
quint32 CommonUtil::Lc_htonl(quint32 data)
|
|
{
|
|
quint32 res = 0;
|
|
if(data == 0 || isBigEndianOrder()){
|
|
res = data;
|
|
}else{
|
|
res = (((data >> 0) & 0xFF) << 24) | (((data >> 8) & 0xFF) << 16) | (((data >> 16) & 0xFF) << 8) | (((data >> 24) & 0xFF) << 0);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
quint16 CommonUtil::Lc_ntohs(quint16 data)
|
|
{
|
|
quint16 res = 0;
|
|
if(data == 0 || isBigEndianOrder()){
|
|
res = data;
|
|
}else{
|
|
res = (((data >> 0) & 0xFF) << 8) | (((data >> 8) & 0xFF) << 0) ;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
quint16 CommonUtil::Lc_htons(quint16 data)
|
|
{
|
|
quint16 res = 0;
|
|
if(data == 0 || isBigEndianOrder()){
|
|
res = data;
|
|
}else{
|
|
res = (((data >> 0) & 0xFF) << 8) | (((data >> 8) & 0xFF) << 0) ;
|
|
}
|
|
return res;
|
|
}
|
|
#endif
|
|
|
|
void CommonUtil::Lc_buf_copy_uc(quint8 *d,const quint8 *s,quint32 num)
|
|
{
|
|
quint32 i;
|
|
for(i = 0; i < num; i ++)
|
|
{
|
|
*(d+i) = *(s+i);
|
|
}
|
|
}
|
|
|
|
void CommonUtil::delay(quint32 n)
|
|
{
|
|
while(n--);
|
|
}
|
|
|
|
QString CommonUtil::getHostIpAddress()
|
|
{
|
|
QString strIpAddress;
|
|
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
|
|
// 获取第一个本主机的IPv4地址
|
|
int nListSize = ipAddressesList.size();
|
|
for (int i = 0; i < nListSize; ++i)
|
|
{
|
|
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
|
|
ipAddressesList.at(i).toIPv4Address()) {
|
|
strIpAddress = ipAddressesList.at(i).toString();
|
|
break;
|
|
}
|
|
}
|
|
// 如果没有找到,则以本地IP地址为IP
|
|
if (strIpAddress.isEmpty())
|
|
strIpAddress = QHostAddress(QHostAddress::LocalHost).toString();
|
|
return strIpAddress;
|
|
}
|
|
|
|
QString CommonUtil::getHostMacAddress()
|
|
{
|
|
QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces();// 获取所有网络接口列表
|
|
int nCnt = nets.count();
|
|
QString strMacAddr = "";
|
|
for(int i = 0; i < nCnt; i ++)
|
|
{
|
|
// 如果此网络接口被激活并且正在运行并且不是回环地址,则就是我们需要找的Mac地址
|
|
if(nets[i].flags().testFlag(QNetworkInterface::IsUp) && nets[i].flags().testFlag(QNetworkInterface::IsRunning) && !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack))
|
|
{
|
|
strMacAddr = nets[i].hardwareAddress();
|
|
break;
|
|
}
|
|
}
|
|
return strMacAddr;
|
|
}
|
|
|