Browse Source

change driver_can_socket. for use 1 thread send, and use waitcondition.

yuchuli 3 years ago
parent
commit
bdd0441391

+ 0 - 32
src/driver/driver_can_socket/basecan.cpp

@@ -1,32 +0,0 @@
-#include "basecan.h"
-
-basecan::basecan()
-{
-
-}
-
-basecan::~basecan()
-{
-    qDebug(" del basecan");
-}
-
-int basecan::GetMessage(const int nch,basecan_msg *pMsg, const int nCap)
-{
-    return 0;
-}
-
-int basecan::SetMessage(const int nch, basecan_msg *pMsg)
-{
-    return 0;
-}
-
-
-void basecan::startdev()
-{
-
-}
-
-void basecan::stopdev()
-{
-
-}

+ 0 - 38
src/driver/driver_can_socket/basecan.h

@@ -1,38 +0,0 @@
-#ifndef BASECAN_H
-#define BASECAN_H
-
-#include <QThread>
-
-#include "ivfault.h"
-#include "ivlog.h"
-
-class basecan_msg
-{
-  public:
-    unsigned int id;
-    bool isExtern;
-    bool isRemote;
-    unsigned char nLen;
-    unsigned char data[8];
-};
-
-class basecan : public QThread
-{
-    Q_OBJECT
-public:
-    basecan();
-    ~basecan();
-    virtual int GetMessage(const int nch,basecan_msg * pMsg,const int nCap);
-    virtual int SetMessage(const int nch,basecan_msg * pMsg); //Send Message
-
-    virtual void startdev();
-    virtual void stopdev();
-
-    iv::Ivfault *mfault = nullptr;
-    iv::Ivlog *mivlog = nullptr;
-signals:
-    void SIG_CANOPENSTATE(bool bCAN,int nR,const char * strres);
-    void SIGTEST();
-};
-
-#endif // BASECAN_H

+ 13 - 7
src/driver/driver_can_socket/canctrl.cpp

@@ -74,13 +74,7 @@ void canctrl::run()
             nSend1 = 0;
             nSend2 = 0;
 
-            mMutexcan1.lock();
-            for(i=0;i<msendmsgvector1.size();i++)
-            {
-                mpcan->SetMessage(0,&(msendmsgvector1.at(i)));
-            }
-            msendmsgvector1.clear();
-            mMutexcan1.unlock();
+
 
 
             proCanSt.set_b_canstate(true);
@@ -171,6 +165,18 @@ void canctrl::sendmsg(int index, iv::can::canmsg xmsg)
     }
     pMutex->unlock();
 
+    if(mbCANOpen)
+    {
+        mMutexcan1.lock();
+        for(i=0;i<msendmsgvector1.size();i++)
+        {
+            mpcan->SetMessage(0,&(msendmsgvector1.at(i)));
+        }
+        msendmsgvector1.clear();
+        mMutexcan1.unlock();
+        mpcan->CmdSend();
+    }
+
 
 }
 

+ 2 - 0
src/driver/driver_can_socket/canctrl.h

@@ -45,6 +45,8 @@ public:
     void sharecanmsg(void * xpa,basecan_msg * pxmsg,int ncount,int nch);
 
 
+
+
 };
 
 #endif // CANCTRL_H

+ 3 - 0
src/driver/driver_can_socket/driver_can_socket.pro

@@ -23,6 +23,9 @@ DEFINES += QT_DEPRECATED_WARNINGS
     error( "Couldn't find the ivprotobuf.pri file!" )
 }
 
+
+#DEFINES += TEST_PROG
+
 INCLUDEPATH += $$PWD/../../include/base/driver/can
 
 # You can also make your code fail to compile if you use deprecated APIs.

+ 33 - 0
src/driver/driver_can_socket/main.cpp

@@ -47,6 +47,35 @@ void signal_handler(int sig)
     }
 }
 
+#ifdef TEST_PROG
+void threadtest()
+{
+    void * pa = iv::modulecomm::RegisterSend("cansend0",100000,100);
+    while(true)
+    {
+        iv::can::canmsg xmsg;
+        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();
+
+        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));
+    }
+}
+#endif
+
 
 int main(int argc, char *argv[])
 {
@@ -73,6 +102,10 @@ int main(int argc, char *argv[])
 
     signal(SIGINT,signal_handler);
 
+#ifdef TEST_PROG
+    std::thread * pthread = new std::thread(threadtest);
+#endif
+
  //   std::thread b(func);
 
     return a.exec();

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

@@ -25,6 +25,7 @@
 #include <QDateTime>
 
 #include <iostream>
+#include  <thread>
 
 /* for hardware timestamps - since Linux 2.6.30 */
 #ifndef SO_TIMESTAMPING
@@ -65,6 +66,8 @@ nvcan::nvcan(const char * strcanname)
 
     mfault->SetFaultState(0,0,"Prepare Initialize.");
 
+    mpsendthread = new std::thread(&nvcan::threadsend,this);
+
 
 }
 
@@ -116,6 +119,7 @@ void nvcan::run()
         }
     }
 
+    mps = &s[0];
     mbCANOpen = true;
     mivlog->verbose("open can succesfully.");
     mfault->SetFaultState(0,0,"CAN OK.");
@@ -229,6 +233,37 @@ void nvcan::run()
             }
         }
 
+
+
+    }
+
+    for (i=0; i<currmax; i++)
+    {
+        close(s[i]);
+    }
+    qDebug("nvcan thread close.");
+    mbRunning = false;
+}
+
+void nvcan::threadsend()
+{
+    int currmax = 1;
+    unsigned int i;
+    while(mbCANOpen == false)
+    {
+        std::this_thread::sleep_for(std::chrono::milliseconds(1));
+    }
+
+    while(mbSendRun)
+    {
+        mWaitMutex.lock();
+        mwc.wait(&mWaitMutex,100);
+        mWaitMutex.unlock();
+
+#ifdef TEST_PROG
+        qDebug("send time : %lld",QDateTime::currentMSecsSinceEpoch());
+#endif
+
         struct canfd_frame framesend[2500];
 
         for(int nch =0;nch<currmax;nch++)
@@ -259,10 +294,11 @@ void nvcan::run()
             }
             mMsgSendBuf[nch].clear();
             mMutex.unlock();
+
             if(nsend > 0)
             {
                 for(i=0;i<nsend;i++)
-                if (write(s[nch], &framesend[i],16) != 16) {
+                if (write(mps[nch], &framesend[i],16) != 16) {
                     mivlog->error("write error 1");
                     perror("write error 1.");
                     continue;
@@ -270,15 +306,10 @@ void nvcan::run()
             }
 
         }
-
     }
 
-    for (i=0; i<currmax; i++)
-    {
-        close(s[i]);
-    }
-    qDebug("nvcan thread close.");
-    mbRunning = false;
+    std::cout<<"nvcan::threadsend exit."<<std::endl;
+
 }
 
 void nvcan::startdev()
@@ -343,6 +374,11 @@ int nvcan::SetMessage(const int nch, basecan_msg *pMsg)
     return 0;
 }
 
+void nvcan::CmdSend()
+{
+    mwc.wakeAll();
+}
+
 void nvcan::onMsg(bool bCAN, int nR, const char *strres)
 {
     mivlog->verbose("msg is %s ",strres);

+ 17 - 0
src/driver/driver_can_socket/nvcan.h

@@ -7,6 +7,9 @@
 
 #include <thread>
 
+#include <QWaitCondition>
+#include <QMutex>
+
 class nvcan : public basecan
 {
     Q_OBJECT
@@ -19,11 +22,17 @@ public:
     int GetMessage(const int nch,basecan_msg * pMsg,const int nCap);
     int SetMessage(const int nch,basecan_msg * pMsg);
 
+    virtual void CmdSend();
+
 private slots:
     void onMsg(bool bCAN,int nR,const char * strres);
 
 private:
     void run();
+    void threadsend();
+    int * mps = 0;
+
+
     std::vector<basecan_msg> mMsgRecvBuf[2];
 
     std::vector<basecan_msg> mMsgSendBuf[2];
@@ -33,6 +42,14 @@ private:
     bool mbCANOpen = false;
     bool mbRunning = false;
     int mnDevNum;
+
+private:
+    bool mbSendRun = true;
+
+    QWaitCondition mwc;
+    QMutex mWaitMutex;
+
+    std::thread * mpsendthread;
 };
 
 #endif // NVCAN_H

+ 5 - 0
src/include/base/driver/can/basecan.cpp

@@ -30,3 +30,8 @@ void basecan::stopdev()
 {
 
 }
+
+void basecan::CmdSend()
+{
+
+}

+ 3 - 0
src/include/base/driver/can/basecan.h

@@ -35,6 +35,9 @@ public:
 signals:
     void SIG_CANOPENSTATE(bool bCAN,int nR,const char * strres);
     void SIGTEST();
+
+public:
+    virtual void CmdSend();
 };
 
 #endif // BASECAN_H