Browse Source

1.解决串口接收时分帧现象,使用定时器,进行超时判断

libo 2 years ago
parent
commit
b925f118bb
3 changed files with 75 additions and 36 deletions
  1. 68 33
      Line.cpp
  2. 3 0
      Line.h
  3. 4 3
      Tools.pro.user

+ 68 - 33
Line.cpp

@@ -164,7 +164,7 @@ Line::Line(QWidget *parent)
 	portui.comboBox = NULL;
 	down = 0;
 	portdialog = new QDialog();
-
+    timer_id_searil = 0;
 	portui.setupUi(portdialog);
     timerid=startTimer(280);
 }
@@ -1394,7 +1394,9 @@ void Line::SetControlDateTime()
     SerialSend(buf);
     qDebug("%s",qtime.toString("yyyy-MM-dd hh:mm:ss").toUtf8().data());
 }
-void Line::timerEvent(QTimerEvent *event)
+
+
+void Line::timerEvent_old(QTimerEvent *event)
 {
 	int i;
 	unsigned int int_val;
@@ -1449,35 +1451,35 @@ void Line::timerEvent(QTimerEvent *event)
             switch(sendflag)
             {
                 case 1:
-                ReadStore();
-                break;
+                    ReadStore();
+                    break;
                 case 2:
-                GetControlDateTime();
-                break;
-            case 3:
-                ReadAutoSealTime();
-                break;
-            case 4:
-                ReadConfig();
-                break;
-             case 5:
-                ReadCoefExp();
-                break;
-              case 6:
-                ReadUart1TimeOut();
-                break;
-              case 7:
-                ReadTankCapicity();
-                break;
-              case 8:
-                ReadBlankSeal();
-                break;
-               case 9:
-                ReadControlSoftwareVer();
-                break;
-               default:
-                sendflag=0;
-                return;
+                    GetControlDateTime();
+                    break;
+                case 3:
+                    ReadAutoSealTime();
+                    break;
+                case 4:
+                    ReadConfig();
+                    break;
+                case 5:
+                    ReadCoefExp();
+                    break;
+                case 6:
+                    ReadUart1TimeOut();
+                    break;
+                case 7:
+                    ReadTankCapicity();
+                    break;
+                case 8:
+                    ReadBlankSeal();
+                    break;
+                case 9:
+                    ReadControlSoftwareVer();
+                    break;
+                default:
+                    sendflag=0;
+                    return;
             }
             ++sendflag;
 #else
@@ -1489,6 +1491,19 @@ void Line::timerEvent(QTimerEvent *event)
 
 }
 
+void Line::timerEvent(QTimerEvent *event)
+{
+    if(event->timerId() == timer_id_searil)
+    {
+        //label2 每隔1秒+1
+        Receive_Data();
+    }
+    else
+    {
+        timerEvent_old(event);
+    }
+}
+
 void Line::readver()
 {
 	int retry;
@@ -1926,7 +1941,8 @@ void Line::on_openfile()
 	if (!fileName.isEmpty())
 	{
 		QFile qfile(fileName);
-		qDebug("%s", fileName.toUtf8());
+        fileName.toUtf8();
+        //qDebug("%s", fileName.toUtf8());
 		if (!qfile.open(QIODevice::ReadOnly))
 		{
 			return;
@@ -2086,14 +2102,33 @@ void Line::Receive_Data()
 
     if(myserial && myserial->isOpen())
     {
-        tt = myserial->readAll();
+        tt = myserial->readAll();                
         q = q+tt;
 
+        if(tt.isEmpty())
+        {
+
+        }
+        else
+        {
+            if( timer_id_searil != 0)
+            {
+                killTimer(timer_id_searil);
+            }
+            timer_id_searil = startTimer(50);
+            return;
+        }
         //qDebug("avans received: %x", str2));
     }
 
     {
-        if(recv_datalen == 0){
+        if(recv_datalen == 0)
+        {
+            if(q.isEmpty())
+            {
+                return;
+            }
+
             if((q.at(0)==0x39) && (q.at(1)==0x01)) //远程标定
             {
                 memcpy(recv_buffer+recv_datalen, q.data(), q.length());

+ 3 - 0
Line.h

@@ -134,6 +134,9 @@ public:
 	QThread tt;
 protected:
 	void timerEvent(QTimerEvent *event);
+    void timerEvent_old(QTimerEvent *event);
+    int timer_id_searil; //串口定时器ID
+    int timer_id_old; //串口定时器ID
 	
 private:
 	Ui::LineClass ui;

+ 4 - 3
Tools.pro.user

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE QtCreatorProject>
-<!-- Written by QtCreator 4.12.2, 2022-10-24T10:39:32. -->
+<!-- Written by QtCreator 4.12.2, 2022-10-26T17:37:05. -->
 <qtcreator>
  <data>
   <variable>EnvironmentId</variable>
@@ -308,8 +308,9 @@
     </valuelist>
     <value type="int" key="PE.EnvironmentAspect.Base">2</value>
     <valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
-    <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:D:/libo_20220905/product/Linetool/Linetool/Tools.pro</value>
-    <value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">D:/libo_20220905/product/Linetool/Linetool/Tools.pro</value>
+    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Tools2</value>
+    <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:D:/libo_20220905/product/Linetool/git_Linetool_boly/Tools.pro</value>
+    <value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">D:/libo_20220905/product/Linetool/git_Linetool_boly/Tools.pro</value>
     <value type="QString" key="RunConfiguration.Arguments"></value>
     <value type="bool" key="RunConfiguration.Arguments.multi">false</value>
     <value type="QString" key="RunConfiguration.OverrideDebuggerStartup"></value>