From 9c991ec6c8a7c1a1e792d31178182da647d27d8d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alejandro=20L=C3=B3pez=20Gestoso?= <alopez@iri.upc.edu>
Date: Mon, 2 May 2016 14:00:39 +0000
Subject: [PATCH] Added a new test for the CAN class

---
 src/examples/test_candump.cpp | 60 +++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
 create mode 100755 src/examples/test_candump.cpp

diff --git a/src/examples/test_candump.cpp b/src/examples/test_candump.cpp
new file mode 100755
index 0000000..fb97713
--- /dev/null
+++ b/src/examples/test_candump.cpp
@@ -0,0 +1,60 @@
+#include "eventserver.h"
+#include "threadserver.h"
+#include "commexceptions.h"
+#include "can.h"
+#include <stdio.h>
+#include <unistd.h>
+#include <string>
+#include <iostream>
+#include <iomanip>
+
+#include <linux/can/raw.h>
+#include <linux/can.h>
+
+const std::string can_dev="slcan0";
+
+int main(int argc,char *argv[])
+{
+  CEventServer *event_server=CEventServer::instance();
+  std::list<std::string> events;
+  CCAN can_port("can_port");
+  CCAN can_port2("can_port2");
+  unsigned char data[8];
+  unsigned int can_id;
+  int i=0,len;
+  int event_id;
+  
+  events.push_back(can_port.get_new_frame_event_id());
+  events.push_back(can_port2.get_new_frame_event_id());
+  try{
+    can_port.open(can_dev);
+    can_port2.open(can_dev);
+    unsigned short int can_filter = (unsigned short int)(7 << 4);
+    can_port.add_id_filter(can_filter, 0x0f0, false);
+    can_filter = (unsigned short int)(1 << 4);
+    can_port2.add_id_filter(can_filter, 0x0f0, false);
+    while(1)
+    {
+      event_id = event_server->wait_first(events);
+      if (event_id == 0)
+      {
+        can_port.read_frame(&can_id,data,&len);
+        std::cout << "  " << can_dev << "  " << std::uppercase << std::hex <<(int)can_id << std::dec << "   [" <<(int)len << "]  ";
+        for(i=0;i<len;i++)
+          std::cout << std::uppercase << std::setw(2) << std::setfill('0') << std::hex << (int)data[i] << " ";
+        std::cout << std::dec << std::endl;
+
+      }
+      else if (event_id == 1)
+      {
+        can_port2.read_frame(&can_id,data,&len);
+        std::cout << "  " << can_dev << "  " << std::uppercase << std::hex <<(int)can_id << std::dec << "   [" <<(int)len << "]  ";
+        for(i=0;i<len;i++)
+          std::cout << std::uppercase << std::setw(2) << std::setfill('0') << std::hex << (int)data[i] << " ";
+        std::cout << std::dec << std::endl;
+      }
+    }
+  }catch(CCommException &e){
+    std::cout << e.what() << std::endl;
+  }
+}
-- 
GitLab