itas109 / CSerialPort

CSerialPort - lightweight cross-platform serial port library for C++/C/C#/Java/Python/Node.js/Electron
https://blog.csdn.net/itas109/article/details/84282860
Other
730 stars 339 forks source link

How to send/write Hex Data 如何发送/写入hex十六进制数据 #38

Closed itas109 closed 4 years ago

itas109 commented 4 years ago

How to send/write Hex Data

如何发送/写入hex十六进制数据

itas109 commented 4 years ago

direct to send Hex data

char sendStr[5] = {0};
sendStr[0] = 0x00;
sendStr[1] = 0x11;
sendStr[2] = 0x22;
sendStr[3] = 0x33;
sendStr[4] = 0x44;

m_serialPort.writeData(sendStr,sizeof(sendStr));
itas109 commented 4 years ago

if use QT, QString to Hex 使用QT,将QString字符串转为十六进制

QString sendStr = QString("0011223344");
QByteArray byteSend;
byteSend = QByteArray::fromHex(sendStr.toLocal8Bit());

 // suppot unicode str
m_serialPort.writeData(byteSend.constData(),byteSend.length());
itas109 commented 4 years ago

if use MFC, CString to Hex 使用MFC,将CString字符串转为十六进制

CString CCOMTOOLDlg::ChangeCharstr2Hexstr(CString Charstr)///字符转为十六进制
{
    CString Hexstr=_T("");
    Charstr.MakeUpper();///将字符串转为大写

    HexStringFilter(Charstr);///过滤非十六进制字符

    int Length=Charstr.GetLength();
    if(Length%2)///若不是偶数,删除最后一个字符
        Charstr.Delete(Length-1);

    Length=Charstr.GetLength();
    for(int i=0;i<Length/2;i++)///?
    {
        Hexstr+=CombineHexChar(Charstr.GetAt(i*2),Charstr.GetAt(i*2+1));
    }
    return Hexstr;
}

void CCOMTOOLDlg::HexStringFilter(CString &str)///十六进制过滤0-9 a-f A-F
{
    BOOL bOK;
    for(int i=0;i<str.GetLength();)///若该字符不是0-9或a-f或A-F,则删除
    {
        bOK=((str.GetAt(i)>='0')&&(str.GetAt(i)<='9'))||
            ((str.GetAt(i)>='A')&&(str.GetAt(i)<='F'))||
            ((str.GetAt(i)>='a')&&(str.GetAt(i)<='f'));
        if(!bOK)
            str.Delete(i);
        else i++;   
    }
}
char CCOMTOOLDlg::CombineHexChar(char CharH,char CharL) ///十六进制转为字符  CombineHexChar(A,B) result=1011;
{
    char result;
    CString temp;
    if(CharH>='0'&&CharH<='9')          result=(CharH-'0');
    else if(CharH>='a'&&CharH<='f')     result=(CharH-'a'+10);
    else if(CharH>='A'&&CharH<='F')     result=(CharH-'A'+10);
    else                                result=0;///执行HexStringFilter之后应该不会出现

    result<<=4; ///把数据左移4位
    if(CharL>='0'&&CharL<='9')          result+=(CharL-'0');
    else if(CharL>='a'&&CharL<='f')     result+=(CharL-'a'+10);
    else if(CharL>='A'&&CharL<='F')     result+=(CharL-'A'+10);
    else                                result+=0;
    return result;
}

send 发送

CString sendData= ChangeCharstr2Hexstr(CString("0011223344"));
m_serialPort.writeData((sendData.GetBuffer(sendData.GetLength()),sendData.GetLength());
itas109 commented 4 years ago

if use char, char to Hex 使用char,将char字符串转为十六进制

char * HexStrFilter(char *charStr, char *fiterStr)
{
    if (charStr == NULL || fiterStr == NULL)
    {
        return NULL;
    }

    int len = strlen(charStr);

    char *pTemp = fiterStr;

    for(int i = 0; i < len; i++)
    {
        if( ((charStr[i] >= '0') && (charStr[i] <= '9')) ||
            ((charStr[i] >= 'A') && (charStr[i] <= 'F')) ||
            ((charStr[i] >= 'a') && (charStr[i] <= 'f')) )
        {
            *fiterStr++ = charStr[i];
        }
    }

    return pTemp;
}

char CombineHexChar(char charH,char charL) /// CombineHexChar(A,B) result=1011;
{
    char result;

    if(charH >= '0' && charH <= '9')
    {
        result = (charH - '0');
    }
    else if(charH >= 'a' && charH <= 'f')
    {
        result = (charH - 'a'+10);
    }
    else if(charH >= 'A' && charH <= 'F')
    {
        result = (charH - 'A' + 10);
    }
    else
    {
        result = 0;/// need to fiter non-hex character
    }

    result <<= 4;

    if(charL >= '0' && charL <= '9')
    {
        result += (charL - '0');
    }
    else if(charL >= 'a' && charL <= 'f')
    {
        result += (charL - 'a'+10);
    }
    else if(charL >= 'A' && charL <= 'F')
    {
        result += (charL - 'A' + 10);
    }
    else
    {
        result += 0;
    }

    return result;
}

int Char2Hex(char *charStr,char *hexStr)/// character to hex, return value is hexStr length
{
    if (charStr == NULL || hexStr == NULL)
    {
        return 0;
    }

    int hexStrCount = 0;

    char *fiterStr = NULL;
    fiterStr = new char[strlen(charStr) + 1];
    memset(fiterStr,0,strlen(charStr) + 1);

    HexStrFilter(charStr,fiterStr);///filter non-hex character

    int len = strlen(fiterStr);

    // warn: if charStr length not even, the last charactor will lost
    for(int i=0;i < len/2; i++)
    {
        *hexStr++ = CombineHexChar(fiterStr[i*2],fiterStr[i*2+1]);
        hexStrCount++;
    }

    if(fiterStr)
    {
        delete [] fiterStr;
        fiterStr = NULL;
    }

    return hexStrCount;
}

send 发送

char charStr[] = "0123456789";
 int len = strlen(charStr);
char *hexStr = NULL;
hexStr = new char[len + 1];
memset(hexStr,0,len + 1);

int hexStrLen = 0;
hexStrLen = Char2Hex(charStr,hexStr);

m_serialPort.writeData(hexStr,hexStrLen );

if(hexStr)
{
    delete [] hexStr;
    hexStr = NULL;
}
itas109 commented 4 years ago

35