tool/MasterDevice.cpp
changeset 1826 ec6223c3b7ec
parent 1804 742607c464c4
child 1831 1875b9fea0ba
--- a/tool/MasterDevice.cpp	Tue Feb 23 17:40:46 2010 +0100
+++ b/tool/MasterDevice.cpp	Wed Feb 24 16:27:11 2010 +0100
@@ -42,10 +42,11 @@
 
 /****************************************************************************/
 
-MasterDevice::MasterDevice()
-{
-    index = 0;
-    fd = -1;
+MasterDevice::MasterDevice(unsigned int index):
+    index(index),
+    masterCount(0U),
+    fd(-1)
+{
 }
 
 /****************************************************************************/
@@ -69,6 +70,7 @@
     stringstream deviceName;
 
     if (fd == -1) { // not already open
+        ec_ioctl_module_t module_data;
         deviceName << "/dev/EtherCAT" << index;
 
         if ((fd = ::open(deviceName.str().c_str(),
@@ -78,6 +80,16 @@
                 << strerror(errno);
             throw MasterDeviceException(err);
         }
+
+        getModule(&module_data);
+        if (module_data.ioctl_version_magic != EC_IOCTL_VERSION_MAGIC) {
+            stringstream err;
+            err << "ioctl() version magic is differing: "
+                << deviceName << ": " << module_data.ioctl_version_magic
+                << ", ethercat tool: " << EC_IOCTL_VERSION_MAGIC;
+            throw MasterDeviceException(err);
+        }
+        masterCount = module_data.master_count;
     }
 }
 
@@ -93,6 +105,17 @@
 
 /****************************************************************************/
 
+void MasterDevice::getModule(ec_ioctl_module_t *data)
+{
+    if (ioctl(fd, EC_IOCTL_MODULE, data) < 0) {
+        stringstream err;
+        err << "Failed to get module information: " << strerror(errno);
+        throw MasterDeviceException(err);
+    }
+}
+
+/****************************************************************************/
+
 void MasterDevice::getMaster(ec_ioctl_master_t *data)
 {
     if (ioctl(fd, EC_IOCTL_MASTER, data) < 0) {