예제 #1
0
      /// <summary>
      /// PCANLight Init function for Plug and Play Hardware.
      /// This function make the following:
      ///		- Activate a Hardware
      ///		- Make a Register Test of 82C200/SJA1000
      ///		- Allocate a Send buffer and a Hardware handle
      ///		- Programs the configuration of the transmit/receive driver
      ///		- Set the Baudrate register
      ///		- Set the Controller in RESET condition
      /// </summary>
      /// <param name="HWType">Which hardware should be initialized</param>
      /// <param name="BTR0BTR1">BTR0-BTR1 baudrate register</param>
      /// <param name="MsgType">f the frame type is standard or extended</param>
      /// <returns>A CANResult value - Error/status of the hardware after execute the function</returns>
      public static CANResult Init(HardwareType HWType, Baudrates BTR0BTR1, FramesType MsgType)
      {
         try
         {
            switch (HWType)
            {
               case HardwareType.PCI_1CH:
                  return (CANResult)PCAN_PCI.Init((ushort)BTR0BTR1, (int)MsgType);

               case HardwareType.PCI_2CH:
                  return (CANResult)PCAN_2PCI.Init((ushort)BTR0BTR1, (int)MsgType);

               case HardwareType.PCC_1CH:
                  return (CANResult)PCAN_PCC.Init((ushort)BTR0BTR1, (int)MsgType);

               case HardwareType.PCC_2CH:
                  return (CANResult)PCAN_2PCC.Init((ushort)BTR0BTR1, (int)MsgType);

               case HardwareType.USB_1CH:
                  return (CANResult)PCAN_USB.Init((ushort)BTR0BTR1, (int)MsgType);

               case HardwareType.USB_2CH:
                  return (CANResult)PCAN_2USB.Init((ushort)BTR0BTR1, (int)MsgType);

               // Hardware is not valid for this function
               //
               default:
                  return CANResult.ERR_ILLHW;
            }
         }
         catch (Exception Ex)
         {
            // Error: Dll does not exists or the function is not available
            //
            Tracer.WriteError(TraceGroup.CANBUS, null, "Init {0}", Ex.Message + "\"");
            return CANResult.ERR_NO_DLL;
         }
      }
예제 #2
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);
      }
예제 #3
0
      /// <summary>
      /// PCANLight Init function for non Plug and Play Hardware.  
      /// This function make the following:
      ///		- Activate a Hardware
      ///		- Make a Register Test of 82C200/SJA1000
      ///		- Allocate a Send buffer and a Hardware handle
      ///		- Programs the configuration of the transmit/receive driver
      ///		- Set the Baudrate register
      ///		- Set the Controller in RESET condition
      /// </summary>
      /// <param name="HWType">Which hardware should be initialized</param>
      /// <param name="BTR0BTR1">BTR0-BTR1 baudrate register</param>
      /// <param name="MsgType">If the frame type is standard or extended</param>
      /// <param name="IO_Port">Input/output Port Address of the hardware</param>
      /// <param name="Interrupt">Interrupt number</param>
      /// <returns>A CANResult value - Error/status of the hardware after execute the function</returns>
      public static CANResult Init(HardwareType HWType, Baudrates BTR0BTR1, FramesType MsgType, uint IO_Port, ushort Interrupt)
      {
         try
         {
            switch (HWType)
            {
               case HardwareType.ISA_1CH:
                  return (CANResult)PCAN_ISA.Init((ushort)BTR0BTR1, (int)MsgType, (int)Hardware.HW_ISA_SJA, IO_Port, Interrupt);

               case HardwareType.ISA_2CH:
                  return (CANResult)PCAN_2ISA.Init((ushort)BTR0BTR1, (int)MsgType, (int)Hardware.HW_ISA_SJA, IO_Port, Interrupt);

               case HardwareType.DNG:
                  return (CANResult)PCAN_DNG.Init((ushort)BTR0BTR1, (int)MsgType, (int)Hardware.HW_DONGLE_SJA, IO_Port, Interrupt);

               case HardwareType.DNP:
                  return (CANResult)PCAN_DNP.Init((ushort)BTR0BTR1, (int)MsgType, (int)Hardware.HW_DONGLE_PRO, IO_Port, Interrupt);

               // Hardware is not valid for this function
               //
               default:
                  return CANResult.ERR_ILLHW;
            }
         }
         catch (Exception Ex)
         {
            // Error: Dll does not exists or the function is not available
            //
            Tracer.WriteError(TraceGroup.CANBUS, null, "ppInit {0}", Ex.Message + "\"");
            return CANResult.ERR_NO_DLL;
         }
      }
예제 #4
0
파일: PCANLight.cs 프로젝트: sbtree/MST
        /// <summary>
        /// PCANLight Init function for Plug and Play Hardware.
        /// This function make the following:
        ///		- Activate a Hardware
        ///		- Make a Register Test of 82C200/SJA1000
        ///		- Allocate a Send buffer and a Hardware handle
        ///		- Programs the configuration of the transmit/receive driver
        ///		- Set the Baudrate register
        ///		- Set the Controller in RESET condition
        /// </summary>
        /// <param name="HWType">Which hardware should be initialized</param>
        /// <param name="BTR0BTR1">BTR0-BTR1 baudrate register</param>
        /// <param name="MsgType">f the frame type is standard or extended</param>
        /// <returns>A CANResult value - Error/status of the hardware after execute the function</returns>
        public static CANResult Init(HardwareType HWType, Baudrates BTR0BTR1, FramesType MsgType)
        {
            try
            {
                switch (HWType)
                {
                    case HardwareType.PCI_1CH:
                        return (CANResult)PCAN_PCI.Init((ushort)BTR0BTR1, (int)MsgType);

                    case HardwareType.PCI_2CH:
                        return (CANResult)PCAN_2PCI.Init((ushort)BTR0BTR1, (int)MsgType);

                    case HardwareType.PCC_1CH:
                        return (CANResult)PCAN_PCC.Init((ushort)BTR0BTR1, (int)MsgType);

                    case HardwareType.PCC_2CH:
                        return (CANResult)PCAN_2PCC.Init((ushort)BTR0BTR1, (int)MsgType);

                    case HardwareType.USB_1CH:
                        return (CANResult)PCAN_USB.Init((ushort)BTR0BTR1, (int)MsgType);

                    case HardwareType.USB_2CH:
                        return (CANResult)PCAN_2USB.Init((ushort)BTR0BTR1, (int)MsgType);

                    // Hardware is not valid for this function
                    //
                    default:
                        return CANResult.ERR_ILLHW;
                }
            }
            catch (Exception Ex)
            {
                // Error: Dll does not exists or the function is not available
                //
                System.Windows.Forms.MessageBox.Show("Error: \"" + Ex.Message + "\"");
                return CANResult.ERR_NO_DLL;
            }
        }