Exemplo n.º 1
0
        public void WriteFrame()
        {
            uint handle = 0;
            uint canId  = 0xFF;

            /*** Config ***/
            uint[] AttrIdList    = { NiCan.NC_ATTR_BAUD_RATE, NiCan.NC_ATTR_START_ON_OPEN };
            uint[] AttrValueList = { Baudrate, NiCan.NC_FALSE };
            uint   NumAttrs      = 2;
            int    status        = NiCan.ncConfig(new StringBuilder(Interface), NumAttrs, AttrIdList, AttrValueList);

            Assert.AreEqual(0, status, NiCanTools.StatusToString(status));

            /*** Open Object ***/
            status = NiCan.ncOpenObject(new StringBuilder(Interface), ref handle);
            Assert.AreEqual(0, status, NiCanTools.StatusToString(status));

            /*** Start ***/
            status = NiCan.ncAction(handle, NiCan.NC_OP_START, 0);
            Assert.AreEqual(0, status, NiCanTools.StatusToString(status));

            /*** Frame ***/
            var frame = new NiCan.NCTYPE_CAN_FRAME();

            frame.ArbitrationId = canId | NiCan.NC_FL_CAN_ARBID_XTD;
            frame.IsRemote      = NiCan.NC_FALSE;
            frame.DataLength    = 8;
            frame.Data0         = 0;
            frame.Data1         = 1;
            frame.Data2         = 2;
            frame.Data3         = 3;
            frame.Data4         = 4;
            frame.Data5         = 5;
            frame.Data6         = 6;
            frame.Data7         = 7;

            /*** Write ***/
            status = NiCan.ncWrite(handle, NiCan.CanFrameSize, ref frame);
            Assert.AreEqual(0, status, NiCanTools.StatusToString(status));

            /*** Stop ***/
            status = NiCan.ncAction(handle, NiCan.NC_OP_STOP, 0);
            Assert.AreEqual(0, status, NiCanTools.StatusToString(status));

            /*** Close ***/
            status = NiCan.ncCloseObject(handle);
            Assert.AreEqual(0, status, NiCanTools.StatusToString(status));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Ütemesen nézzük mi a helyzet a CAN-el
        /// </summary>
        private void DoWork()
        {
            uint attrValue = 0;

            _isRunning = true;
            Exception loopExp = null;

            #region Thread Started Publishing
            Action doMehtod = () => OnStarted();
            if (App.SyncContext != null)
            {
                App.SyncContext.Post((e1) => { doMehtod(); }, null);
            }
            else
            {
                doMehtod();
            }
            #endregion

            try
            {
                _handle = NiCanOpen(CanInterface);
            }
            catch (Exception ex)
            {
                loopExp = ex;
            }
            GetWaitForParseFrames = 0;
            GetDroppedFrames      = 0;
            GetParsedFrames       = 0;
            GetRxFrames           = 0;
            GetTxFrames           = 0;
            GetWaitForTxFrames    = 0;
            do
            {
                if (loopExp != null)
                {
                    break;
                }

                var rx = new NiCan.NCTYPE_CAN_STRUCT();

                /*** Get NC_ATTR_READ_PENDING ***/
                loopExp = NiCanStatusCheck(NiCan.ncGetAttribute(_handle, NiCan.NC_ATTR_READ_PENDING, 4, ref attrValue));
                if (loopExp != null)
                {
                    break;
                }

                GetWaitForParseFrames = (int)attrValue;

                if (GetWaitForParseFrames != 0)
                {
                    /*** Read ***/
                    loopExp = NiCanStatusCheck(NiCan.ncRead(_handle, NiCan.CanStructSize, ref rx));
                    if (loopExp != null)
                    {
                        break;
                    }
                    GetRxFrames++;

                    if ((rx.ArbitrationId & 0x20000000) == 0x20000000)
                    { /*Message is Extended */
                        /*Mask*/
                        UInt32 arbId = rx.ArbitrationId & 0x1FFFFFFF;

                        byte[] datatemp = new byte[]
                        { rx.Data0,
                          rx.Data1,
                          rx.Data2,
                          rx.Data3,
                          rx.Data4,
                          rx.Data5,
                          rx.Data6,
                          rx.Data7, };
                        byte[] data = new byte[rx.DataLength];

                        Buffer.BlockCopy(datatemp, 0, data, 0, rx.DataLength);

                        IoLog.Instance.WirteLine("RX " + arbId.ToString("X8") + " " + Tools.ByteArrayLogString(data));

                        doMehtod = () =>
                        {
                            if (_explorer.UpdateTask(new CanMsg(arbId, data)))
                            {
                                GetParsedFrames++;
                            }
                            else
                            {
                                GetDroppedFrames++;
                            }
                        };
                        if (App.SyncContext != null)
                        {
                            App.SyncContext.Post((e1) => { doMehtod(); }, null);
                        }
                        else
                        {
                            doMehtod();
                        }


                        /*** Write ***/
                        GetWaitForTxFrames = TxQueue.Count;
                        if ((GetWaitForTxFrames = TxQueue.Count) != 0)
                        {
                            var tx   = TxQueue.Dequeue();
                            var niTx = new NiCan.NCTYPE_CAN_FRAME();
                            tx.ArbId          |= 0x20000000;
                            niTx.ArbitrationId = tx.ArbId;
                            niTx.DataLength    = 8;
                            niTx.IsRemote      = NiCan.NC_FALSE;
                            niTx.Data0         = tx.Data[0];
                            niTx.Data1         = tx.Data[1];
                            niTx.Data2         = tx.Data[2];
                            niTx.Data3         = tx.Data[3];
                            niTx.Data4         = tx.Data[4];
                            niTx.Data5         = tx.Data[5];
                            niTx.Data6         = tx.Data[6];
                            niTx.Data7         = tx.Data[7];

                            IoLog.Instance.WirteLine("TX " + tx.ArbId.ToString("X8") + " " + Tools.ByteArrayLogString(tx.Data));
                            loopExp = NiCanStatusCheck(NiCan.ncWrite(_handle, NiCan.CanFrameSize, ref niTx));
                            if (loopExp != null)
                            {
                                break;
                            }
                            GetTxFrames++;
                        }
                    }
                    else
                    {
                        GetDroppedFrames++;
                    }
                }


                if (_shutdownEvent.WaitOne(0))
                {
                    Debug.WriteLine(GetType().Namespace + "." +
                                    GetType().Name + "." +
                                    MethodBase.GetCurrentMethod().Name +
                                    "(): DoWork is now shutdown!");
                    break;
                }
            } while (true);

            /*Probléma megjelnítése*/
            if (loopExp != null)
            {
                AppLog.Instance.WriteLine("DoWork()");
            }
            #region Resource Freeing
            if (_handle != 0)
            {
                NiCanClose(_handle);
                _handle = 0;
            }
            _readyToDisposeEvent.Set();
            _isRunning = false;
            #endregion

            #region Thread Terminate Publishing
            doMehtod = () =>
            {
                try
                {
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    /*Minden áron jelezni kell! hogy megállt.*/
                    OnStopped();
                    Debug.WriteLine(GetType().Namespace + "." + GetType().Name + "." + MethodBase.GetCurrentMethod().Name + "(): Loop is colsed...");
                }
            };
            if (App.SyncContext != null)
            {
                App.SyncContext.Post((e1) => { doMehtod(); }, null);
            }
            else
            {
                doMehtod();
            }
            #endregion
        }