// **********************
        // Turns canInterface off
        // **********************
        public static void CanBusOff(string canInterface)
        {
            string hardwareString0 = canInterface.Replace(" ", "");

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

            // Need Error Checking to ensure that device closes
            ECOMLibrary.CloseDevice((UInt32)BusInterface.ReturnHandle(canInterface));
            ErrorLog.NewLogEntry("CAN", "Bus Off Success/Failed: " + msgOutput[1]);
        }
Esempio n. 2
0
        } // End GenericCanTransmitSingle

        //************************************
        // CAN Transmit for multiple transmissions
        //************************************
        public static void CanTransmitMultiple(CanData can)
        {
            can.handle = BusInterface.ReturnHandle(can.hardware);
            KvaserWriteCan writeCan = new KvaserWriteCan();

            Thread t2 = new Thread(delegate()
            {
                writeCan.KvaserWriteMultiple(can);
            });

            t2.IsBackground = true;
            t2.Start();
        }  // End GenericCanTransmitMultiple
        // **********************
        // Turns canInterface off
        // **********************
        public static void CanBusOff(string canInterface)
        {
            string hardwareString0 = canInterface.Replace(" ", "");

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

            int iResult;
            int iNumberOfErrors = 0;

            iResult = neoCSNet2003.icsNeoDll.icsneoClosePort(BusInterface.ReturnHandle(canInterface), ref iNumberOfErrors);

            if (iResult == 1)
            {
                ErrorLog.NewLogEntry("CAN", "Bus Off Success: " + msgOutput[1]);
            }
            else
            {
                ErrorLog.NewLogEntry("CAN", "Bus Off Failed: " + msgOutput[1]);
            }
        }
Esempio n. 4
0
        }  // End GenericCanTransmitMultiple

        // CAN Receive
        public static bool CanReceive(CanData can)
        {
            // string hardwareString0 = can.hardware.Replace(" ", "");
            // string[] msgOutput = hardwareString0.Split(';');

            // to pull the can.handle from the hardware selection
            can.handle = BusInterface.ReturnHandle(can.hardware);
            //MainWindow.ErrorDisplayString("can.handle: " + can.handle + " ; msg: " + msgOutput[3]);

            // Kvaser CAN Read
            Array.Clear(can.msg, 0, 8);
            can.status = Canlib.canRead(can.handle, out can.id, can.msg, out can.dlc, out can.flags, out can.time);
            // MainWindow.ErrorDisplayString("can.handle in receive: " + can.handle);
            CheckFlags(can);

            if (can.status != Canlib.canStatus.canOK)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        } // End Read
Esempio n. 5
0
        //************************************
        // CAN Transmit for a single transmission
        //************************************
        public static void CanTransmitSingle(CanData can)
        {
            string hardwareString0 = can.hardware.Replace(" ", "");

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

            can.handle = BusInterface.ReturnHandle(can.hardware);

            // Threading Part

            /*CanWrite writeCan = new CanWrite();
             *
             * // Starts the Kvaser threading
             * Thread t2 = new Thread(delegate()
             * {
             *  writeCan.KvaserWrite(can);
             * });
             * t2.Start();
             */

            // Non-threading write option
            Canlib.canWriteWait(can.handle, can.id, can.msg, can.dlc, can.flags, 1000);
            CheckFlags(can);
        } // End GenericCanTransmitSingle
        public void ECOMWriteMultiple(CanData can)
        {
            for (int x = 0; x < can.number; x++)
            {
                can.handle = BusInterface.ReturnHandle(can.hardware);
                //MainWindow.ErrorDisplayString("ECom Handle: " + Convert.ToString(can.handle));

                ECOMLibrary.SFFMessage txMessage  = new ECOMLibrary.SFFMessage();
                ECOMLibrary.EFFMessage etxMessage = new ECOMLibrary.EFFMessage();

                // Pulls the top 3 bits for IDH and the bottom 8 bits for IDL
                txMessage.IDH = (byte)(can.id & 0x700);
                txMessage.IDL = (byte)(can.id & 0x0FF);
                // Converts the entire id into an unit for extended sending
                etxMessage.ID = (UInt32)can.id;

                txMessage.DataLength  = (byte)can.dlc;
                etxMessage.DataLength = (byte)can.dlc;
                txMessage.options     = (byte)can.flags;
                etxMessage.options    = (byte)can.flags;

                // Puts the individual bytes into the can.msg byte array
                if (can.dlc > 0)
                {
                    txMessage.data1  = can.msg[0];
                    etxMessage.data1 = can.msg[0];
                }

                if (can.dlc > 1)
                {
                    txMessage.data2  = can.msg[1];
                    etxMessage.data2 = can.msg[1];
                }

                if (can.dlc > 2)
                {
                    txMessage.data3  = can.msg[2];
                    etxMessage.data3 = can.msg[2];
                }

                if (can.dlc > 3)
                {
                    txMessage.data4  = can.msg[3];
                    etxMessage.data4 = can.msg[3];
                }

                if (can.dlc > 4)
                {
                    txMessage.data5  = can.msg[4];
                    etxMessage.data5 = can.msg[4];
                }

                if (can.dlc > 5)
                {
                    txMessage.data6  = can.msg[5];
                    etxMessage.data6 = can.msg[5];
                }

                if (can.dlc > 6)
                {
                    txMessage.data7  = can.msg[6];
                    etxMessage.data7 = can.msg[6];
                }

                if (can.dlc > 7)
                {
                    txMessage.data8  = can.msg[7];
                    etxMessage.data8 = can.msg[7];
                }

                if (can.flags == 4)
                {
                    ECOMLibrary.CANTransmitMessageEx((UInt32)can.handle, ref etxMessage);
                }
                else
                {
                    ECOMLibrary.CANTransmitMessage((UInt32)can.handle, ref txMessage);
                }


                System.Threading.Thread.Sleep(can.timeBtw);

                if (can.increment == true)
                {
                    can.id++;
                }
            }
        }
        }  // End GenericCanTransmitMultiple

        // CAN Receive
        public static bool CanReceive(CanData can)
        {
            Byte ReturnError = 0;

            ECOMLibrary.SFFMessage RxMessage = new ECOMLibrary.SFFMessage();
            ECOMLibrary.EFFMessage ExMessage = new ECOMLibrary.EFFMessage();

            try
            {
                //ECOM library call to get a standard message
                ReturnError = ECOMLibrary.CANReceiveMessage((UInt32)BusInterface.ReturnHandle(can.hardware), ref RxMessage);
            }
            catch {
                ErrorLog.NewLogEntry("ECOM", Convert.ToString(ReturnError));
            }

            if (ReturnError == 0)
            {
                // Converts the ID low and ID high
                can.id   = Convert.ToInt32(RxMessage.IDL + RxMessage.IDH);
                can.dlc  = Convert.ToInt32(RxMessage.DataLength);
                can.time = Convert.ToInt64(RxMessage.TimeStamp);

                // Puts the individual bytes into the can.msg byte array
                if (can.dlc > 0)
                {
                    can.msg[0] = RxMessage.data1;
                }
                if (can.dlc > 1)
                {
                    can.msg[1] = RxMessage.data2;
                }
                if (can.dlc > 2)
                {
                    can.msg[2] = RxMessage.data3;
                }
                if (can.dlc > 3)
                {
                    can.msg[3] = RxMessage.data4;
                }
                if (can.dlc > 4)
                {
                    can.msg[4] = RxMessage.data5;
                }
                if (can.dlc > 5)
                {
                    can.msg[5] = RxMessage.data6;
                }
                if (can.dlc > 6)
                {
                    can.msg[6] = RxMessage.data7;
                }
                if (can.dlc > 7)
                {
                    can.msg[7] = RxMessage.data8;
                }

                // Revisions Needed -- Need to pull flag information
                can.flags = Convert.ToInt32(RxMessage.options);

                //MainWindow.ErrorDisplayString(Convert.ToString(can.flags));

                // For error testing
                // MainWindow.ErrorDisplayByteArray(can.msg);
                //MainWindow.ErrorDisplayString(Convert.ToString(can.dlc));
            }
            else
            {
                ReturnError = ECOMLibrary.CANReceiveMessageEx((UInt32)BusInterface.ReturnHandle(can.hardware), ref ExMessage);

                can.id   = Convert.ToInt32(ExMessage.ID);
                can.dlc  = Convert.ToInt32(ExMessage.DataLength);
                can.time = Convert.ToInt64(ExMessage.TimeStamp);

                // Puts the individual bytes into the can.msg byte array
                if (can.dlc > 0)
                {
                    can.msg[0] = ExMessage.data1;
                }
                if (can.dlc > 1)
                {
                    can.msg[1] = ExMessage.data2;
                }
                if (can.dlc > 2)
                {
                    can.msg[2] = ExMessage.data3;
                }
                if (can.dlc > 3)
                {
                    can.msg[3] = ExMessage.data4;
                }
                if (can.dlc > 4)
                {
                    can.msg[4] = ExMessage.data5;
                }
                if (can.dlc > 5)
                {
                    can.msg[5] = ExMessage.data6;
                }
                if (can.dlc > 6)
                {
                    can.msg[6] = ExMessage.data7;
                }
                if (can.dlc > 7)
                {
                    can.msg[7] = ExMessage.data8;
                }

                // Revisions Needed -- Need to pull flag information
                can.flags = Convert.ToInt32(ExMessage.options);

                // For error testing
                //  MainWindow.ErrorDisplayByteArray(can.msg);
            }

            if (ReturnError != 0)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        } // End Read
        //************************************
        // CAN Transmit for a single transmission
        //************************************
        public static void CanTransmitSingle(CanData can)
        {
            // Implementation for Standard Message & Extended Message

            can.handle = BusInterface.ReturnHandle(can.hardware);
            //MainWindow.ErrorDisplayString("ECom Handle: " + Convert.ToString(can.handle));

            ECOMLibrary.SFFMessage txMessage  = new ECOMLibrary.SFFMessage();
            ECOMLibrary.EFFMessage etxMessage = new ECOMLibrary.EFFMessage();

            // Pulls the top 3 bits for IDH and the bottom 8 bits for IDL
            txMessage.IDH = (byte)(can.id & 0x700);
            txMessage.IDL = (byte)(can.id & 0x0FF);
            // Converts the entire id into an unit for extended sending
            etxMessage.ID = (UInt32)can.id;

            txMessage.DataLength  = (byte)can.dlc;
            etxMessage.DataLength = (byte)can.dlc;
            txMessage.options     = (byte)can.flags;
            etxMessage.options    = (byte)can.flags;

            // Puts the individual bytes into the can.msg byte array
            if (can.dlc > 0)
            {
                txMessage.data1  = can.msg[0];
                etxMessage.data1 = can.msg[0];
            }

            if (can.dlc > 1)
            {
                txMessage.data2  = can.msg[1];
                etxMessage.data2 = can.msg[1];
            }

            if (can.dlc > 2)
            {
                txMessage.data3  = can.msg[2];
                etxMessage.data3 = can.msg[2];
            }

            if (can.dlc > 3)
            {
                txMessage.data4  = can.msg[3];
                etxMessage.data4 = can.msg[3];
            }

            if (can.dlc > 4)
            {
                txMessage.data5  = can.msg[4];
                etxMessage.data5 = can.msg[4];
            }

            if (can.dlc > 5)
            {
                txMessage.data6  = can.msg[5];
                etxMessage.data6 = can.msg[5];
            }

            if (can.dlc > 6)
            {
                txMessage.data7  = can.msg[6];
                etxMessage.data7 = can.msg[6];
            }
            if (can.dlc > 7)
            {
                txMessage.data8  = can.msg[7];
                etxMessage.data8 = can.msg[7];
            }

            // Non-threading write option
            if (can.flags == 4)
            {
                ECOMLibrary.CANTransmitMessageEx((UInt32)can.handle, ref etxMessage);
            }
            else
            {
                ECOMLibrary.CANTransmitMessage((UInt32)can.handle, ref txMessage);
            }

            CheckFlags(can);
        } // End GenericCanTransmitSingle