Home > Mobile >  Qt questions about serial data sending and receiving
Qt questions about serial data sending and receiving

Time:09-24

This is I write connection serial command
 
Void MotorController: : connect_serial ()//slot function, the corresponding signal is connect_device button click ()
{
Foreach (const QSerialPortInfo & amp; Sinfo, QSerialPortInfo: : availablePorts ()) {
MCSP - & gt; SetPort (sinfo);//MCSP is a pointer to the QSerialPort class
MCSP - & gt; Open (QIODevice: : ReadWrite);
QByteArray checkDevice;
QString2Hex (" 02504 d50312f313030300d0a ", checkDevice);//convert the string into hexadecimal character groups
MCSP - & gt; Write (checkDevice);
If (check_serial ())
break;
}
}

Bool MotorController: : check_serial ()//check if connected to the target device
{
Bool check_link=0;
QByteArray wished_reply;
QString2Hex (43 09 50 "4 d 50 31 0 0 a" d, wished_reply);
QByteArray readAnswer;
ReadAnswer=* buf;//buf is defined as: QByteArray * buf=new QByteArray; From serial port is used to store the data received
ShowLinkStatus - & gt; SetText (readAnswer);//showLinkStatus is a QLabel, initial state what words are not
If (readAnswer!=wished_reply)
MCSP - & gt; Close ();
The else {
Check_link=1;
Connect_device - & gt; Hide ();
}
Return check_link;
}

Void MotorController: : the read_data ()//this is readyRead () signal connection slot
{
* buf=MCSP - & gt; ReadAll ();
}

After the operation found the equipment doesn't receive all checkDevice command, showLinkStatus without any text, connect_device is not hidden,
Then the check_serial function make the following changes in the if statement
 
If (readAnswer!=wished_reply)
/* MCSP - & gt; Close () */.
The else {
Check_link=1;
Connect_device - & gt; Hide ();
}

Run again found equipment successfully received the command (turntable rotation), but there is no any text, showLinkStatus connect_device is not hidden,
Then rewrite the code
 
QByteArray MotorController: : the read_data ()
{
QByteArray buf1;
Buf1=MCSP - & gt; ReadAll ();
Return buf1;
}

Void MotorController: : connect_serial ()
{
Foreach (const QSerialPortInfo & amp; Sinfo, QSerialPortInfo: : availablePorts ()) {
MCSP - & gt; SetPort (sinfo);
MCSP - & gt; Open (QIODevice: : ReadWrite);
QByteArray checkDevice;
QString2Hex (" 02504 d50312f313030300d0a ", checkDevice);
MCSP - & gt; Write (checkDevice);
If (check_serial ())
break;
}
}

Bool MotorController: : check_serial ()
{
Bool check_link=0;
QByteArray wished_reply;
QString2Hex (43 09 50 "4 d 50 31 0 0 a" d, wished_reply);
QByteArray readAnswer;
ReadAnswer=read_data ();
ShowLinkStatus - & gt; SetText (readAnswer);
If (readAnswer!=wished_reply)
/* MCSP - & gt; Close () */.
The else {
Check_link=1;
Connect_device - & gt; Hide ();
}
Return check_link;
}

After the operation found: according to the next connect_device, turntable rotation but showLinkStatus without any text, connect_device no hidden; According to the second time that turntable rotation, showLinkStatus appear normal text, connect_device hidden,
If the MCSP - & gt; Close () is not commented out three kinds of code can make turntable rotation, and there was no text, showLinkStatus connect_device no hidden
Excuse me you appear what is the cause of the above situation, I want to be after the first press connect_device reached turntable rotation, showLinkStatus appear normal text, connect_device hidden such effect change how to do?
Wish you a great god glad,

CodePudding user response:

You can specify a serial port a try, don't enumerate all of the serial port to send

CodePudding user response:

reference 1st floor feiyangqingyun response:
you can specify a serial port a try, don't enumerate all serial port to send

I use and equipment to connect the connecting line is the USB to RS232, the computer itself to identify the serial port is this cable, I don't enumeration connection and testing whether it is right that I once can't be used in the program after connecting,
What other solution? Or show me what is the cause of the problem,

CodePudding user response:

Post is too long, the building may have been resolved, but I still under: you should be check the wrong time when check readready haven't feedback data, the check code should be moved to the read_data slot () function inside, just add a condition judgment, next time do not need to continue to check and accept the normal data line,
  •  Tags:  
  • Qt
  • Related