Realization of hexadecimal transceiver based on QT

Source: Internet
Author: User

Previous wrote a serial assistant with QT to do the blog post (based on Qt5.6 do a simple serial port with the transceiver function), do out of the serial assistant can only carry out the character of the transceiver, if the reception is the hexadecimal data, it will show garbled, and now on its basis to do some modification, so that the implementation of hexadecimal transceiver.

First I found two functions on the Internet, and then the transceiver function to make a simple modification, the content of the received the layout of some control. The following two functions are posted.

void Widget::stringtohex (QString str, qbytearray &senddata)//string converted to hexadecimal data 0-f {int hexdata,lowhexdata;
    int hexdatalen = 0;
    int len = Str.length ();
    Senddata.resize (LEN/2);

    Char lstr,hstr;
    for (int i=0; i<len;)
        {//char lstr, hstr=str[i].tolatin1 ();
            if (Hstr = = ") {i++;
        Continue
        } i++;
        if (i >= len) break;
        Lstr = Str[i].tolatin1 ();
        Hexdata = Converthexchar (HSTR);
        Lowhexdata = Converthexchar (LSTR); if ((hexdata = = 16) | |
            (Lowhexdata = = 16))
        Break
        else Hexdata = Hexdata*16+lowhexdata;
        i++;
        Senddata[hexdatalen] = (char) hexdata;
    hexdatalen++;
} senddata.resize (Hexdatalen); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
Char Widget::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 ch-ch;//not within the 0-f range will be sent to 0
}
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

Add these two functions to the source file and you can use them.

Remember that this is the definition part of the function, and also the private slots in the header file to declare the function.

Then modify the two functions that are sent and received:

Hair of

void widget::on_pushbutton_send_clicked ()
{
    QString str = ui->lineedit_write->text ();// Get the string
    int len = Str.length () from Lineedit;
    if (len%2 = = 1)   //If the number of data sent is odd, a character 0
    {
        str = str.insert (len-1, ' 0 ') is added before the last single character, and//insert (int position, Const QString & str)
    }
    qbytearray senddata;

     Stringtohex (str,senddata);//Convert str string to 16 binary form

     my_serialport->write (senddata);//Send to serial port
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Received by

void widget::readmycom ()//Read buffered data, read once per second
{
    Qbytearray temp = My_serialport->readall ();
    Qdatastream out (&temp,qiodevice::readwrite);    Reads a byte array into
    while (!out.atend ())
    {
           qint8 outchar = 0;
           out>>outchar;   Each byte is populated once until the end
           of the//16 binary conversion
           QString str = QString ("%1"). Arg (Outchar&0xff,2,16,qlatin1char (' 0 '));
           Ui->textbrowser->insertplaintext (Str.toupper ());//Uppercase
           Ui->textbrowser->insertplaintext ("");// Add a space after each two characters
           ui->textbrowser->movecursor (qtextcursor::end);
    }
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

After the change, the effect is this.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.