QT5串口讀取宇電溫控器溫度

宇電溫控器AI5.X系列ios

類庫運行環境:win10+qt5+vs2017編程

類說明:TempControl,提供獲取當前溫控器溫度值,PV,SV,MV等值,調用公有函數接口,得到返回值。app

附帶測試用例函數

#ifndef _TEMPCONTROL_H_
#define _TEMPCONTROL_H_oop

#pragma execution_character_set("utf-8")
#include <QObject>
#include <iostream>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QTime>
#include <QTimer>
#include <qDebug>
#include <QEventLoop>
using namespace std;
class TempControl : public QObject
{
Q_OBJECT學習

public:
TempControl(QString portName,
QIODevice::OpenModeFlag mode,
qint32 baudRate,
QSerialPort::DataBits dataBits,
QSerialPort::Parity parity,
QSerialPort::FlowControl flowControl,
QSerialPort::StopBits stopBits);
~TempControl();
int Init();
void deinit();
void printHex(QByteArray cmd);
float getCrtTemp();
float getTemp();//獲取當前溫度
float getProNum();
float getSc();
float getCrtTempTime();
float getSpr();
float getStepNum();
float getPonp();
float getPaf();
float getStepState();
float getPonTime();
float getAt(); //自整定 測試

void setTemp( float Val, float CrtVal, int Proid = 0 , int AddressIde = 1 );
void setSc(float Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void setProNum(float Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void setCrtTempTime(float Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void setSPr(float Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void setStepNum(float Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void setPonP(float Val, float CrtVal, int Proid = 0, int AddressIde = 1);// 上電控制參數 Cont,
void setPaf(float Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void setStepState(float Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void SetAt(int AtVal, int Proid , int AddressIde );ui

private:
QString portName;
QIODevice::OpenModeFlag mode;
qint32 baudRate;
QSerialPort::DataBits dataBits;
QSerialPort::Parity parity;
QSerialPort::FlowControl flowControl;
QSerialPort::StopBits stopBits;
QSerialPort *m_serialport;this

void delayMs(int value);
int send(QByteArray &cmd);
int recieve(QByteArray &cmd, int timeout);
void recieve(QByteArray &cmd);
int Udian_CheckRc(QByteArray ReadBytes, int AddressIde);
char HexH(int v);
char HexL(int v);
int GetHexVal(char v);
void getCrtTempCmd(QByteArray &WriteBytes, float Val, int Proid= 0, int AddressIde = 1);//控制溫度
void getTempCmd(QByteArray &WriteBytes, float Val, float CrtVal, int Proid = 0, int AddressIde = 1);//當前溫度
void getProNumCmd(QByteArray &WriteBytes, float Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void getScCmd(QByteArray &WriteBytes, float Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void getCrtTempTimeCmd(QByteArray &WriteBytes, float Val, int Proid = 0, int AddressIde = 1);
void getSprCmd(QByteArray &WriteBytes, float Val, int Proid = 0, int AddressIde = 1);
void getStepNumCmd(QByteArray &WriteBytes, float Val, int Proid = 0, int AddressIde = 1);
void getPonpCmd(QByteArray &WriteBytes, float Val, int Proid = 0, int AddressIde = 1);//上電控制參數
void getPafCmd(QByteArray &WriteBytes, float Val, int Proid = 0, int AddressIde = 1);
void getStepStateCmd(QByteArray &WriteBytes, float Val, int Proid = 0, int AddressIde = 1);
void getPonTimeCmd(QByteArray &WriteBytes, float Val, int Proid = 0, int AddressIde = 1);
void getAtCmd(QByteArray &WriteBytes, float Val, int Proid = 0, int AddressIde = 1); //自整定 spa

float getCrtTempValue(QByteArray ReadBytes, int Proid = 0, int AddressIde = 1);
float getTempValue(QByteArray ReadBytes, int Proid = 0, int AddressIde = 1);
float getProNumValue(QByteArray ReadBytes, int Proid = 0, int AddressIde = 1);
float getScValue(QByteArray ReadBytes, int Proid = 0, int AddressIde = 1);
float getCrtTempTimeValue(QByteArray ReadBytes, int Proid = 0, int AddressIde = 1);
float getSprValue(QByteArray ReadBytes, int Proid = 0, int AddressIde = 1);
float getStepNumValue(QByteArray ReadBytes, int Proid = 0, int AddressIde = 1);
float getPonpValue(QByteArray ReadBytes, int Proid = 0, int AddressIde = 1);
float getPafValue(QByteArray ReadBytes, int Proid = 0, int AddressIde = 1);
float getStepStateValue(QByteArray ReadBytes, int Proid = 0, int AddressIde = 1);
float getPonTimeValue(QByteArray ReadBytes, int Proid = 0, int AddressIde = 1);
float getAtValue(QByteArray ReadBytes, int Proid = 0, int AddressIde = 1); //自整定

void setTempCmd(QByteArray &WriteBytes, float Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void setScCmd(QByteArray &WriteBytes, float Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void setProNumCmd(QByteArray &WriteBytes, int Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void setCrtTempTimeCmd(QByteArray &WriteBytes, float Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void setSPrCmd(QByteArray &WriteBytes, int Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void setStepNumCmd(QByteArray &WriteBytes, int Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void setPonPCmd(QByteArray &WriteBytes, int Val, float CrtVal, int Proid = 0, int AddressIde = 1);// 上電控制參數 Cont,
void setPafCmd(QByteArray &WriteBytes, int Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void setStepStateCmd(QByteArray &WriteBytes, int Val, float CrtVal, int Proid = 0, int AddressIde = 1);
void SetAtCmd(QByteArray &WriteBytes, int AtVal, int Proid = 0, int AddressIde = 1);


};
#endif // !_TEMPCONTROL_H_

 

#include "TempControl.h"

TempControl::TempControl(QString portName,
QIODevice::OpenModeFlag mode,
qint32 baudRate,
QSerialPort::DataBits dataBits,
QSerialPort::Parity parity,
QSerialPort::FlowControl flowControl,
QSerialPort::StopBits stopBits)
: QObject()
{
this->portName = portName;
this->mode = mode;
this->baudRate = baudRate;
this->dataBits = dataBits;
this->flowControl = flowControl;
this->stopBits = stopBits;
m_serialport = new QSerialPort();
}
TempControl::~TempControl()
{

delete m_serialport;
}
void TempControl::printHex(QByteArray cmd)
{
qDebug() << cmd.toHex() << endl;

}

int TempControl::Init()
{

m_serialport->setPortName(portName);
//以讀寫方式打開串口
if (m_serialport->open(mode))
{
//設置波特率
m_serialport->setBaudRate(baudRate);
//設置數據位
m_serialport->setDataBits(dataBits);
//設置校驗位
m_serialport->setParity(QSerialPort::NoParity);
//設置流控制
m_serialport->setFlowControl(QSerialPort::NoFlowControl);
//設置中止位
m_serialport->setStopBits(QSerialPort::OneStop);
m_serialport->clearError();
m_serialport->clear();
m_serialport->setDataTerminalReady(true);
//每秒讀一次
return 0;
}
else
{
return -1;
}
}


void TempControl::deinit()
{
m_serialport->close();
}

int TempControl::send(QByteArray &cmd) //ms
{
qDebug() << "send cmd:" << cmd.toHex() << endl;
m_serialport->clear();
m_serialport->write(cmd);
bool ret = m_serialport->flush();
qDebug() << "flush:" << ret << endl;
delayMs(100);
return ret;
}
void TempControl::recieve(QByteArray &cmd)
{
QByteArray readComData = m_serialport->readAll();
if (readComData != "")
{
qDebug() << "receive cmd:" << readComData << endl;
}
cmd = readComData;

readComData.clear();


}
int TempControl::recieve(QByteArray &cmd, int timeout)
{

int numRead = 0, numReadTotal = 10;
char buff[10];
cmd.clear();

QTime startTime = QTime::currentTime();
while (1)
{
QTime endTime = QTime::currentTime();
if (((((endTime.hour() - startTime.hour()) * 60 + (endTime.minute() - startTime.minute())) * 60 +
(endTime.second() - startTime.second())) * 1000 + endTime.msec() - startTime.msec()) > timeout)
{
break;
}

int ret = m_serialport->waitForReadyRead(1000);
int num = m_serialport->bytesAvailable();
if (!ret && num != 0)
{
QByteArray recieve = m_serialport->read(num);
if (recieve.size())
{
m_serialport->clear();
qDebug() << "recieve:" << recieve.toHex() << endl;
cmd.append(recieve);
recieve.clear();
numRead += num;
qDebug() << "num:" << numRead << endl;
}
if (numRead == 10)
{
break;
}
}
else
{
QEventLoop eventloop;
QTimer::singleShot(100, &eventloop, SLOT(quit()));
eventloop.exec();
}
}

qDebug() << "cmd:" << cmd.toHex() << endl;
return 0;


}

void TempControl::delayMs(int value)
{
QEventLoop eventloop;
QTimer::singleShot(value, &eventloop, SLOT(quit()));
eventloop.exec();


}
float TempControl::getCrtTemp()
{
float fValue;
QByteArray cmd,getCmd;
cmd.resize(8);
getCrtTempCmd(cmd, 1,0,1);
send(cmd);
recieve(getCmd,1000*100);
fValue = getCrtTempValue(getCmd,0,1);
return fValue;
}
float TempControl::getTemp()
{
float fValue;
QByteArray cmd, getCmd;
cmd.resize(8);
getTempCmd(cmd, 1,1,0,1);
send(cmd);
recieve(getCmd,1000*100);
fValue = getTempValue(getCmd);
return fValue;
}
float TempControl::getProNum()
{
float fValue;
QByteArray cmd, getCmd;
cmd.resize(8);
getProNumCmd(cmd, 1,1,0,1);
send(cmd);
recieve(getCmd, 1000 * 100);
fValue = getProNumValue(getCmd);
return fValue;
}
float TempControl::getSc()
{
float fValue;
QByteArray cmd, getCmd;
cmd.resize(8);
getScCmd(cmd, 1, 1,0,1);
send(cmd);
recieve(getCmd, 1000 * 100);
fValue = getScValue(getCmd);
return fValue;
}
float TempControl::getCrtTempTime()
{
float fValue;
QByteArray cmd, getCmd;
cmd.resize(8);
getCrtTempTimeCmd(cmd, 1, 0, 1);
send(cmd);
recieve(getCmd, 1000 * 100);
fValue = getCrtTempValue(getCmd, 0, 1);
return fValue;
}
float TempControl::getSpr()
{
float fValue;
QByteArray cmd, getCmd;
cmd.resize(8);
getSprCmd(cmd, 1, 0, 1);
send(cmd);
recieve(getCmd, 1000 * 100);
fValue = getSprValue(getCmd, 0, 1);
return fValue;
}
float TempControl::getStepNum()
{
float fValue;
QByteArray cmd, getCmd;
cmd.resize(8);
getStepNumCmd(cmd, 1, 0, 1);
send(cmd);
recieve(getCmd, 1000 * 100);
fValue = getStepNumValue(getCmd, 0, 1);
return fValue;
}
float TempControl::getPonp()
{
float fValue;
QByteArray cmd, getCmd;
cmd.resize(8);
getPonpCmd(cmd, 1, 0, 1);
send(cmd);
recieve(getCmd, 1000 * 100);
fValue = getStepNumValue(getCmd, 0, 1);
return fValue;
}
float TempControl::getPaf()
{
float fValue;
QByteArray cmd, getCmd;
cmd.resize(8);
getPafCmd(cmd, 1, 0, 1);
send(cmd);
recieve(getCmd, 1000 * 100);
fValue = getPafValue(getCmd, 0, 1);
return fValue;
}
float TempControl::getStepState()
{
float fValue;
QByteArray cmd, getCmd;
cmd.resize(8);
getStepStateCmd(cmd, 1, 0, 1);
send(cmd);
recieve(getCmd, 1000 * 100);
fValue = getStepStateValue(getCmd, 0, 1);
return fValue;
}
float TempControl::getPonTime()
{
float fValue;
QByteArray cmd, getCmd;
cmd.resize(8);
getPonTimeCmd(cmd, 1, 0, 1);
send(cmd);
recieve(getCmd, 1000 * 100);
fValue = getPonTimeValue(getCmd, 0, 1);
return fValue;
}
float TempControl::getAt() //自整定
{
float fValue;
QByteArray cmd, getCmd;
cmd.resize(8);
getAtCmd(cmd, 1, 0, 1);
send(cmd);
recieve(getCmd, 1000 * 100);
fValue = getAtValue(getCmd, 0, 1);
return fValue;
}

void TempControl::setTemp(float Val, float CrtVal, int Proid, int AddressIde )
{
QByteArray cmd, getCmd;
cmd.resize(8);
setTempCmd(cmd, Val, CrtVal, 0, 1);
send(cmd);
}
void TempControl::setSc(float Val, float CrtVal, int Proid, int AddressIde)
{
QByteArray cmd, getCmd;
cmd.resize(8);
setScCmd(cmd, Val, CrtVal, 0, 1);
send(cmd);

}
void TempControl::setProNum(float Val, float CrtVal, int Proid, int AddressIde)
{
QByteArray cmd, getCmd;
cmd.resize(8);
setProNumCmd(cmd, Val, CrtVal, 0, 1);
send(cmd);

}
void TempControl::setCrtTempTime(float Val, float CrtVal, int Proid, int AddressIde)
{
QByteArray cmd, getCmd;
cmd.resize(8);
setCrtTempTimeCmd(cmd, Val, CrtVal, 0, 1);
send(cmd);

}
void TempControl::setSPr(float Val, float CrtVal, int Proid, int AddressIde)
{

QByteArray cmd, getCmd;
cmd.resize(8);
setSPrCmd(cmd, Val, CrtVal, 0, 1);
send(cmd);
}
void TempControl::setStepNum(float Val, float CrtVal, int Proid, int AddressIde)
{
QByteArray cmd, getCmd;
cmd.resize(8);
setStepNumCmd(cmd, Val, CrtVal, 0, 1);
send(cmd);

}
void TempControl::setPonP(float Val, float CrtVal, int Proid, int AddressIde)// 上電控制參數 Cont,
{
QByteArray cmd, getCmd;
cmd.resize(8);
setPonPCmd(cmd, Val, CrtVal, 0, 1);
send(cmd);

}
void TempControl::setPaf(float Val, float CrtVal, int Proid, int AddressIde)
{
QByteArray cmd, getCmd;
cmd.resize(8);
setPafCmd(cmd, Val, CrtVal, 0, 1);
send(cmd);

}
void TempControl::setStepState(float Val, float CrtVal, int Proid, int AddressIde)
{
QByteArray cmd, getCmd;
cmd.resize(8);
setStepStateCmd(cmd, Val, CrtVal, 0, 1);
send(cmd);
}
void TempControl::SetAt(int AtVal, int Proid, int AddressIde)
{
QByteArray cmd, getCmd;
cmd.resize(8);
SetAtCmd(cmd, AtVal, 0, 1);
send(cmd);

}


char TempControl::HexH(int v)
{
switch ((v >> 4) & 0xF)
{
case 0: return '0';
case 1: return '1';
case 2: return '2';
case 3: return '3';
case 4: return '4';
case 5: return '5';
case 6: return '6';
case 7: return '7';
case 8: return '8';
case 9: return '9';
case 10: return 'A';
case 11: return 'B';
case 12: return 'C';
case 13: return 'D';
case 14: return 'E';
case 15: return 'F';
default: return '0';
}
}
char TempControl::HexL(int v)
{
switch (v & 0xF)
{
case 0: return '0';
case 1: return '1';
case 2: return '2';
case 3: return '3';
case 4: return '4';
case 5: return '5';
case 6: return '6';
case 7: return '7';
case 8: return '8';
case 9: return '9';
case 10: return 'A';
case 11: return 'B';
case 12: return 'C';
case 13: return 'D';
case 14: return 'E';
case 15: return 'F';
default: return '0';
}
}
int TempControl::GetHexVal(char v)
{
if (v >= '0' && v <= '9')
{
return v - '0';
}
if (v >= 'A' && v <= 'F')
{
return v + 10 - 'A';
}
return 0;
}
int TempControl::Udian_CheckRc(QByteArray ReadBytes, int AddressIde)
{

unsigned char DatPv = ReadBytes[0] + ReadBytes[1] * 0x100;
unsigned char DatSv = ReadBytes[2] + ReadBytes[3] * 0x100;
unsigned char DatMv = ReadBytes[4];
unsigned char DatAl = ReadBytes[5];
unsigned char DatFun = ReadBytes[6] + ReadBytes[7] * 0x100;
unsigned char DatRc = ReadBytes[8] + ReadBytes[9] * 0x100;
unsigned char cRc =( DatPv + DatSv + DatMv + DatAl * 0x100 + DatFun + AddressIde);
cRc &= 0xffff;
qDebug() << "datPv:" << DatPv
<< "datSv:" << DatSv
<< "datMv:" << DatMv
<< "datAI:" << DatAl
<< "datFun:" << DatFun
<< "datRc" << DatRc
<< "cRc" << cRc << endl;
if (DatRc == cRc)
{
return 1;
}
else
{
return 0;
}

}
void TempControl::getCrtTempCmd(QByteArray &WriteBytes, float Val, int Proid, int AddressIde)
{

int cRc = 0, ret = 0;
ushort ucRc;

WriteBytes[0] = 0x80 + AddressIde;
WriteBytes[1] = 0x80 + AddressIde;
WriteBytes[2] = 0x52;
if (Proid == 0)
{
WriteBytes[3] = 0;
}
else
{
WriteBytes[3] = 0x50 + Proid * 2;
}

WriteBytes[4] = 0;
WriteBytes[5] = 0;
cRc = WriteBytes[3] * 256 + 82 + AddressIde;
cRc = cRc & 0xffff; ucRc = cRc;
WriteBytes[6] = ucRc & 0xFF;
WriteBytes[7] = ucRc >> 8;
}
float TempControl::getCrtTempValue(QByteArray ReadBytes, int Proid, int AddressIde)
{
float Val;
long LongVal = 0;
if (Udian_CheckRc(ReadBytes, AddressIde) == 1)
{

LongVal = (unsigned char)ReadBytes[6] + (unsigned char)(ReadBytes[7] * 256);
Val = LongVal / 10.0f;
return Val;
}
else
{
return -1;

}

}
void TempControl::getTempCmd(QByteArray &WriteBytes, float Val, float CrtVal, int Proid, int AddressIde)
{
int cRc = 0, ret = 0;
int ucRc = 0;
WriteBytes[0] = 0x80 + AddressIde;
WriteBytes[1] = 0x80 + AddressIde;
WriteBytes[2] = 0x52;
WriteBytes[3] = 0;
WriteBytes[4] = 0;
WriteBytes[5] = 0;
cRc = WriteBytes[3] * 256 + 82 + AddressIde;
cRc = cRc & 0xffff; ucRc = cRc;
WriteBytes[6] =(ucRc & 0xFF);
WriteBytes[7] = (ucRc >> 8);
}
float TempControl::getTempValue(QByteArray ReadBytes, int Proid, int AddressIde)
{
float Val;
float CrtVal;
ushort LongVal = 0;

if (Udian_CheckRc(ReadBytes, AddressIde) == 1)
{
LongVal = (unsigned char)ReadBytes[0] + (unsigned char)(ReadBytes[1]) * 256;
qDebug() << "longVal:" << LongVal << endl;
Val = LongVal / 10.0f;
LongVal = (unsigned char)ReadBytes[6] + (unsigned char)(ReadBytes[7]) * 256;
CrtVal = LongVal / 10.0f;
return Val;

}
else
{
return -1;

}

}
void TempControl::setTempCmd(QByteArray &WriteBytes, float Val, float CrtVal, int Proid, int AddressIde)
{

int cRc = 0, ret = 0;
ushort ucRc = 0;
long LongVal = 0;
if (Val >= 0)
{
LongVal = (int)(Val * 10 + 0.001f);
}
else
{
LongVal = 65536 - (int)(0 - Val * 10.0f + 0.001f);
}
WriteBytes[0] = 0x80 + AddressIde;
WriteBytes[1] = 0x80 + AddressIde;
WriteBytes[2] = 0x43;
WriteBytes[3] = 0x50 + Proid * 2;
WriteBytes[4] = LongVal % 256;
WriteBytes[5] = LongVal / 256;
cRc = WriteBytes[3] * 256;
cRc += WriteBytes[2];
cRc += WriteBytes[4];
cRc += WriteBytes[5] * 256;
cRc += AddressIde; cRc &= 0xFFFF; ucRc = cRc;
WriteBytes[6] = ucRc % 256;
WriteBytes[7] = ucRc / 256;

}


void TempControl::setScCmd(QByteArray &WriteBytes, float Val, float CrtVal, int Proid, int AddressIde)
{
int cRc = 0, ret = 0;
ushort ucRc = 0;
long LongVal = 0;
if (Val >= 0)
{
LongVal = (int)(Val * 10 + 0.001f);
}
else
{
LongVal = 65536 - (int)(0 - Val * 10.0f + 0.001f);
}
WriteBytes[0] = 0x80 + AddressIde;
WriteBytes[1] = 0x80 + AddressIde;
WriteBytes[2] = 0x43;
WriteBytes[3] = 0x10;
WriteBytes[4] = LongVal % 256;
WriteBytes[5] = LongVal / 256;
cRc = WriteBytes[3] * 256;
cRc += WriteBytes[2];
cRc += WriteBytes[4];
cRc += WriteBytes[5] * 256;
cRc += AddressIde;
cRc &= 0xFFFF;
ucRc = cRc;
WriteBytes[6] = ucRc % 256;
WriteBytes[7] = ucRc / 256;

}

void TempControl::setProNumCmd(QByteArray &WriteBytes, int Val, float CrtVal, int Proid, int AddressIde)
{
int cRc = 0, ret = 0;
ushort ucRc = 0;
WriteBytes[0] = 0x80 + AddressIde;
WriteBytes[1] = 0x80 + AddressIde;
WriteBytes[2] = 0x43;
WriteBytes[3] = 0x2B;
WriteBytes[4] = Val % 256;
WriteBytes[5] = Val / 256;
cRc = WriteBytes[3] * 256;
cRc += WriteBytes[2];
cRc += WriteBytes[4];
cRc += WriteBytes[5] * 256;
cRc += AddressIde; cRc &= 0xFFFF;
ucRc = cRc;
WriteBytes[6] = ucRc % 256;
WriteBytes[7] = ucRc / 256;

}
void TempControl::getScCmd(QByteArray &WriteBytes, float Val, float CrtVal, int Proid, int AddressIde)
{

int cRc = 0, ret = 0;
ushort ucRc = 0;
long LongVal = 0;
WriteBytes[0] = 0x80 + AddressIde;
WriteBytes[1] = 0x80 + AddressIde;
WriteBytes[2] = 0x52;
WriteBytes[3] = 0x10;
WriteBytes[4] = 0;
WriteBytes[5] = 0;
cRc = WriteBytes[3] * 256 + 82 + AddressIde;
cRc = cRc & 0xffff;
ucRc = cRc;
WriteBytes[6] = ucRc % 256;
WriteBytes[7] = ucRc / 256;
}
float TempControl::getScValue(QByteArray ReadBytes, int Proid, int AddressIde)
{
float Val;
long LongVal = 0;
if (Udian_CheckRc(ReadBytes, AddressIde) == 1)
{

LongVal = (unsigned char)ReadBytes[6] + (unsigned char)(ReadBytes[7]) * 256;
Val = LongVal / 10.0f;
return Val;
}
else
{
return -1;

}


}
void TempControl::getProNumCmd(QByteArray &WriteBytes, float Val, float CrtVal, int Proid, int AddressIde)
{

int cRc = 0, ret = 0;
ushort ucRc = 0;
WriteBytes[0] = (0x80 + AddressIde);
WriteBytes[1] = (0x80 + AddressIde);
WriteBytes[2] = 0x52;
WriteBytes[3] = 0x2B;
WriteBytes[4] = 0;
WriteBytes[5] = 0;
cRc = WriteBytes[3] * 256 + 82 + AddressIde;
cRc = cRc & 0xffff;
ucRc = cRc;
WriteBytes[6] = (ucRc % 256);
WriteBytes[7] = (ucRc / 256);
}
float TempControl::getProNumValue(QByteArray ReadBytes, int Proid, int AddressIde)
{
float Val;
long LongVal = 0;
if (Udian_CheckRc(ReadBytes, AddressIde) == 1)
{
Val = (unsigned char)ReadBytes[6] + (unsigned char)(ReadBytes[7]) * 256;
return Val;
}
else
{
return -1;

}

 


}
void TempControl::setCrtTempTimeCmd(QByteArray &WriteBytes, float Val, float CrtVal, int Proid, int AddressIde)
{

int cRc = 0, ret = 0;
ushort ucRc = 0;
long LongVal = 0;
if (Val >= 0)
{
LongVal = (int)(Val * 10 + 0.001f);
}
else
{
LongVal = 65536 - (int)(0 - Val * 10.0f + 0.001f);
}
WriteBytes[0] = 0x80 + AddressIde;
WriteBytes[1] = (0x80 + AddressIde);
WriteBytes[2] = (0x43);
WriteBytes[3] = (0x51 + Proid * 2);
WriteBytes[4] = (LongVal % 256);
WriteBytes[5] = (LongVal / 256);
cRc = WriteBytes[3] * 256;
cRc += WriteBytes[2];
cRc += WriteBytes[4];
cRc += WriteBytes[5] * 256;
cRc += AddressIde; cRc &= 0xFFFF;
ucRc = cRc;
WriteBytes[6] = (ucRc % 256);
WriteBytes[7] = (ucRc / 256);
}
void TempControl::getCrtTempTimeCmd(QByteArray &WriteBytes, float Val, int Proid, int AddressIde)
{

int cRc = 0, ret = 0;
ushort ucRc = 0;
long LongVal = 0;
WriteBytes[0] = (0x80 + AddressIde);
WriteBytes[1] = (0x80 + AddressIde);
WriteBytes[2] = (0x52);
WriteBytes[3] = 0x51 + Proid * 2;
WriteBytes[4] = 0;
WriteBytes[5] = 0;
cRc = WriteBytes[3] * 256 + 82 + AddressIde;
cRc = cRc & 0xffff;
ucRc = cRc;
WriteBytes[6] = ucRc % 256;
WriteBytes[7] = ucRc / 256;
}
float TempControl::getCrtTempTimeValue(QByteArray ReadBytes, int Proid, int AddressIde)
{
float Val;
long LongVal = 0;

if (Udian_CheckRc(ReadBytes, AddressIde) == 1)
{

LongVal = (unsigned char)ReadBytes[6] + (unsigned char)(ReadBytes[7]) * 256;
Val = LongVal / 10.0f;
return Val;
}
else
{
return -1;

}


}
void TempControl::setSPrCmd(QByteArray &WriteBytes, int Val, float CrtVal, int Proid, int AddressIde)
{

int ret = 0;
ushort ucRc = 0;
int cRc = 0;
WriteBytes[0] = (0x80 + AddressIde);
WriteBytes[1] = (0x80 + AddressIde);
WriteBytes[2] = (0x43);
WriteBytes[3] = (0x2A);
WriteBytes[4] = (Val % 256);
WriteBytes[5] = (Val / 256);
cRc = WriteBytes[3] * 256;
cRc += WriteBytes[2];
cRc += WriteBytes[4];
cRc += WriteBytes[5] * 256;
cRc += AddressIde; cRc &= 0xFFFF;
ucRc = cRc;
WriteBytes[6] = (ucRc % 256);
WriteBytes[7] = (ucRc / 256);

}
void TempControl::getSprCmd(QByteArray &WriteBytes, float Val, int Proid, int AddressIde)
{

int cRc = 0, ret = 0;
long LongVal = 0;
ushort ucRc = 0;
WriteBytes[0] = (0x80 + AddressIde);
WriteBytes[1] = (0x80 + AddressIde);
WriteBytes[2] = 0x52;
WriteBytes[3] = 0x2A;
WriteBytes[4] = 0;
WriteBytes[5] = 0;
cRc = WriteBytes[3] * 256 + 82 + AddressIde;
cRc = cRc & 0xffff;
ucRc = cRc;
WriteBytes[6] = (ucRc & 0xFF);
WriteBytes[7] = (ucRc >> 8);

}
float TempControl::getSprValue(QByteArray ReadBytes, int Proid, int AddressIde)
{

float Val;
long LongVal = 0;
if (Udian_CheckRc(ReadBytes, AddressIde) == 1)
{

LongVal = (unsigned char)ReadBytes[6] + (unsigned char)(ReadBytes[7]) * 256;
Val = (int)LongVal;
return Val;
}
else
{
return -1;
}


}
void TempControl::setStepNumCmd(QByteArray &WriteBytes, int Val, float CrtVal, int Proid, int AddressIde)
{

int cRc = 0, ret = 0;
ushort ucRc = 0;
WriteBytes[0] = (0x80 + AddressIde);
WriteBytes[1] = (0x80 + AddressIde);
WriteBytes[2] = (0x43);
WriteBytes[3] = (0x2E);
WriteBytes[4] = (Val % 256);
WriteBytes[5] = (Val / 256);
cRc = WriteBytes[3] * 256;
cRc += WriteBytes[2];
cRc += WriteBytes[4];
cRc += WriteBytes[5] * 256;
cRc += AddressIde; cRc &= 0xFFFF;
ucRc = cRc;
WriteBytes[6] = ucRc % 256;
WriteBytes[7] = ucRc / 256;
}

void TempControl::getStepNumCmd(QByteArray &WriteBytes, float Val, int Proid, int AddressIde)
{

int cRc = 0, ret = 0;
ushort ucRc = 0;
long LongVal = 0;
WriteBytes[0] = (0x80 + AddressIde);
WriteBytes[1] = (0x80 + AddressIde);
WriteBytes[2] = (0x52);
WriteBytes[3] = (0x2E);
WriteBytes[4] = 0;
WriteBytes[5] = 0;
cRc = WriteBytes[3] * 256 + 82 + AddressIde;
cRc = cRc & 0xffff; ucRc = cRc;
WriteBytes[6] = ucRc & 0xFF;
WriteBytes[7] = ucRc >> 8;
}
float TempControl::getStepNumValue(QByteArray ReadBytes, int Proid, int AddressIde)
{
float Val;
long LongVal = 0;

if (Udian_CheckRc(ReadBytes, AddressIde) == 1)
{

LongVal = (unsigned char)ReadBytes[6] + (unsigned char)(ReadBytes[7]) * 256;
Val = LongVal;
return Val;
}
else

{
return -1;
}


}
void TempControl::setPonPCmd(QByteArray &WriteBytes, int Val, float CrtVal, int Proid, int AddressIde)// 上電控制參數 Cont,
{

int cRc = 0, ret = 0;
ushort ucRc = 0;
WriteBytes[0] = (0x80 + AddressIde);
WriteBytes[1] = (0x80 + AddressIde);
WriteBytes[2] = (0x43);
WriteBytes[3] = (0x2C);
WriteBytes[4] = (Val % 256);
WriteBytes[5] = (Val / 256);
cRc = WriteBytes[3] * 256;
cRc += WriteBytes[2];
cRc += WriteBytes[4];
cRc += WriteBytes[5] * 256;
cRc += AddressIde; cRc &= 0xFFFF; ucRc = cRc;
WriteBytes[6] = ucRc % 256;
WriteBytes[7] = ucRc / 256;

}
void TempControl::getPonpCmd(QByteArray &WriteBytes, float Val, int Proid, int AddressIde)//上電控制參數
{

int cRc = 0, ret = 0;
ushort ucRc = 0;
long LongVal = 0;
WriteBytes[0] = (0x80 + AddressIde);
WriteBytes[1] = (0x80 + AddressIde);
WriteBytes[2] = (0x52);
WriteBytes[3] = (0x2C);
WriteBytes[4] = 0;
WriteBytes[5] = 0;
cRc = WriteBytes[3] * 256 + 82 + AddressIde;
cRc = cRc & 0xffff; ucRc = cRc;
WriteBytes[6] = ucRc & 0xFF;
WriteBytes[7] = ucRc >> 8;
}
float TempControl::getPonpValue(QByteArray ReadBytes, int Proid, int AddressIde)
{

float Val;
long LongVal = 0;

if (Udian_CheckRc(ReadBytes, AddressIde) == 1)
{

LongVal = (unsigned char)ReadBytes[6] + (unsigned char)(ReadBytes[7]) * 256;
Val = LongVal;
return Val;
}
return -1;


}
void TempControl::setPafCmd(QByteArray &WriteBytes, int Val, float CrtVal, int Proid, int AddressIde)// A=1 B=0/1 C=0:分鐘 D=0 E=0 F =0 PAF=A+2B+4C+8D+16E+32F 推薦值 PAF=1 / 3
{

int cRc = 0, ret = 0;
ushort ucRc = 0;
WriteBytes[0] = 0x80 + AddressIde;
WriteBytes[1] = 0x80 + AddressIde;
WriteBytes[2] = 0x43;
WriteBytes[3] = 0x2D;
WriteBytes[4] = Val % 256;
WriteBytes[5] = Val / 256;
cRc = WriteBytes[3] * 256;
cRc += WriteBytes[2];
cRc += WriteBytes[4];
cRc += WriteBytes[5] * 256;
cRc += AddressIde; cRc &= 0xFFFF; ucRc = cRc;
WriteBytes[6] = ucRc % 256;
WriteBytes[7] = ucRc / 256;
}
// 停電前爲中止狀態則繼續中止,不然在儀表通電後繼續在原終止處執行。
// StoP,通電後不管出現何種狀況,儀表都進入中止狀態。
// run1,停電前爲中止狀態則繼續中止,不然來電後都自動從頭開始運行程序。
// dASt,在通電後若是沒有誤差報警則程序繼續執行,如有誤差報警則中止運行。
// HoLd(僅AI-719P),儀表在運行中停電,來電後不管出現何種狀況,儀表都進入暫停狀態。但若是儀表停電前爲中止狀態,則來電後仍保持中止狀態
void TempControl::getPafCmd(QByteArray &WriteBytes, float Val, int Proid, int AddressIde)
{

int cRc = 0, ret = 0;
ushort ucRc = 0;
long LongVal = 0;
WriteBytes[0] = (0x80 + AddressIde);
WriteBytes[1] = (0x80 + AddressIde);
WriteBytes[2] = (0x52);
WriteBytes[3] = (0x2D);
WriteBytes[4] = 0;
WriteBytes[5] = 0;
cRc = WriteBytes[3] * 256 + 82 + AddressIde;
cRc = cRc & 0xffff;
ucRc = cRc;
WriteBytes[6] = ucRc & 0xFF;
WriteBytes[7] = ucRc >> 8;
}
float TempControl::getPafValue(QByteArray ReadBytes, int Proid, int AddressIde)
{
float Val;
long LongVal = 0;

if (Udian_CheckRc(ReadBytes, AddressIde) == 1)
{

LongVal = (unsigned char)ReadBytes[6] + (unsigned char)(ReadBytes[7]) * 256;
Val = LongVal;
return Val;
}
else
{
return -1;

}


}
//PAF參數用於選擇程序控制功能,其計算方法以下:
//PAF=A×1+B×2 +C×4 +D×8+E×16+F×32
//A=0,準備功能(rdy)無效;A=1,準備功能有效。
//B=0,斜率模式,程序運行時存在溫度差異時,按折線過渡,能夠定義不一樣的升溫模式,也能夠降溫運行;B=1,平臺模式(恆溫模式),每段程序定義給定值及保溫時間,段間升溫速率可受SPr限制,到達下段條件可受rdy參數限制;另外,即便設置B=0,若是程序最後一段不是結束命令,則也執行恆溫模式,時間到後自動結束。
//C=0,程序時間以分爲單位;C=1,時間以小時爲單位。
//D=0,無測量值啓動功能;D=1,有測量值啓動功能。
//E=0,做爲程序給定發生器時上顯示窗顯示測量值;E=1,做爲程序給定發生器時上顯示窗顯示程序段號。
//F=0,標準運行模式;F=1,程序運行時執行RUN操做將進入暫停(HoLd)狀態。
//斜率模式
// 參數PAF.B=0時,程序編排統一採用溫度~時間~溫度格式,其定義是:從當前段設置溫度,通過該段設置的時間到達下一溫度。溫度設置值的單位同測量值PV,而時間值的單位可選擇分鐘或小時。在斜率模式下,若運行到Pno定義的最後一段程序不爲中止命令或跳轉命令(後文時間設置可編輯),則表示在該溫度下保溫該段時間後自動結束。下例爲一個包含線性升溫、恆溫、線性降溫、跳轉循環、準備、暫停的5段程序例子。
//第1段 SP 1=100.0 t 1=30.0 ;100℃起開始線性升溫到SP 2,升溫時間爲30分鐘,升溫斜率爲10℃/分
//第2段 SP 2=400.0 t 2=60.0 ;在400℃保溫運行,時間爲60分
//第3段 SP 3=400.0 t 3=120.0 ;降溫到SP 4,降溫時間爲120分,降溫斜率爲2℃/分
//第4段 SP 4=160.0 t 4=0.0 ;降溫至160℃後進入暫停狀態,需執行運行(run)才能繼續運行下一段
//第5段 SP 5=160.0 t 5=-1.0 ;跳往第1段執行,從頭循環開始運行。
//採用溫度~時間編程方法的優勢是升溫、降溫的斜率設置的範圍很是寬。升溫及恆溫段具備統一的設置格式,方便學習。
//設置曲線更靈活,能夠設置連續設置升溫段(如用不一樣斜率的升溫段近似實現函數升溫),或連續的恆溫
//平臺模式 設置參數PAF.B=1可選擇平臺模式,適合不須要獨立設置升溫斜率且不須要設置降溫斜率的應用,能夠簡化編程且更有效利用段數,每段程序含義爲溫度~該溫度恆溫時間,段與段之間也能夠用SPr參數定義一個升溫速率限制,
//若SPr設置爲0則表示全速升溫,因爲升溫時間沒法肯定並會佔據保溫時間,可設置rdy有效,以保證正確的恆溫時間
// A=1 B=0/1 C=0:分鐘 D=0 E=0 F =0 PAF=A+2B+4C+8D+16E+32F 推薦值 PAF=1 / 3

void TempControl::setStepStateCmd(QByteArray &WriteBytes, int Val, float CrtVal, int Proid, int AddressIde)
{

int cRc = 0, ret = 0;
ushort ucRc = 0;
WriteBytes[0] = (0x80 + AddressIde);
WriteBytes[1] = (0x80 + AddressIde);
WriteBytes[2] = 0x43;
WriteBytes[3] = 0x1B;
WriteBytes[4] = Val % 256;
WriteBytes[5] = Val / 256;
cRc = WriteBytes[3] * 256;
cRc += WriteBytes[2];
cRc += WriteBytes[4];
cRc += WriteBytes[5] * 256;
cRc += AddressIde; cRc &= 0xFFFF;
ucRc = cRc;
WriteBytes[6] = ucRc % 256;
WriteBytes[7] = ucRc / 256;


}
void TempControl::getStepStateCmd(QByteArray &WriteBytes, float Val, int Proid, int AddressIde)
{

int cRc = 0, ret = 0;
ushort ucRc = 0;
long LongVal = 0;
WriteBytes[0] = (0x80 + AddressIde);
WriteBytes[1] = (0x80 + AddressIde);
WriteBytes[2] = 0x52;
WriteBytes[3] = 0x1B;
WriteBytes[4] = 0;
WriteBytes[5] = 0;
cRc = WriteBytes[3] * 256 + 82 + AddressIde;
cRc = cRc & 0xffff; ucRc = cRc;
WriteBytes[6] = ucRc & 0xFF;
WriteBytes[7] = ucRc >> 8;
}
float TempControl::getStepStateValue(QByteArray ReadBytes, int Proid, int AddressIde)
{
float Val;
long LongVal = 0;
if (Udian_CheckRc(ReadBytes, AddressIde) == 1)
{

LongVal = (unsigned char)ReadBytes[6] + (unsigned char)(ReadBytes[7]) * 256;
Val = LongVal;
return Val;

}
else
{
return -1;
}

}

//已經運行時間
void TempControl::getPonTimeCmd(QByteArray &WriteBytes, float Val, int Proid, int AddressIde)
{

int cRc = 0, ret = 0;
ushort ucRc = 0;
long LongVal = 0;
WriteBytes[0] = 0x80 + AddressIde;
WriteBytes[1] = 0x80 + AddressIde;
WriteBytes[2] = 0x52;
WriteBytes[3] = 0x2F;
WriteBytes[4] = 0;
WriteBytes[5] = 0;
cRc = WriteBytes[3] * 256 + 82 + AddressIde;
cRc = cRc & 0xffff; ucRc = cRc;
WriteBytes[6] = ucRc & 0xFF;
WriteBytes[7] = ucRc >> 8;
}
float TempControl::getPonTimeValue(QByteArray ReadBytes, int Proid, int AddressIde)
{
float Val;
long LongVal = 0;
if (Udian_CheckRc(ReadBytes, AddressIde) == 1)
{

LongVal = (unsigned char)ReadBytes[6] + (unsigned char)(ReadBytes[7]) * 256;
Val = LongVal;
return Val;

}
else
{
return -1;

}

 

}


void TempControl::SetAtCmd(QByteArray &WriteBytes, int AtVal, int Proid, int AddressIde) //自整定
{

int cRc = 0, ret = 0;
ushort ucRc = 0;
WriteBytes[0] = 0x80 + AddressIde;
WriteBytes[1] = 0x80 + AddressIde;
WriteBytes[2] = 0x43;
WriteBytes[3] = 0x1d;
WriteBytes[4] = AtVal % 256;
WriteBytes[5] = AtVal / 256;
cRc = WriteBytes[3] * 256;
cRc += WriteBytes[2];
cRc += WriteBytes[4];
cRc += WriteBytes[5] * 256;
cRc += AddressIde; cRc &= 0xFFFF; ucRc = cRc;
WriteBytes[6] = ucRc % 256;
WriteBytes[7] = ucRc / 256;
}

void TempControl::getAtCmd(QByteArray &WriteBytes, float Val, int Proid, int AddressIde) //自整定
{

int cRc = 0, ret = 0;
ushort ucRc = 0;
long LongVal = 0;
WriteBytes[0] = 0x80 + AddressIde;
WriteBytes[1] = 0x80 + AddressIde;
WriteBytes[2] = 0x52;
WriteBytes[3] = 0x1d;
WriteBytes[4] = 0;
WriteBytes[5] = 0;
cRc = WriteBytes[3] * 256 + 82 + AddressIde;
cRc = cRc & 0xffff; ucRc = cRc;
WriteBytes[6] = ucRc & 0xFF;
WriteBytes[7] = ucRc >> 8;

}
float TempControl::getAtValue(QByteArray ReadBytes, int Proid, int AddressIde) //自整定
{
float AtVal;
long LongVal = 0;
if (Udian_CheckRc(ReadBytes, AddressIde) == 1)
{

LongVal = (unsigned char)ReadBytes[6] + (unsigned char)(ReadBytes[7]) * 256;
AtVal = LongVal;
return AtVal;

}
else
{
return -1;

}

}

/*測試用例:獲取當前溫度值*/

#include <QtCore/QCoreApplication>
#include "TempControl.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TempControl *Yu_Dian;
Yu_Dian = new TempControl("com5",
QIODevice::ReadWrite,
9600,
QSerialPort::Data8,
QSerialPort::NoParity,
QSerialPort::NoFlowControl,
QSerialPort::OneStop);
Yu_Dian->Init();
while (1)
{
float fPV = Yu_Dian->getTemp();
qDebug() << "PV:" << fPV<< endl;;

}

return a.exec();}

相關文章
相關標籤/搜索