zoukankan      html  css  js  c++  java
  • QT5串口读取宇电温控器温度

    宇电温控器AI5.X系列

    类库运行环境:win10+qt5+vs2017

    类说明:TempControl,提供获取当前温控器温度值,PV,SV,MV等值,调用公有函数接口,获得返回值。

    附带测试用例

    #ifndef _TEMPCONTROL_H_
    #define _TEMPCONTROL_H_

    #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 );

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

    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); //自整定

    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();
    }

    后知后觉、越学越菜
  • 相关阅读:
    设计模式 --单例模式
    Neor Profile SQL 中文汉化
    office online server 安装部署整合到到C#项目
    C# 线程池
    WinForm版 屏幕截图
    golang-nsq高性能消息队列
    【Go Time】Go语言里的条件语句else、switch
    【Go Time】Go语言常量定义关键字const
    【Go Time】Go定义变量
    【Go Time】Go语言里的空接口
  • 原文地址:https://www.cnblogs.com/chenhuanting/p/10824935.html
Copyright © 2011-2022 走看看