public TruckBusParameters()
      {
         this.BusInterface = BusInterfaces.USBA;
         this.BitRate = 0;

         this.ConsumerHeartbeatRate = 0;
         this.ProducerHeartbeatRate = 0;
         this.ControllerBusId = 0;

         this.ReelMotorBusId = 0;
         this.ReelDigitalBusId = 0;
         this.ReelAnalogBusId = 0;
         this.ReelEncoderBusId = 0;
         this.FeederTopFrontMotorBusId = 0;
         this.FeederTopRearMotorBusId = 0;
         this.FeederBottomFrontMotorBusId = 0;
         this.FeederBottomRearMotorBusId = 0;
         this.FeederEncoderBusId = 0;
         this.GuideLeftMotorBusId = 0;
         this.GuideRightMotorBusId = 0;
         this.LaunchDigitalIoBusId = 0;
         this.LaunchAnalogIoBusId = 0;
         this.GpsBusId = 0;
         this.FrontPumpBusId = 0;
         this.FrontScaleRs232BusId = 0;
         this.RearPumpBusId = 0;
         this.RearScaleRs232BusId = 0;

         this.ControllerTraceMask = 0;
         this.ReelMotorTraceMask = 0;
         this.ReelDigitalTraceMask = 0;
         this.ReelAnalogTraceMask = 0;
         this.ReelEncoderTraceMask = 0;
         this.FeederTopFrontMotorTraceMask = 0;
         this.FeederTopRearMotorTraceMask = 0;
         this.FeederBottomFrontMotorTraceMask = 0;
         this.FeederBottomRearMotorTraceMask = 0;
         this.FeederEncoderTraceMask = 0;
         this.GuideLeftMotorTraceMask = 0;
         this.GuideRightMotorTraceMask = 0;
         this.LaunchDigitalIoTraceMask = 0;
         this.LaunchAnalogIoTraceMask = 0;
         this.GpsTraceMask = 0;
         this.FrontPumpTraceMask = 0;
         this.FrontScaleRs232TraceMask = 0;
         this.RearPumpTraceMask = 0;
         this.RearScaleRs232TraceMask = 0;
      }
      public RobotBusParameters()
      {
         this.BusInterface = BusInterfaces.USBA;
         this.BitRate = 0;

         this.ConsumerHeartbeatRate = 0;
         this.ProducerHeartbeatRate = 0;
         this.ControllerBusId = 0;

         this.RobotBodyBusId = 0;
         this.RobotTopFrontWheelBusId = 0;
         this.RobotTopRearWheelBusId = 0;
         this.RobotBottomFrontWheelBusId = 0;
         this.RobotBottomRearWheelBusId = 0;

         this.ControllerTraceMask = 0;
         this.RobotBodyTraceMask = 0;
         this.RobotTopFrontWheelTraceMask = 0;
         this.RobotTopRearWheelTraceMask = 0;
         this.RobotBottomFrontWheelTraceMask = 0;
         this.RobotBottomRearWheelTraceMask = 0;
      }
Example #3
0
      private void ActivityButton_Click(object sender, EventArgs e)
      {
         if (false == this.active)
         {
            this.busAReceiveQueue.Clear();
            this.busBReceiveQueue.Clear();

            this.busASelected = (0 != this.BusAInterfaceComboBox.SelectedIndex);
            this.busBSelected = (0 != this.BusBInterfaceComboBox.SelectedIndex);

            if (false != this.busASelected)
            {
               this.busInterfaceA = (BusInterfaces)this.BusAInterfaceComboBox.SelectedItem;
               CANResult startResult = PCANLight.Start(this.busInterfaceA, 50000, FramesType.INIT_TYPE_ST, TraceGroup.TBUS, this.BusAReceiveHandler);
               this.busASelected = (CANResult.ERR_OK == startResult);

               if (false != this.busASelected)
               {
                  foreach (DeviceControl deviceControl in this.busADeviceList)
                  {
                     deviceControl.PowerUp();
                  }
               }
               else
               {
                  this.StatusLabel.Text = "Unable to start bus A interface.";
               }
            }

            if (false != this.busBSelected)
            {
               this.busInterfaceB = (BusInterfaces)this.BusBInterfaceComboBox.SelectedItem;
               CANResult startResult = PCANLight.Start(this.busInterfaceB, 50000, FramesType.INIT_TYPE_ST, TraceGroup.TBUS, this.BusBReceiveHandler);
               this.busBSelected = (CANResult.ERR_OK == startResult);

               if (false != this.busBSelected)
               {
                  foreach (DeviceControl deviceControl in this.busBDeviceList)
                  {
                     deviceControl.PowerUp();
                  }
               }
               else
               {
                  this.StatusLabel.Text = "Unable to start bus B interface.";
               }
            }

            if ((false != busASelected) || (false != busBSelected))
            {
               if ((false != this.busASelected) && (false != this.busBSelected))
               {
                  this.StatusLabel.Text = "Buses powered.";
               }
               else if (false != this.busASelected)
               {
                  this.StatusLabel.Text = "Bus A powered.";
               }
               else if (false != this.busBSelected)
               {
                  this.StatusLabel.Text = "Bus B powered.";
               }

               this.active = true;
               this.ActivityButton.Text = "Power Down";
               this.BusAInterfaceComboBox.Enabled = false;
               this.BusBInterfaceComboBox.Enabled = false;
               this.AddDeviceButton.Enabled = false;
               this.DeviceComboBox.Enabled = false;
               this.BusComboBox.Enabled = false;
               this.RemoveButton.Enabled = false;
               this.ReadButton.Enabled = false;
            }
            else
            {
               this.StatusLabel.Text = "Unable to power up.";
            }
         }
         else
         {
            if (false != busASelected)
            {
               foreach (DeviceControl deviceControl in this.busADeviceList)
               {
                  deviceControl.PowerDown();
               }

               PCANLight.Stop(this.busInterfaceA);
            }

            if (false != busBSelected)
            {
               foreach (DeviceControl deviceControl in this.busBDeviceList)
               {
                  deviceControl.PowerDown();
               }

               PCANLight.Stop(this.busInterfaceB);
            }

            this.StatusLabel.Text = "Devices powered down.";

            this.active = false;
            this.ActivityButton.Text = "Power Up";
            this.BusAInterfaceComboBox.Enabled = true;
            this.BusBInterfaceComboBox.Enabled = true;
            this.AddDeviceButton.Enabled = true;
            this.DeviceComboBox.Enabled = true;
            this.BusComboBox.Enabled = true;
            this.RemoveButton.Enabled = true;
            this.ReadButton.Enabled = true;
         }
      }
Example #4
0
      public static CANResult SendTimestamp(BusInterfaces busInterface, UInt32 timeStamp)
      {
         CANResult result = CANResult.ERR_BUSOFF;

         int busIndex = (int)busInterface;

         if (null == context[busIndex])
         {
            context[busIndex] = new BusContext();
         } 
         
         if (false != context[busIndex].active)
         {
            TCLightMsg msg;

            msg = new TCLightMsg();

            msg.ID = (uint)0x100;
            msg.Len = (byte)4;
            msg.MsgType = MsgTypes.MSGTYPE_STANDARD;

            msg.Data[0] = (byte)((timeStamp >> 0) & 0xFF);
            msg.Data[1] = (byte)((timeStamp >> 8) & 0xFF);
            msg.Data[2] = (byte)((timeStamp >> 16) & 0xFF);
            msg.Data[3] = (byte)((timeStamp >> 24) & 0xFF);

            result = PCANLight.Write(context[busIndex].activeHardware, msg);

            if (CANResult.ERR_OK != result)
            {
               Tracer.WriteError(context[busIndex].traceGroup, "", "timestamp send failure {0}", timeStamp);
            }
            else
            {
               Tracer.WriteError(context[busIndex].traceGroup, "", "timestamp");
            }
         }

         return (result);
      }
Example #5
0
      public static CANResult SendSync(BusInterfaces busInterface)
      {
         CANResult result = CANResult.ERR_BUSOFF;

         int busIndex = (int)busInterface;

         if (null == context[busIndex])
         {
            context[busIndex] = new BusContext();
         } 
         
         if (false != context[busIndex].active)
         {
            TCLightMsg msg;

            msg = new TCLightMsg();

            msg.ID = (uint)0x080;
            msg.Len = (byte)0;
            msg.MsgType = MsgTypes.MSGTYPE_STANDARD;

            result = PCANLight.Write(context[busIndex].activeHardware, msg);

            if (CANResult.ERR_OK != result)
            {
               Tracer.WriteError(context[busIndex].traceGroup, "", "sync send failure {0}", result.ToString());
            }
            else
            {
               Tracer.WriteError(context[busIndex].traceGroup, "", "sync");
            }
         }

         return (result);
      }
Example #6
0
      public static CANResult Send(BusInterfaces busInterface, int id, byte[] data)
      {
         CANResult result = CANResult.ERR_BUSOFF;

         int busIndex = (int)busInterface;

         if (null == context[busIndex])
         {
            context[busIndex] = new BusContext();
         }

         if (false != context[busIndex].active)
         {
            TCLightMsg msg;

            msg = new TCLightMsg();

            msg.ID = (uint)id;
            msg.Len = (byte)data.Length;
            msg.MsgType = MsgTypes.MSGTYPE_STANDARD;

            for (int i = 0; i < data.Length; i++)
            {
               msg.Data[i] = data[i];
            }

            result = PCANLight.Write(context[busIndex].activeHardware, msg);

            if (CANResult.ERR_OK != result)
            {
               StringBuilder sb = new StringBuilder();

               for (int i = 0; i < data.Length; i++)
               {
                  sb.AppendFormat("{0:X2}", data[i]);
               }

               Tracer.WriteError(context[busIndex].traceGroup, "", "send failure {0} data {1}", result, sb.ToString());
            }
         }

         return (result);
      }
Example #7
0
      /// <summary>
      /// Function to stop interface.
      /// </summary>
      /// <remarks>
      /// PCAN returns error when another application is monitoring bus.
      /// </remarks>
      /// <param name="busInterface">interface to access</param>
      public static void Stop(BusInterfaces busInterface)
      {
         int busIndex = (int)busInterface;

         if (null == context[busIndex])
         {
            context[busIndex] = new BusContext();
         }

         if (null != context[busIndex].receiveThread)
         {
            context[busIndex].receiveThreadExecute = false;
            context[busIndex].receiveEvent.Set();
            context[busIndex].receiveThread.Join(3000);
            context[busIndex].receiveThread = null;
         }

         if (null != context[busIndex].receiveEvent)
         {
            context[busIndex].receiveEvent.Close();
            context[busIndex].receiveEvent = null;
         }

         if (false != context[busIndex].active)
         {
            CANResult closeResult = CANResult.ERR_OK;
            closeResult = PCANLight.Close(context[busIndex].activeHardware);

            if (CANResult.ERR_OK == closeResult)
            {
               Tracer.WriteHigh(context[busIndex].traceGroup, "", "stopped");
            }
            else
            {
               Tracer.WriteError(context[busIndex].traceGroup, "", "stop error {0}", closeResult.ToString());
            }

            context[busIndex].active = false;
         }
      }
Example #8
0
      public static CANResult Start(BusInterfaces busInterface, int bitRate, FramesType messageType, TraceGroup traceGroup, ReceiveDelegateHandler receiveHandler, int ioPort = -1, short interrupt = -1)
      {
         int busIndex = (int)busInterface;

         if (null == context[busIndex])
         {
            context[busIndex] = new BusContext();
         }

         HardwareType hardwareType = GetHardwareType(busInterface);
         Baudrates interfaceBaudRate = GetInterfaceBaudRate(bitRate);
			CANResult result = CANResult.ERR_NETINUSE;

         if (false == context[busIndex].active)
         {
            result = CANResult.ERR_OK;
            string dllVersionString = "";
            uint nodeId = 0;

            if (CANResult.ERR_OK == result)
            {
               CANResult dllVersionResult = PCANLight.DllVersionInfo(hardwareType, out dllVersionString);

               if (CANResult.ERR_OK == dllVersionResult)
               {
                  int majorVersion = 0;

                  String[] versionTabInfo = dllVersionString.Split('.');

                  if (versionTabInfo.Length > 0)
                  {
                     Int32.TryParse(versionTabInfo[0].ToString(), out majorVersion);
                  }

                  if (majorVersion < 2)
                  {
                     Tracer.WriteError(traceGroup, "", "DLL version 2.x or higher needed, version \"{0}\" invalid", dllVersionString);
                  }
               }
               else
               {
                  Tracer.WriteError(traceGroup, "", "DLL version error {0}", dllVersionResult.ToString());
                  result = dllVersionResult;
               }
            }

            if (CANResult.ERR_OK == result)
            {
               CANResult initResult = CANResult.ERR_OK;

               if ((-1 != ioPort) && (-1 != interrupt))
               {
                  initResult = PCANLight.Init(hardwareType, interfaceBaudRate, messageType, (uint)ioPort, (ushort)interrupt);
               }
               else
               {
                  initResult = PCANLight.Init(hardwareType, interfaceBaudRate, messageType);
               }

               if (CANResult.ERR_OK != initResult)
               {
                  Tracer.WriteError(traceGroup, "", "DLL init error {0}", initResult.ToString());
                  result = initResult;
               }
            }

            if (CANResult.ERR_OK == result)
            {
               CANResult resetResult = PCANLight.ResetClient(hardwareType);

               if (CANResult.ERR_OK != resetResult)
               {
                  Tracer.WriteError(traceGroup, "", "DLL reset error {0}", resetResult.ToString());
                  result = resetResult;
               }
            }

            if ((HardwareType.USB_1CH == hardwareType) || (HardwareType.USB_2CH == hardwareType))
            {
               if (CANResult.ERR_OK == result)
               {
                  CANResult deviceNumberResult = CANResult.ERR_OK;
                  deviceNumberResult = PCANLight.GetUSBDeviceNr(hardwareType, out nodeId);

                  if (CANResult.ERR_OK != deviceNumberResult)
                  {
                     Tracer.WriteError(traceGroup, "", "DLL device number error {0}", deviceNumberResult.ToString());
                     result = deviceNumberResult;
                  }
               }
            }

            if (CANResult.ERR_OK == result)
            {
               CANResult setEventResult = CANResult.ERR_OK;
               context[busIndex].receiveEvent = new AutoResetEvent(false);
               setEventResult = PCANLight.SetRcvEvent(hardwareType, context[busIndex].receiveEvent);

               if (CANResult.ERR_OK != setEventResult)
               {
                  Tracer.WriteError(traceGroup, "", "DLL receive event error {0}", setEventResult.ToString());
                  result = setEventResult;
               }
            }

            if (CANResult.ERR_OK == result)
            {
               context[busIndex].traceGroup = traceGroup;
               context[busIndex].receiverHandler = new ReceiveDelegateHandler(receiveHandler);
               context[busIndex].receiveThread = new Thread(() => ReceiveProcess(context[busIndex]));
               context[busIndex].receiveThread.IsBackground = true;
               context[busIndex].receiveThread.Name = "CAN " + busInterface.ToString() + " reader";

               context[busIndex].receiveThreadExecute = true;
               context[busIndex].receiveThread.Start();
            }

            if (CANResult.ERR_OK == result)
            {
               context[busIndex].activeHardware = hardwareType;
               context[busIndex].active = true;
               Tracer.WriteHigh(traceGroup, "", "started, version {0}, node id {1}", dllVersionString, nodeId);
            }
         }

         return (result);
      }
Example #9
0
      private static HardwareType GetHardwareType(BusInterfaces busInterface)
      {
         HardwareType result = HardwareType.DNG;

         if (BusInterfaces.PCIA == busInterface)
         {
            result = HardwareType.PCI_1CH;
         }
         else if (BusInterfaces.PCIB == busInterface)
         {
            result = HardwareType.PCI_2CH;
         }
         else if (BusInterfaces.USBA == busInterface)
         {
            result = HardwareType.USB_1CH;
         }
         else if (BusInterfaces.USBB == busInterface)
         {
            result = HardwareType.USB_2CH;
         }

         return (result);
      }
Example #10
0
      private void StartBus()
      {
         this.busReady = false;

         if (false == this.busReady)
         {
            this.busInterfaceId = ParameterAccessor.Instance.TruckBus.BusInterface;
            CANResult startResult = PCANLight.Start(this.busInterfaceId, ParameterAccessor.Instance.TruckBus.BitRate, FramesType.INIT_TYPE_ST, TraceGroup.TBUS, this.BusReceiveHandler);
            this.busReady = (CANResult.ERR_OK == startResult);
         }
         
         if (false != this.busReady)
         {
            PCANLight.ResetBus(this.busInterfaceId);

            DateTime busStartLimit = DateTime.Now.AddMilliseconds(BootTimeoutPeriod);

            for (; this.execute; )
            {
               bool allBooted = true;

               foreach (Device device in this.deviceList)
               {
                  allBooted = allBooted && device.ReceiveBootupHeartbeat;

                  if (false == allBooted)
                  {
                     break;
                  }
               }

               if ((false != allBooted) || (DateTime.Now > busStartLimit))
               {
                  break;
               }

               Thread.Sleep(1);
            }

            foreach (Device device in this.deviceList)
            {
               if (false == device.ReceiveBootupHeartbeat)
               {
                  device.Fault("boot timeout");
               }
            }
         }
         else
         {
            this.busStatus = "interface failure";
            Tracer.WriteMedium(TraceGroup.TBUS, "", "bus failure");

            foreach (Device device in this.deviceList)
            {
               device.Fault("interface not ready");
            }
         }
      }
Example #11
0
      public void Start(BusInterfaces busInterface, int bitRate, ref string result)
      {
         if (null == result)
         {
            this.busInterface = busInterface;
            CANResult startResult = PCANLight.Start(this.busInterface, bitRate, FramesType.INIT_TYPE_ST, TraceGroup.TBUS, this.BusReceiveHandler);
            result = (CANResult.ERR_OK != startResult) ? "Unable to start" : null;
         }

         if (null == result)
         {
            this.thread = new Thread(this.DeviceProcess);
            this.thread.IsBackground = true;
            this.thread.Name = "Bus Interface";

            this.execute = true;
            this.thread.Start();
         }
      }