Browse Source

change driver_can_socket.

yuchuli 3 years ago
parent
commit
cb4615f0fb
2 changed files with 74 additions and 18 deletions
  1. 17 10
      src/driver/driver_can_socket/main.cpp
  2. 57 8
      src/driver/driver_can_socket/nvcan.cpp

+ 17 - 10
src/driver/driver_can_socket/main.cpp

@@ -50,6 +50,8 @@ void signal_handler(int sig)
 #ifdef TEST_PROG
 void threadtest()
 {
+    QTimer xTimer;
+    xTimer.setTimerType(Qt::PreciseTimer);
     void * pa = iv::modulecomm::RegisterSend("cansend0",100000,100);
     while(true)
     {
@@ -57,21 +59,26 @@ void threadtest()
         xmsg.set_channel(0);
         xmsg.set_mstime(QDateTime::currentMSecsSinceEpoch());
         xmsg.set_index(0);
-        iv::can::canraw * pcanraw = xmsg.add_rawmsg();
-        pcanraw->set_bext(false);
-        pcanraw->set_id(11);
-        pcanraw->set_bremote(false);
-        char xdata[8];
-        xdata[0] = 11;
-        pcanraw->set_data(xdata,8);
-        pcanraw->set_len(8);
-        int nbytesize = xmsg.ByteSize();
+        int i;
+        for(i=0;i<10;i++)
+        {
+            iv::can::canraw * pcanraw = xmsg.add_rawmsg();
+            pcanraw->set_bext(false);
+            pcanraw->set_id(11);
+            pcanraw->set_bremote(false);
+            char xdata[8];
+            xdata[0] = 11;
+            pcanraw->set_data(xdata,8);
+            pcanraw->set_len(8);
+        }
 
+        int nbytesize = xmsg.ByteSize();
         std::shared_ptr<char> str_ptr = std::shared_ptr<char>(new char[nbytesize]);
         if(xmsg.SerializeToArray(str_ptr.get(),nbytesize))
             iv::modulecomm::ModuleSendMsg(pa,str_ptr.get(),nbytesize);
 
-        std::this_thread::sleep_for(std::chrono::milliseconds(1));
+ //       std::this_thread::sleep_for(std::chrono::milliseconds(1));
+        std::this_thread::sleep_for(std::chrono::microseconds(5000));
     }
 }
 #endif

+ 57 - 8
src/driver/driver_can_socket/nvcan.cpp

@@ -248,12 +248,15 @@ void nvcan::run()
 void nvcan::threadsend()
 {
     int currmax = 1;
-    unsigned int i;
+    int i;
     while(mbCANOpen == false)
     {
         std::this_thread::sleep_for(std::chrono::milliseconds(1));
     }
 
+    qint64 nLastSecond = 0;
+    int nsecondsend = 0;
+    int nretry = 0;
     while(mbSendRun)
     {
         mWaitMutex.lock();
@@ -261,7 +264,7 @@ void nvcan::threadsend()
         mWaitMutex.unlock();
 
 #ifdef TEST_PROG
-        qDebug("send time : %lld",QDateTime::currentMSecsSinceEpoch());
+//        qDebug("send time : %lld",QDateTime::currentMSecsSinceEpoch());
 #endif
 
         struct canfd_frame framesend[2500];
@@ -298,10 +301,45 @@ void nvcan::threadsend()
             if(nsend > 0)
             {
                 for(i=0;i<nsend;i++)
-                if (write(mps[nch], &framesend[i],16) != 16) {
-                    mivlog->error("write error 1");
-                    perror("write error 1.");
-                    continue;
+                {
+                    if (write(mps[nch], &framesend[i],16) != 16) {
+                        mivlog->error("write error 1");
+ //                       perror("write error 1.");
+                        nretry++;
+                        if(nretry > 30)
+                        {
+                            std::cout<<"retry fail,retry:"<<nretry<<std::endl;
+                        }
+                        else
+                        {
+
+
+                        }
+                        if(nretry < 100)
+                        {
+                            i--;
+                        }
+                        else
+                        {
+                            std::cout<<"retry more than 10ms"<<std::endl;
+                        }
+                         std::this_thread::sleep_for(std::chrono::microseconds(100));
+ //                       std::cout<<"retry send."<<std::endl;
+                        continue;
+                    }
+                    else
+                    {
+                        nretry = 0;
+                        nsecondsend++;
+                        qint64 nnowsecond = QDateTime::currentSecsSinceEpoch();
+                        if( nnowsecond != nLastSecond)
+                        {
+                            nLastSecond = nnowsecond;
+                            std::cout<<" second send count: "<<nsecondsend<<std::endl;
+                            nsecondsend = 0;
+                        }
+
+                    }
                 }
             }
 
@@ -366,9 +404,20 @@ int nvcan::SetMessage(const int nch, basecan_msg *pMsg)
 {
     if((nch>1)||(nch < 0))return -1;
 
-    if(mMsgSendBuf[nch].size() > BUF_SIZE)return -2;
-
     mMutex.lock();
+    if(mMsgSendBuf[nch].size() > BUF_SIZE)
+    {
+        std::cout<<"buffer full."<<std::endl;
+        mMutex.unlock();
+        return -2;
+    }
+
+    if(mMsgRecvBuf[nch].size() > 100)
+    {
+        std::cout<<"buffer data more 100"<<std::endl;
+    }
+
+
     mMsgSendBuf[nch].push_back(*pMsg);
     mMutex.unlock();
     return 0;