} // End DetectCanInterfaces

        // ***************************************
        // GenericCanBusOn
        // Turns canInterface on based on User Selection
        // ***************************************
        public static int CanBusOn(string canInterface, string bitRateSetting)
        {
            string hardwareString0 = canInterface.Replace(" ", "");

            string[] msgOutput = hardwareString0.Split(';');

            Byte   ReturnError = 0; //This will be used for all error return status codes
            UInt32 EcomHandle;      //This will be the HANDLE to our ECOM device used for all function calls
            byte   EComCANBitrate = ECOMLibrary.CAN_BAUD_500K;

            ECOMLibrary.DeviceInfo di = new ECOMLibrary.DeviceInfo();

            // Assigns settings for the adapter
            if (bitRateSetting == "1M")
            {
                EComCANBitrate = ECOMLibrary.CAN_BAUD_1MB;
            }
            else if (bitRateSetting == "500K")
            {
                EComCANBitrate = ECOMLibrary.CAN_BAUD_500K;
            }
            else if (bitRateSetting == "250K")
            {
                EComCANBitrate = ECOMLibrary.CAN_BAUD_250K;
            }
            else if (bitRateSetting == "125K")
            {
                EComCANBitrate = ECOMLibrary.CAN_BAUD_125K;
            }
            else if (bitRateSetting == "100K" || msgOutput[4] == "62K" || msgOutput[4] == "50K" || msgOutput[4] == "33K")
            {
                MainWindow.ErrorDisplayString("ECom Adapter does not support low speed CAN (100K, 62K, 50K, 33K)");
                return(-1);
            }

            EcomHandle = ECOMLibrary.CANOpen(Convert.ToUInt32(msgOutput[3]), EComCANBitrate, ref ReturnError);
            //MainWindow.ErrorDisplayString("ECom Handle: " + Convert.ToString(EcomHandle));
            BusInterface.AddHandle(canInterface, (int)EcomHandle);

            if (EcomHandle == 0 || ReturnError != 0)
            {
                StringBuilder ErrMsg = new StringBuilder(400);
                ECOMLibrary.GetFriendlyErrorMessage(ReturnError, ErrMsg, 400);
                MainWindow.ErrorDisplayString("CANOpen failed with error message: " + ErrMsg);
                return(-1);
            }

            ECOMLibrary.GetDeviceInfo(EcomHandle, ref di);
            ErrorLog.NewLogEntry("CAN", "Bus On: " + di.SerialNumber);
            return(1);
        }
Esempio n. 2
0
        } // End DetectCanInterfaces

        // ***************************************
        // GenericCanBusOn
        // Turns canInterface on based on User Selection
        // ***************************************
        public static int CanBusOn(string canInterface, string bitRateSetting)
        {
            string hardwareString0 = canInterface.Replace(" ", "");

            string[] msgOutput = hardwareString0.Split(';');

            int canHandle;
            int channelFlags;
            int KvaserCANBitrate = Canlib.canBITRATE_500K;

            Canlib.canStatus status;

            // Assigns settings for the adapter
            if (bitRateSetting == "1M")
            {
                KvaserCANBitrate = Canlib.canBITRATE_1M;
            }
            else if (bitRateSetting == "500K")
            {
                KvaserCANBitrate = Canlib.canBITRATE_500K;
            }
            else if (bitRateSetting == "250K")
            {
                KvaserCANBitrate = Canlib.canBITRATE_250K;
            }
            else if (bitRateSetting == "125K")
            {
                KvaserCANBitrate = Canlib.canBITRATE_125K;
            }
            else if (bitRateSetting == "100K")
            {
                KvaserCANBitrate = Canlib.canBITRATE_100K;
            }
            else if (bitRateSetting == "62K")
            {
                KvaserCANBitrate = Canlib.canBITRATE_62K;
            }
            else if (bitRateSetting == "50K")
            {
                KvaserCANBitrate = Canlib.canBITRATE_50K;
            }
            else if (bitRateSetting == "33K")
            {
                KvaserCANBitrate = 33000;
            }

            // Checks for Virtual Flag
            if (msgOutput[1].IndexOf("Virtual") != -1)
            {
                channelFlags = Canlib.canOPEN_ACCEPT_VIRTUAL;
            }
            else
            {
                channelFlags = 0;
            }

            // Opens CAN channel
            canHandle = Canlib.canOpenChannel(Convert.ToInt32(msgOutput[3]), channelFlags);

            // Sets parameters for the CAN channel; special parameters for 33K, single-wire operation
            if (bitRateSetting == "33K")
            {
                status = Canlib.canSetBusParams(canHandle, 33000, 5, 2, 2, 1, 0);
            }
            else
            {
                // Standard settings for other operations
                status = Canlib.canSetBusParams(canHandle, KvaserCANBitrate, 0, 0, 0, 0, 0);
            }

            if (status < 0)
            {
                ErrorLog.NewLogEntry("CAN", "Kvaser bus setting parameters failed: " + KvaserCANBitrate);
                return(-1);
            }
            else
            {
                ErrorLog.NewLogEntry("CAN", "Kvaser bus setting parameters success: " + KvaserCANBitrate);
            }

            // possible configuration for 33K single-wire operation; above settings work
            //Canlib.canSetBusParamsC200(canHandle, 0x5D, 0x05);

            status = Canlib.canBusOn(canHandle);
            if (status < 0)
            {
                ErrorLog.NewLogEntry("CAN", "Bus On Failed: " + msgOutput[1]);
                return(-1);
            }
            else
            {
                ErrorLog.NewLogEntry("CAN", "Bus On Success: " + msgOutput[1]);
            }

            // Associates the int holder for the Kvaser interface back with the interface dictionary structure
            BusInterface.AddHandle(canInterface, canHandle);

            //MainWindow.ErrorDisplayString("Bus On - BusInterface: " + canInterface + " ; Handle: " + canHandle);

            return(1);
        }