Exemplo n.º 1
0
        /// <summary>
        ///  Назначить скорость передачи данных CAN-адаптера
        /// </summary>
        public void SetSpeed(int Speed)
        {
            switch (Speed)
            {
            case 250: vCanSpeed = CanBitrate.Cia250KBit; break;

            case 1000: vCanSpeed = CanBitrate.Cia1000KBit; break;

            case 500: vCanSpeed = CanBitrate.Cia500KBit; break;

            case 800: vCanSpeed = CanBitrate.Cia800KBit; break;

            case 125: vCanSpeed = CanBitrate.Cia125KBit; break;

            case 10: vCanSpeed = CanBitrate.Cia10KBit; break;

            case 20: vCanSpeed = CanBitrate.Cia20KBit; break;

            case 80: vCanSpeed = CanBitrate.Cia800KBit; break;
            }
        }
Exemplo n.º 2
0
        static public bool InitSocket(String adapter, uint baudrate)
        {
            FinalizeApp();
            IBalObject bal       = null;
            bool       succeeded = false;
            IVciDevice device    = null;


            try
            {
                device = GetDeviceByString(adapter);
                //
                // Open bus access layer
                //
                bal = device.OpenBusAccessLayer();

                //
                // Open a message channel for the CAN controller
                //
                mCanChn = bal.OpenSocket(0, typeof(ICanChannel)) as ICanChannel;

                /*//
                 * // Open the scheduler of the CAN controller
                 * //
                 * Log("4"); // не проходит переинициализацию, что-то надо сделать
                 * mCanSched = bal.OpenSocket(0, typeof(ICanScheduler)) as ICanScheduler;*/

                // Initialize the message channel
                mCanChn.Initialize(1024, 128, false);

                // Get a message reader object
                mReader = mCanChn.GetMessageReader();

                // Initialize message reader
                mReader.Threshold = 1;

                // Create and assign the event that's set if at least one message
                // was received.
                mRxEvent = new System.Threading.AutoResetEvent(false);
                mReader.AssignEvent(mRxEvent);

                // Get a message wrtier object
                mWriter = mCanChn.GetMessageWriter();

                // Initialize message writer
                mWriter.Threshold = 1;

                mTxEvent = new System.Threading.AutoResetEvent(false);
                mWriter.AssignEvent(mTxEvent);

                // Activate the message channel
                mCanChn.Activate();


                //
                // Open the CAN controller
                //
                mCanCtl = bal.OpenSocket(0, typeof(ICanControl)) as ICanControl;
                CanBitrate _cb = new CanBitrate();
                switch (baudrate)
                {
                case BAUDRATE_125:
                    _cb = CanBitrate.Cia125KBit; break;

                case BAUDRATE_250:
                    _cb = CanBitrate.Cia250KBit; break;

                default:
                    _cb = CanBitrate.Cia125KBit; break;
                }
                // Initialize the CAN controller
                mCanCtl.InitLine(CanOperatingModes.Standard |
                                 CanOperatingModes.Extended |       //extended отключить наверняка стоит
                                 CanOperatingModes.ErrFrame,
                                 _cb);

                //
                // print line status
                //
                Debug.WriteLine(" LineStatus: " + mCanCtl.LineStatus + "\n|");

                // Set the acceptance filter for std identifiers
                mCanCtl.SetAccFilter(CanFilter.Std,
                                     (uint)CanAccCode.All, (uint)CanAccMask.All);

                // Set the acceptance filter for ext identifiers
                mCanCtl.SetAccFilter(CanFilter.Ext,
                                     (uint)CanAccCode.All, (uint)CanAccMask.All);

                // Start the CAN controller
                mCanCtl.StartLine();

                succeeded = true;
            }
            catch (Exception exc)
            {
                Debug.WriteLine("Error: Initializing socket failed : " + exc.Message + "\n|");
                succeeded = false;
            }
            finally
            {
                DisposeVciObject(device);
                DisposeVciObject(bal);
            }

            return(succeeded);
        }