S19解析

#ifndef CS19_H
#define CS19_H
 
#include 
 
const quint8 MAX_S19_LINE_COUNT_LENGHT = 76;
const quint8 MIN_S19_LINE_COUNT_LENGHT = 10;
const quint8 MAX_S19_LINE_LENGHT = 156;
 
typedef enum __tagS19ErrorCode
{
    S19_NO_ERROR = 0,
    S19_FORMAT_ERROR,
    S19_VERIFY_ERROR,
    S19_LENGHT_ERROR,
    S19_USERPAPR_EEROR,
}ES19ErrorCode;
 
typedef enum __tagS19Type
{
    S0 = 0,
    S1,
    S2,
    S3,
    S5,
    S7,
    S8,
    S9,
    S_MAX,
}emS19Type;
 
typedef struct __tagS19LineData
{
    emS19Type   type;
    quint8      count;
    quint32     address;
    quint8      data[100];
    quint8      checksum;
    quint8      datalen;
}stS19LineData;
 
class CS19
{
public:
    CS19();
    ES19ErrorCode getS19LineData(QByteArray bydata,stS19LineData *p);
 
private:
    char ConvertHexChar(char ch);
};
 
#endif // CS19_H

 

#include "Cs19.h"
 
const QString S19TypeTable[8] =
{
    "S0","S1","S2","S3","S5","S7","S8","S9"
};
 
CS19::CS19()
{
 
}
 
char CS19::ConvertHexChar(char ch)
{
    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);
}
 
ES19ErrorCode CS19::getS19LineData(QByteArray bydata,stS19LineData *p)
{
    quint16 i = 0;
    quint8 cs_temp = 0;
    QString str(bydata);
    char *pcdata = bydata.data();
    quint32 linelen = str.size();
 
    if(linelen < MIN_S19_LINE_COUNT_LENGHT)
    {
        qDebug("S19_LENGHT_ERROR");
        return S19_LENGHT_ERROR;
    }
 
    //获取Type
    QString stype = str.mid(0,2);
    for(i = 0; i < S_MAX; i++)
    {
        if(stype == S19TypeTable[i])
        {
            p->type = (emS19Type)i;
            break;
        }
    }
    if(i == S_MAX) {qDebug("S19_FORMAT_ERROR");return S19_FORMAT_ERROR;}
 
    //获取count
    p->count = (ConvertHexChar(*(pcdata + 2)) << 4) | ConvertHexChar(*(pcdata + 3));
    cs_temp += p->count;
    if(p->count != ((linelen / 2) - 3))
    {
        qDebug("S19_FORMAT_ERROR");
        return S19_FORMAT_ERROR;
    }
    //获取address
    if(p->type == S0)//2 Bytes:0x0000填充
    {
        p->address = 0x0;
    }
    else if((p->type == S1) || (p->type == S5) || (p->type == S9))//2 Bytes
    {
        p->address = (ConvertHexChar(*(pcdata + 4)) << 12) | \
                     (ConvertHexChar(*(pcdata + 5)) << 8) | \
                     (ConvertHexChar(*(pcdata + 6)) << 4) | \
                     (ConvertHexChar(*(pcdata + 7)));
 
        cs_temp += (p->address >> 8) & 0x0FF;
        cs_temp += p->address & 0x0FF;
 
        //获取data
        for(i = 0; i < p->count - 3; i++)
        {
            p->data[i] = (ConvertHexChar(*(pcdata + 2*i + 8)) << 4) | ConvertHexChar(*(pcdata + 2*i + 9));
            cs_temp += p->data[i];
        }
        //获取checksum
        p->checksum = (ConvertHexChar(*(pcdata + 2*i + 8)) << 4) | ConvertHexChar(*(pcdata + 2*i + 9));
        if(p->checksum != (0xFF - cs_temp))
        {
            qDebug("S19_VERIFY_ERROR");
            return S19_VERIFY_ERROR;
        }
    }
    else if((p->type == S2) || (p->type == S8))//3 Bytes
    {
        p->address = (ConvertHexChar(*(pcdata + 4)) << 20) | \
                     (ConvertHexChar(*(pcdata + 5)) << 16) | \
                     (ConvertHexChar(*(pcdata + 6)) << 12) | \
                     (ConvertHexChar(*(pcdata + 7)) << 8) | \
                     (ConvertHexChar(*(pcdata + 8)) << 4) | \
                     (ConvertHexChar(*(pcdata + 9)));
 
        cs_temp += (p->address >> 16) & 0x0FF;
        cs_temp += (p->address >> 8) & 0x0FF;
        cs_temp += p->address & 0x0FF;
 
        //获取data
        for(i = 0; i < p->count - 4; i++)
        {
            p->data[i] = (ConvertHexChar(*(pcdata + 2*i + 10)) << 4) | ConvertHexChar(*(pcdata + 2*i + 11));
            cs_temp += p->data[i];
        }
        //获取checksum
        p->checksum = (ConvertHexChar(*(pcdata + 2*i + 10)) << 4) | ConvertHexChar(*(pcdata + 2*i + 11));
        if(p->checksum != (0xFF - cs_temp))
        {
            qDebug("S19_VERIFY_ERROR");
            return S19_VERIFY_ERROR;
        }
    }
    else if((p->type == S3) || (p->type == S7))//4 Bytes
    {
        p->address = (ConvertHexChar(*(pcdata + 4)) << 28) | \
                     (ConvertHexChar(*(pcdata + 5)) << 24) | \
                     (ConvertHexChar(*(pcdata + 6)) << 20) | \
                     (ConvertHexChar(*(pcdata + 7)) << 16) | \
                     (ConvertHexChar(*(pcdata + 8)) << 12) | \
                     (ConvertHexChar(*(pcdata + 9)) << 8) | \
                     (ConvertHexChar(*(pcdata + 10)) << 4) | \
                     (ConvertHexChar(*(pcdata + 11)));
 
        cs_temp += (p->address >> 24) & 0x0FF;
        cs_temp += (p->address >> 16) & 0x0FF;
        cs_temp += (p->address >> 8) & 0x0FF;
        cs_temp += p->address & 0x0FF;
 
        //获取data
        for(i = 0; i < p->count - 5; i++)
        {
            p->data[i] = (ConvertHexChar(*(pcdata + 2*i + 12)) << 4) | ConvertHexChar(*(pcdata + 2*i + 13));
            cs_temp += p->data[i];
        }
        //获取checksum
        p->checksum = (ConvertHexChar(*(pcdata + 2*i + 12)) << 4) | ConvertHexChar(*(pcdata + 2*i + 13));
        if(p->checksum != (0xFF - cs_temp))
        {
            qDebug("S19_VERIFY_ERROR");
            return S19_VERIFY_ERROR;
        }
    }
    //获取datalen
    //if(p->type == S0)       p->datalen = p->count - 3;
    if(p->type == S1)  p->datalen = p->count - 3;
    else if(p->type == S2)  p->datalen = p->count - 4;
    else if(p->type == S3)  p->datalen = p->count - 5;
    else                    p->datalen = 0;
 
    return S19_NO_ERROR;
}

 

你可能感兴趣的:(MCU)