Example #1
0
        private static void StubFindSerialDevices(object sender, DoWorkEventArgs e)
        {
            DeviceInfo dinfo = null;
            
            string comport = (string)e.Argument;

            if (Thread.CurrentThread.Name == null)
                Thread.CurrentThread.Name = comport;

            var MeterTypes = Assembly.GetExecutingAssembly().GetTypes()
                                 .Where(t => t.IsClass) // Only include classes
                                 .Where(t => t.IsSubclassOf(typeof(AbstractMeter)))
                                 .Where(t => t.GetInterfaces().Contains(typeof(IMeter)))
                                 .OrderBy(t => t.Namespace)
                                 .ThenBy(t => t.Name);
            
            //for each com port test each meter type to see if its on that port
            foreach (var reflect in MeterTypes)
            {
                Type t = reflect.UnderlyingSystemType;
                IMeter meter = (IMeter)Activator.CreateInstance(t);

                //if failed to connect skip this meter
                if (!meter.Connect(comport))
                    continue;

                try
                {
                    Console.WriteLine("Testing " + comport + " for " + reflect.UnderlyingSystemType.ToString());
                    if (meter.Port.IsOpen && meter.IsMeterConnected(comport))
                    {
                        dinfo = new DeviceInfo();
                        dinfo.DeviceType = t;
                        dinfo.ComPortName = comport;
                        dinfo.HIDDevice = false;
                        dinfo.Device = meter;

                        lock (DevicesFound)
                        {
                            DevicesFound.Add(dinfo);
                            return; //found a device so return.  Can be removed to find all devices on a system.
                        }
                    }
                }//try
                finally
                {
                    meter.Dispose();
                }
            }
        }
Example #2
0
        private static void StubFindHIDDevices()
        {
            var MeterTypes = Assembly.GetExecutingAssembly().GetTypes()
                                 .Where(t => t.IsClass) // Only include classes
                                 .Where(t => t.IsSubclassOf(typeof(AbstractMeter)))
                                 .Where(t => t.GetInterfaces().Contains(typeof(IMeterHID)))
                                 .OrderBy(t => t.Namespace)
                                 .ThenBy(t => t.Name);

            foreach (var reflect in MeterTypes)
            {
                Type t = reflect.UnderlyingSystemType;
                using (IMeterHID meter = (IMeterHID)Activator.CreateInstance(t))
                {
                    if (meter.IsMeterConnected())
                    {
                        DeviceInfo di = new DeviceInfo();
                        di.DeviceType = t;
                        di.HIDDevice = true;
                        di.Device = meter;

                        lock (DevicesFound)
                        {
                            DevicesFound.Add(di);
                            return; //found a device so return.  Can be removed to find all HID devices on a system.
                        }
                    }

                    if (meter.Port.SpecifiedDevice != null)
                        meter.Port.SpecifiedDevice.Dispose();

                    meter.Port.Dispose();
                }
            }
        }