Exemple #1
0
        public bool StartLogging(string DirectoryForLogFile, bool MultipleFiles, bool TimestampName, UInt32 MaxMegabytes)
        {
            TPCANStatus stsResult; //result of call to PEAK
            UInt32      iBuffer;   //Int buffer to send to peak

            logging = false;       //true is logging turned on 9return value)

            if (Directory.Exists(DirectoryForLogFile))
            {
                ConfigureTraceFile(DirectoryForLogFile, MultipleFiles, TimestampName, MaxMegabytes);
            }
            else
            {
                ConfigureTraceFile("", MultipleFiles, TimestampName, MaxMegabytes);
                AddMessage(Feedback, "Directory for trace file: " + DirectoryForLogFile + ", not set (doesn't exist), default is exe location.");
            }

            iBuffer   = (uint)PCANBasic.PCAN_PARAMETER_ON;
            stsResult = PCANBasic.SetValue(PeakCANHandle, TPCANParameter.PCAN_TRACE_STATUS, ref iBuffer, sizeof(UInt32));
            if (stsResult == TPCANStatus.PCAN_ERROR_OK)
            {
                AddMessage(Feedback, "Trace log on");
                logging = true;
            }
            return(logging);
        }
Exemple #2
0
        /// <summary>
        /// Thread-Function used for reading PCAN-Basic messages
        /// </summary>
        private void CANReadThreadFunc()
        {
            UInt32 iBuffer;

            iBuffer = Convert.ToUInt32(ReceiveEvent.SafeWaitHandle.DangerousGetHandle().ToInt32());
            // Sets the handle of the Receive-Event.
            LastOperationStatus = PCANBasic.SetValue(PeakCANHandle, TPCANParameter.PCAN_RECEIVE_EVENT, ref iBuffer, sizeof(UInt32));

            if (LastOperationStatus != TPCANStatus.PCAN_ERROR_OK)
            {
                //throw new Exception(PeakCANStatusErrorString(LastOperationStatus));
            }

            if (ControlForm != null)
            {
                while (RxMessages)
                {
                    // Waiting for Receive-Event
                    if (ReceiveEvent.WaitOne(50))
                    {
                        // Process Receive-Event using .NET Invoke function
                        // in order to interact with Winforms UI (calling the
                        // function ReadMessage)
                        if (RxMessages)  //Double check reading is still required
                        {
                            ControlForm.Invoke(ReadDelegate);
                        }
                    }
                }
            }
            else
            {
                throw new Exception("No control form set.");
            }
        }
Exemple #3
0
        /// <summary>
        /// Get a list of connected devices
        /// </summary>
        /// <returns>List of devices</returns>
        public static List <string> GetUSBDevices()
        {
            //Uses the PEAK System USB1 to USB16 handles for the PCAN-USB devices
            TPCANStatus   dllRet = TPCANStatus.PCAN_ERROR_UNKNOWN; //Assume unknown state to start
            UInt32        iBuffer;                                 //Passed to PCAN dll in GetValue call
            List <string> PCANDevices = null;                      //The list of devices to return

            bool isFD;                                             //CAN Flexible Data Rate (not currently supported by PCAN_USB)

            for (int i = 0; i < handlesArray.Length; i++)
            {
                // Checks for a Plug&Play Handle and, depending upon the return value, include it
                // into the list of available hardware channels.
                dllRet = PCANBasic.GetValue(handlesArray[i], TPCANParameter.PCAN_CHANNEL_CONDITION, out iBuffer, sizeof(UInt32));
                if ((dllRet == TPCANStatus.PCAN_ERROR_OK) && ((iBuffer & PCANBasic.PCAN_CHANNEL_AVAILABLE) == PCANBasic.PCAN_CHANNEL_AVAILABLE || (iBuffer & PCANBasic.PCAN_CHANNEL_PCANVIEW) == PCANBasic.PCAN_CHANNEL_PCANVIEW))
                {
                    dllRet = PCANBasic.GetValue(handlesArray[i], TPCANParameter.PCAN_CHANNEL_FEATURES, out iBuffer, sizeof(UInt32));
                    isFD   = (dllRet == TPCANStatus.PCAN_ERROR_OK) && ((iBuffer & PCANBasic.FEATURE_FD_CAPABLE) == PCANBasic.FEATURE_FD_CAPABLE);
                    if (PCANDevices == null)
                    {
                        PCANDevices = new List <string>();
                    }
                    PCANDevices.Add(FormatChannelName(handlesArray[i], isFD));
                }
            }
            return(PCANDevices);
        }
Exemple #4
0
 /// <summary>
 /// Initialise the given PEAK USB device
 /// </summary>
 /// <param name="CANHandle">PEAK USB handle</param>
 /// <param name="CANBaudRate">Baud rate as displayed</param>
 /// <returns>Device operation status</returns>
 public TPCANStatus InitializeCAN(PCANHandle CANHandle, string CANBaudRate, bool EnableRead)
 {
     LastOperationStatus = PCANBasic.Initialize(CANHandle, CANBaudRateToPeakCANBaudRate(CANBaudRate), (TPCANType)0, 0, 0);
     InitializeMessage();
     //Setup receive
     if (EnableRead)
     {
         SetCANMessageRead();
     }
     return(LastOperationStatus);
 }
Exemple #5
0
        public bool StopLogging()
        {
            UInt32      iBuffer   = (uint)PCANBasic.PCAN_PARAMETER_OFF;
            TPCANStatus stsResult = PCANBasic.SetValue(PeakCANHandle, TPCANParameter.PCAN_TRACE_STATUS, ref iBuffer, sizeof(UInt32));

            if (stsResult == TPCANStatus.PCAN_ERROR_OK)
            {
                AddMessage(Feedback, "Trace log off");
                logging = false;
            }
            return(logging);
        }
Exemple #6
0
        /// <summary>
        /// Function for reading CAN messages on normal CAN devices
        /// Set to the ReadDelegate
        /// </summary>
        /// <returns>A TPCANStatus error code</returns>
        private void ReadMessage()
        {
            //TODO move to moduel level for performance
            TPCANMsg       CANMsg;
            TPCANTimestamp CANTimeStamp;
            TPCANStatus    stsResult;
            ulong          timeInMicroseconds;

            //Read till buffer empty
            do
            {
                // The "Read" function of PCANBasic
                stsResult = PCANBasic.Read(PeakCANHandle, out CANMsg, out CANTimeStamp);
                if (stsResult != TPCANStatus.PCAN_ERROR_QRCVEMPTY)
                {
                    //Get the PEAK packet time in
                    timeInMicroseconds = Convert.ToUInt64(CANTimeStamp.micros + 1000 * CANTimeStamp.millis + 0x100000000 * 1000 * CANTimeStamp.millis_overflow);
                    //Store the read in packet and get a reference to it.
                    CurrentPacket = UpdatePackets(timeInMicroseconds, CANMsg.ID, CANMsg.LEN, CANMsg.DATA);
                    //Display it if possible, either over existing line to new line
                    if (CurrentPacket.DisplayIndex > -1)
                    {
                        AddMessage(ReceivedMessages, PacketToString(CurrentPacket), CurrentPacket.DisplayIndex);
                    }
                    else
                    {
                        CurrentPacket.DisplayIndex = AddMessage(ReceivedMessages, PacketToString(CurrentPacket));
                    }
                    if (WatchForPackets)
                    {
                        if (FoundPacket())
                        {
                            AddMessage(Feedback, "Found " + PacketToString(CurrentPacket) + " " + DateTime.Now.ToLocalTime());
                            if (DiffPacket != null)
                            {
                                ulong diff = CurrentPacket.Microseconds - DiffPacket.Microseconds;
                                AddMessage(Feedback, "Diff: " + (Convert.ToDouble(diff) / 1000000.0d).ToString("F6", CultureInfo.InvariantCulture) + " " + DateTime.Now.ToLocalTime());
                            }
                            else
                            {
                                AddMessage(Feedback, "First Packet");
                            }
                            Console.Beep();
                            DiffPacket = Packet.Clone(CurrentPacket);
                        }
                    }
                }
                if (stsResult == TPCANStatus.PCAN_ERROR_ILLOPERATION)
                {
                    break;
                }
            } while (PeakCANHandle > 0 && (!Convert.ToBoolean(stsResult & TPCANStatus.PCAN_ERROR_QRCVEMPTY)));
        }
Exemple #7
0
        /// <summary>
        /// Get identify setting from PCAN USB device
        /// </summary>
        /// <param name="PCANUSBHandle">PCAN USB device handle</param>
        /// <returns>true if identifying is on for the device, else false</returns>
        public static bool IsIdentifyOn(PCANHandle PCANUSBHandle)
        {
            TPCANStatus dllRet = TPCANStatus.PCAN_ERROR_UNKNOWN; //Assume unknown state to start
            UInt32      iBuffer;                                 //Passed to PCAN dll in GetValue call

            dllRet = PCANBasic.GetValue(PCANUSBHandle, TPCANParameter.PCAN_CHANNEL_IDENTIFYING, out iBuffer, sizeof(UInt32));
            if (dllRet == TPCANStatus.PCAN_ERROR_OK && ((iBuffer & PCANBasic.PCAN_PARAMETER_ON) == PCANBasic.PCAN_PARAMETER_ON))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #8
0
        /// <summary>
        /// Help Function used to get an error as text
        /// </summary>
        /// <param name="error">Error code to be translated</param>
        /// <returns>A text with the translated error</returns>
        public string PeakCANStatusErrorString(TPCANStatus error)
        {
            // Creates a buffer big enough for a error-text
            StringBuilder strTemp = new StringBuilder(256);

            // Gets the text using the GetErrorText API function
            // If the function success, the translated error is returned. If it fails,
            // a text describing the current error is returned.
            //
            if (PCANBasic.GetErrorText(error, 0, strTemp) != TPCANStatus.PCAN_ERROR_OK)
            {
                return(string.Format("An error occurred. Error-code's text ({0:X}) couldn't be retrieved", error));
            }
            else
            {
                return(strTemp.ToString());
            }
        }
Exemple #9
0
        /// <summary>
        /// Release the PEAK CAN USB device
        /// </summary>
        /// <returns></returns>
        public TPCANStatus Uninitialize()
        {
            TPCANStatus ret = TPCANStatus.PCAN_ERROR_UNKNOWN;

            //Stop reading
            RxMessages = false;
            if (PeakCANHandle != 0)
            {
                ret           = PCANBasic.Uninitialize(PeakCANHandle);
                PeakCANHandle = 0;
            }
            if (ReadThread != null)
            {
                ReadThread.Abort();
                ReadThread.Join();
                ReadThread = null;
            }
            AddMessage(Feedback, "Uninitialised.");
            LastOperationStatus = ret;
            return(ret);
        }
Exemple #10
0
        /// <summary>
        /// Configures the PCAN-Trace file for a PCAN-Basic Channel
        /// </summary>
        private void ConfigureTraceFile(string TraceDirectory, bool MultipleFiles, bool TimeStampName, UInt32 MaxMegabytes)
        {
            UInt32      iBuffer;
            TPCANStatus stsResult;

            // Configure the size of a trace file (max 100 MBs)
            stsResult = PCANBasic.SetValue(PeakCANHandle, TPCANParameter.PCAN_TRACE_SIZE, ref MaxMegabytes, sizeof(UInt32));
            if (stsResult != TPCANStatus.PCAN_ERROR_OK)
            {
                AddMessage(Feedback, GetFormatedError(stsResult));
            }

            //Configure trace location
            stsResult = PCANBasic.SetValue(PeakCANHandle, TPCANParameter.PCAN_TRACE_LOCATION, TraceDirectory, (uint)TraceDirectory.Length);
            if (stsResult != TPCANStatus.PCAN_ERROR_OK)
            {
                AddMessage(Feedback, GetFormatedError(stsResult));
            }

            //Single or multiple files
            if (MultipleFiles)
            {
                iBuffer = PCANBasic.TRACE_FILE_SEGMENTED | PCANBasic.TRACE_FILE_OVERWRITE;
            }
            else
            {
                iBuffer = PCANBasic.TRACE_FILE_SINGLE | PCANBasic.TRACE_FILE_OVERWRITE;
            }

            if (TimeStampName)
            {
                iBuffer = iBuffer | PCANBasic.TRACE_FILE_DATE | PCANBasic.TRACE_FILE_TIME;
            }

            stsResult = PCANBasic.SetValue(PeakCANHandle, TPCANParameter.PCAN_TRACE_CONFIGURE, ref iBuffer, sizeof(UInt32));
            if (stsResult != TPCANStatus.PCAN_ERROR_OK)
            {
                AddMessage(Feedback, GetFormatedError(stsResult));
            }
        }
Exemple #11
0
        public TPCANStatus WriteFrame(UInt32 Id, int DataLength, byte[] Data)
        {
            TPCANMsg CANMsg;

            // Create a TPCANMsg message structure
            CANMsg = new TPCANMsg();

            // Configure the Message Type.
            CANMsg.MSGTYPE = TPCANMessageType.PCAN_MESSAGE_STANDARD;
            // Message contents (ID, Length of the Data, Data)
            CANMsg.ID   = Id;
            CANMsg.LEN  = (byte)DataLength;
            CANMsg.DATA = Data;

            // The message is sent to the configured hardware
            LastOperationStatus = PCANBasic.Write(PeakCANHandle, ref CANMsg);
            if (LastOperationStatus != TPCANStatus.PCAN_ERROR_OK)
            {
                LastOperationErrorMessage();
            }
            return(LastOperationStatus);
        }
Exemple #12
0
        /// <summary>
        /// Set the identify setting for a PCAN USB device
        /// </summary>
        /// <param name="PCANUSBHandle">PCAN USB device handle</param>
        /// <param name="OnOff">identify on or off</param>
        /// <returns>true if setting was made correctly, else false if it failed</returns>
        public static bool SetIdentify(PCANHandle PCANUSBHandle, bool OnOff)
        {
            TPCANStatus dllRet = TPCANStatus.PCAN_ERROR_UNKNOWN; //Assume unknown state to start
            UInt32      iBuffer;                                 //Passed to PCAN dll in GetValue call

            if (OnOff)
            {
                iBuffer = PCANBasic.PCAN_PARAMETER_ON;
            }
            else
            {
                iBuffer = PCANBasic.PCAN_PARAMETER_OFF;
            }

            dllRet = PCANBasic.SetValue(PCANUSBHandle, TPCANParameter.PCAN_CHANNEL_IDENTIFYING, ref iBuffer, sizeof(UInt32));
            if (dllRet == TPCANStatus.PCAN_ERROR_OK)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }