Пример #1
0
        internal int FWwrite(byte[] buffer2, int id)
        {
            //int id = 888;
            Canlib.canStatus status;
            status = Canlib.canWrite(hcan, id, buffer2, 8, Canlib.canMSG_STD);
            if (status != Canlib.canStatus.canOK)
            {
                send2Terminal("mingi error sõnumi saatmisel");
            }

            //Get ACK response
            byte[] data = new byte[8];
            int    dlc;
            int    flags;
            long   time;

            while (id != 889)
            {
                Canlib.canReadWait(hcan, out id, data, out dlc, out flags, out time, 100);
            }

            if (data[0] == ACK)
            {
                return(ACK);
            }
            else
            {
                return(NACK);
            }
        }
Пример #2
0
        /// <summary>
        /// readMessages is the "run" method of this class. It reads all incomming messages
        /// and publishes them to registered ICANListeners.
        /// </summary>
        public void readMessages()
        {
            byte[] msg = new byte[8];
            int    dlc;
            int    flag, id;
            long   time;

            Canlib.canStatus status;
            CANMessage       canMessage = new CANMessage();

            logger.Debug("readMessages started");
            while (true)
            {
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        logger.Debug("readMessages thread ended");
                        return;
                    }
                }
                status = Canlib.canReadWait(handleRead, out id, msg, out dlc, out flag, out time, 250);
                if ((flag & Canlib.canMSG_ERROR_FRAME) == 0)
                {
                    if (status == Canlib.canStatus.canOK)
                    {
                        canMessage.setID((uint)id);
                        canMessage.setTimeStamp((uint)time);
                        canMessage.setFlags((byte)flag);
                        canMessage.setCanData(msg, (byte)dlc);

                        lock (m_listeners)
                        {
                            //AddToCanTrace("RX: " + canMessage.getID().ToString("X3") + " " + canMessage.getLength().ToString("X1") + " " + canMessage.getData().ToString("X16"));
                            //Console.WriteLine("MSG: " + rxMessage);
                            foreach (ICANListener listener in m_listeners)
                            {
                                listener.handleMessage(canMessage);
                            }
                            //CastInformationEvent(canMessage); // <GS-05042011> re-activated this function
                        }
                    }
                    else if (status == Canlib.canStatus.canERR_NOMSG)
                    {
                        Thread.Sleep(1);
                    }
                }
                else
                {
                    logger.Debug("error frame");
                }
            }
        }
Пример #3
0
        /// <summary>
        /// readMessages is the "run" method of this class. It reads all incomming messages
        /// and publishes them to registered ICANListeners.
        /// </summary>
        public void readMessages()
        {
            byte[] msg = new byte[8];
            int    dlc;
            int    flag, id;
            long   time;

            Canlib.canStatus status;
            CANMessage       canMessage = new CANMessage();

            logger.Debug("readMessages started");
            while (true)
            {
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        logger.Debug("readMessages thread ended");
                        return;
                    }
                }
                status = Canlib.canReadWait(handleRead, out id, msg, out dlc, out flag, out time, 250);
                if ((flag & Canlib.canMSG_ERROR_FRAME) == 0)
                {
                    if (status == Canlib.canStatus.canOK)
                    {
                        if (acceptMessageId((uint)id))
                        {
                            canMessage.setID((uint)id);
                            canMessage.setTimeStamp((uint)time);
                            canMessage.setFlags((byte)flag);
                            canMessage.setCanData(msg, (byte)dlc);

                            receivedMessage(canMessage);
                        }
                    }
                    else if (status == Canlib.canStatus.canERR_NOMSG)
                    {
                        Thread.Sleep(1);
                    }
                    else
                    {
                        logger.Debug("error" + status);
                    }
                }
                else
                {
                    logger.Debug("error frame");
                }
            }
        }
Пример #4
0
        /*
         * Looks for messages and sends them to the output box.
         */

        private void DumpMessageLoop(out string[] msg)
        {
            Canlib.canStatus status;
            int id;

            byte[] data = new byte[8];
            int    dlc;
            int    flags;
            long   time;
            bool   noError = true;
            int    i = 0, j = 0;

            msg = new string[1000];

            //Open up a new handle for reading
            //  readHandle = Canlib.canOpenChannel(channel, Canlib.canOPEN_ACCEPT_VIRTUAL);

            //    status = Canlib.canBusOn(readHandle);
            //
            Thread.Sleep(200);

            while (j < 200)
            {
                status = Canlib.canReadWait(handle, out id, data, out dlc, out flags, out time, 5);

                if (status == Canlib.canStatus.canOK && id == DigRecivedID)
                {
                    if ((flags & Canlib.canMSG_ERROR_FRAME) == Canlib.canMSG_ERROR_FRAME)
                    {
                        // msg[i] = "***ERROR FRAME RECEIVED***";
                    }
                    else
                    {
                        msg[i] = String.Format("{0}  {1}  {2:x2} {3:x2} {4:x2} {5:x2} {6:x2} {7:x2} {8:x2} {9:x2}   {10}\r",
                                               id, dlc, data[0], data[1], data[2], data[3], data[4],
                                               data[5], data[6], data[7], time);
                    }
                    //Sends the message to the ProcessMessage method
                    i++;
                }
                else if (status != Canlib.canStatus.canERR_NOMSG)
                {
                    //Sends the error status to the ProcessMessage method and breaks the loop

                    noError = false;
                }
                j++;
            }
            //  Canlib.canBusOff(readHandle);
        }
Пример #5
0
        /*
         * Looks for messages and sends them to the output box.
         */
        private void DumpMessageLoop(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            Canlib.canStatus status;
            int id;

            byte[] data = new byte[8];
            int    dlc;
            int    flags;
            long   time;
            bool   noError = true;
            string msg;

            //Open up a new handle for reading
            readHandle = Canlib.canOpenChannel(channel, Canlib.canOPEN_ACCEPT_VIRTUAL);

            status = Canlib.canBusOn(readHandle);

            while (noError && onBus && readHandle >= 0)
            {
                status = Canlib.canReadWait(readHandle, out id, data, out dlc, out flags, out time, 50);

                if (status == Canlib.canStatus.canOK)
                {
                    if ((flags & Canlib.canMSG_ERROR_FRAME) == Canlib.canMSG_ERROR_FRAME)
                    {
                        msg = "***ERROR FRAME RECEIVED***";
                    }
                    else
                    {
                        msg = String.Format("{0}  {1}  {2:x2} {3:x2} {4:x2} {5:x2} {6:x2} {7:x2} {8:x2} {9:x2}   {10}\r",
                                            id, dlc, data[0], data[1], data[2], data[3], data[4],
                                            data[5], data[6], data[7], time);
                    }

                    //Sends the message to the ProcessMessage method
                    worker.ReportProgress(0, msg);
                }

                else if (status != Canlib.canStatus.canERR_NOMSG)
                {
                    //Sends the error status to the ProcessMessage method and breaks the loop
                    worker.ReportProgress(100, status);
                    noError = false;
                }
            }
            Canlib.canBusOff(readHandle);
        }
Пример #6
0
        public Message ReadMessage(int readHandle, out Canlib.canStatus status)
        {
            status = Canlib.canStatus.canOK;
            Message m = null;
            int     id;

            byte[] data = new byte[8];
            int    dlc;
            int    flags;
            long   time;

            status = Canlib.canReadWait(readHandle, out id, data, out dlc, out flags, out time, 50);
            m      = new Message(id, data, dlc, flags, time);

            return(m);
        }
Пример #7
0
        public void DumpMessageLoop()
        {
            Canlib.canStatus status;
            bool             finished = false;

            //These variables hold the incoming message
            byte[] data = new byte[8];
            int    id;
            int    dlc;
            int    flags;
            long   time;

            send2Terminal("Channel opened. Press Disconnect to close. ");
            send2Terminal("ID  DLC DATA                      Timestamp");

            while (!finished)
            {
                //Wait for up to 100 ms for a message on the channel
                status = Canlib.canReadWait(hcan, out id, data, out dlc, out flags, out time, 100);


                //If a message was received, display i
                if (status == Canlib.canStatus.canOK)
                {
                    DumpMessage(id, data, dlc, flags, time);
                }

                //Call DisplayError and exit in case an actual error occurs
                else if (status != Canlib.canStatus.canERR_NOMSG)
                {
                    CheckStatus(status, "canRead/canReadWait");
                    lock (MainWindow._locker) wnd.runCan = false;
                    deinitCan();
                }

                //Breaks the loop if the user presses the Disconnect key
                lock (MainWindow._locker) finished = !wnd.runCan;
            }
            //deinitCan();
        }
Пример #8
0
        public bool CanLongMsgSend(int id, byte[] data, int datalength, int retrycount, double timeout, ref int EcuID, ref byte[] READDATA, ref string errmsg)
        {
            if ((datalength < 9) || (data.Length < 9))
            {
                errmsg = "Command Data Length <=8";
                return(false);
            }

            bool _LoopChk = true;
            int  _EcuID;

            byte[] _EcuData = new byte[8];
            int    _EcuDlc;
            int    _EcuFlags;
            long   _EcuTime;

            int    _Ecu_FS    = -1; // 정상적으로 수신시 확인되는 값, FS FlowStatus, BS BlockSize, STmin
            int    _Ecu_BS    = 0;
            int    _Ecu_STmin = 0;
            double _Ecu_Stmin_sec; // 소수점단위 계산
            bool   _Ecu_FlowControl_Receive_OK = false;

            byte[] _Data_FF           = new byte[8];
            byte[] _Data_CF           = new byte[8];
            int    _LastSendDataIndex = 0;

            Canlib.canStatus status;
            Stopwatch        _stopwatch_total = new Stopwatch();
            Stopwatch        _stopwatch_send  = new Stopwatch();

            // 명령 만들기
            _Data_FF[0] = 0x10;

            Int32 _DataLength_12bit = 0x00000000;

            _DataLength_12bit = (Int32)datalength;
            _DataLength_12bit = _DataLength_12bit & 0x00000FFF;

            byte byte1Lownibble;
            byte byte2;

            byte1Lownibble = (byte)((_DataLength_12bit & 0x00000F00) >> 16);
            byte2          = (byte)(_DataLength_12bit & 0x000000FF);

            _Data_FF[0] = (byte)(0x10 | byte1Lownibble);
            _Data_FF[1] = byte2;
            _Data_FF[2] = data[0];
            _Data_FF[3] = data[1];
            _Data_FF[4] = data[2];
            _Data_FF[5] = data[3];
            _Data_FF[6] = data[4];
            _Data_FF[7] = data[5];

            _LastSendDataIndex = 6;
            //
            //Canlib.canBusOn(handle);
            //status = Canlib.canWrite(handle, id, _Data_FF, 8, flags);
            //Open up a new handle for reading
            readHandle = Canlib.canOpenChannel(channel, Canlib.canOPEN_ACCEPT_LARGE_DLC);
            status     = Canlib.canAccept(readHandle, 0x7DF, Canlib.canFILTER_SET_CODE_STD);
            status     = Canlib.canAccept(readHandle, 0x7D0, Canlib.canFILTER_SET_MASK_STD);
            status     = Canlib.canBusOn(readHandle);
            if (status == Canlib.canStatus.canOK)
            {
                onBus = true;
            }
            else
            {
                onBus = false;
            }
            if (readHandle < 0)
            {
                errmsg = "canOpenChannel Handle create error";
                return(false);
            }
            if (!onBus)
            {
                errmsg = "onBus false";
                return(false);
            }
            _stopwatch_total.Reset();
            _stopwatch_total.Start();
            _stopwatch_send.Reset();
            _stopwatch_send.Start();
            // Receive Channel Open -> SEND
            Canlib.canBusOn(handle);
            status = Canlib.canWrite(handle, id, _Data_FF, 8, flags);
            while (_LoopChk)
            {
                // TimeOut Check
                long   st            = _stopwatch_total.ElapsedMilliseconds;
                double _durationtime = st / 1000.0;  // ms -> sec
                if (_durationtime > timeout)
                {
                    // 설정시간안에 메세지 및 처리를 못했을 경우 종료
                    errmsg   = "timeout error";
                    _LoopChk = false;
                    break;
                }
                // Message Recieve Check
                bool _RecvOK = false;
                status = Canlib.canReadWait(readHandle, out _EcuID, _EcuData, out _EcuDlc, out _EcuFlags, out _EcuTime, 100); // 100ms timeout
                if (status == Canlib.canStatus.canOK)
                {
                    if ((_EcuFlags & Canlib.canMSG_ERROR_FRAME) == Canlib.canMSG_ERROR_FRAME)
                    {
                        errmsg = "***ERROR FRAME RECEIVED***";
                    }
                    else
                    {
                        errmsg = String.Format("ID={0:X4}  {1}  {2:X2} {3:X2} {4:X2} {5:X2} {6:X2} {7:X2} {8:X2} {9:X2}   {10}\r",
                                               _EcuID, _EcuDlc, _EcuData[0], _EcuData[1], _EcuData[2], _EcuData[3], _EcuData[4],
                                               _EcuData[5], _EcuData[6], _EcuData[7], _EcuTime);
                        if (_EcuID == 0x07D9)
                        {
                            _RecvOK = true;
                        }
                    }
                }
                else if (status != Canlib.canStatus.canERR_NOMSG)
                {
                    errmsg = "TimeOut:NO MSG";
                }
                //CheckStatus("Read message " + errmsg, status);
                //Canlib.canBusOff(readHandle);
                if ((_EcuID == 0x07D9) && (_RecvOK))
                {
                    byte EcuFC_Check = _EcuData[0];
                    EcuFC_Check = (byte)((EcuFC_Check & 0x00F0) >> 4);
                    if (EcuFC_Check == 3)
                    {
                        _LoopChk   = false;
                        _Ecu_FS    = (_EcuData[0] & 0x000F);
                        _Ecu_BS    = (int)(_EcuData[1] & 0x00FF);
                        _Ecu_STmin = (int)(_EcuData[2] & 0x00FF);
                        if (_Ecu_STmin <= 0x7F)
                        {
                            _Ecu_Stmin_sec = _Ecu_STmin * 0.001;
                        }
                        if (_Ecu_STmin >= 0xF1)
                        {
                            _Ecu_Stmin_sec = (_Ecu_STmin - 0xF0) * 0.0001;
                        }
                        _Ecu_FlowControl_Receive_OK = true;
                    }
                    string sndmsg = String.Format("ID={0:X4}  {1}  {2:X2} {3:X2} {4:X2} {5:X2} {6:X2} {7:X2} {8:X2} {9:X2}\r",
                                                  id, 8, _Data_FF[0], _Data_FF[1], _Data_FF[2], _Data_FF[3], _Data_FF[4],
                                                  _Data_FF[5], _Data_FF[6], _Data_FF[7]);
                    CheckStatus("PC->ECU " + sndmsg, Canlib.canStatus.canOK);
                    CheckStatus("ECU->PC " + errmsg, status);
                }
            }

            Canlib.canClose(readHandle);

            if (!_Ecu_FlowControl_Receive_OK)
            {
                errmsg = "Ecu FlowControl not arrived";
                return(false);
            }
            if (_Ecu_FS > 0)
            {
                errmsg = "Ecu FlowControl signal Busy";
                return(false);
            }



            // ECU에서 FC를 정상적으로 수신후 데이터 전송....
            _stopwatch_total.Reset();
            _stopwatch_total.Start();
            _stopwatch_send.Reset();
            _stopwatch_send.Start();
            _LoopChk = true;
            int  _SequenceNumber     = 1;
            byte _SN                 = (byte)_SequenceNumber;
            int  _BlockSendLoopCount = _Ecu_BS;

            Canlib.canBusOn(handle);
            bool _EcuLastReceiveOK = false;

            // CF 전송후 수신확인.
            //Open up a new handle for reading
            readHandle = Canlib.canOpenChannel(channel, Canlib.canOPEN_ACCEPT_LARGE_DLC);
            status     = Canlib.canAccept(readHandle, 0x7DF, Canlib.canFILTER_SET_CODE_STD);
            status     = Canlib.canAccept(readHandle, 0x7D0, Canlib.canFILTER_SET_MASK_STD);
            status     = Canlib.canBusOn(readHandle);
            if (status == Canlib.canStatus.canOK)
            {
                onBus = true;
            }
            else
            {
                onBus = false;
            }
            if (readHandle < 0)
            {
                errmsg = "canOpenChannel Handle create error";
                return(false);
            }
            if (!onBus)
            {
                errmsg = "onBus false";
                return(false);
            }

            while (_LoopChk)
            {
                // TimeOut Check
                long   st            = _stopwatch_total.ElapsedMilliseconds;
                double _durationtime = st / 1000.0;  // ms -> sec
                if (_durationtime > timeout)
                {
                    // 설정시간안에 메세지 및 처리를 못했을 경우 종료
                    errmsg   = "timeout error";
                    _LoopChk = false;
                    break;
                }
                if (datalength < _LastSendDataIndex)
                {
                    _LoopChk = false;
                    break;
                }
                _stopwatch_send.Reset();
                _stopwatch_send.Start();
                // CF 전송
                for (int hs = 0; hs < _BlockSendLoopCount; hs++)
                {
                    // FF전송후 CF 데이터 전송 준비
                    _Data_CF[0] = 0x20;
                    _Data_CF[0] = (byte)(_Data_CF[0] + _SN);
                    for (int i = 0; i < 7; i++)
                    {
                        if (_LastSendDataIndex < datalength)
                        {
                            _Data_CF[1 + i] = data[_LastSendDataIndex];
                        }
                        else
                        {
                            _Data_CF[1 + i] = 0x00; // 0xAA;
                        }
                        _LastSendDataIndex++;
                    }
                    _SN++;
                    if (_SN > 16)
                    {
                        _SN = 0;
                    }
                    status = Canlib.canWrite(handle, id, _Data_CF, 8, flags);
                    long   _dt    = _stopwatch_send.ElapsedMilliseconds;
                    string sndmsg = String.Format("ID={0:X4}  {1}  {2:X2} {3:X2} {4:X2} {5:X2} {6:X2} {7:X2} {8:X2} {9:X2}   {10:D5}ms\r",
                                                  id, 8, _Data_CF[0], _Data_CF[1], _Data_CF[2], _Data_CF[3], _Data_CF[4],
                                                  _Data_CF[5], _Data_CF[6], _Data_CF[7], _dt);
                    CheckStatus("PC->ECU " + sndmsg, status);
                    break; // Only One Run-----------------------------------------------실제 전송될 데이터 길이만큼만 전송함----------------------------------------------------------
                }


                // Message Recieve Check
                bool _RecvOK = false;

                bool      _FinalReceiveCheck = true;
                Stopwatch _Final             = new Stopwatch();
                _Final.Reset();
                _Final.Start();
                while (_FinalReceiveCheck)
                {
                    if ((_Final.ElapsedMilliseconds / 1000.0) > 3.0)
                    {
                        errmsg             = "Finnal ECU Receive Msg TIMEOUT";
                        _FinalReceiveCheck = false;
                        break;
                    }
                    status = Canlib.canReadWait(readHandle, out _EcuID, _EcuData, out _EcuDlc, out _EcuFlags, out _EcuTime, 100); // 100ms timeout
                    if (status == Canlib.canStatus.canOK)
                    {
                        if ((_EcuFlags & Canlib.canMSG_ERROR_FRAME) == Canlib.canMSG_ERROR_FRAME)
                        {
                            errmsg = "***ERROR FRAME RECEIVED***";
                        }
                        else
                        {
                            errmsg = String.Format("ID={0:X4}  {1}  {2:X2} {3:X2} {4:X2} {5:X2} {6:X2} {7:X2} {8:X2} {9:X2}   {10}\r",
                                                   _EcuID, _EcuDlc, _EcuData[0], _EcuData[1], _EcuData[2], _EcuData[3], _EcuData[4],
                                                   _EcuData[5], _EcuData[6], _EcuData[7], _EcuTime);
                            if (_EcuID == 0x07D9)
                            {
                                _RecvOK = true;
                                CheckStatus("ECU->PC " + errmsg, status);
                            }
                        }
                    }
                    else if (status != Canlib.canStatus.canERR_NOMSG)
                    {
                        errmsg = "TimeOut:NO MSG";
                    }
                    //CheckStatus("Read message " + errmsg, status);
                    //Canlib.canBusOff(readHandle);
                    if ((_EcuID == 0x07D9) && (_RecvOK))
                    {
                        Canlib.canBusOn(handle);
                        byte[] temp = new byte[8] {
                            0x30, 0x00, 0x02, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
                        };
                        status = Canlib.canWrite(handle, 0x07D1, _Data_FF, 8, flags);
                        CheckStatus("PC->ECU " + "PC AutoResponse(0x30,0x00,0x02,0xAA,0xAA,0xAA,0xAA,0xaa", status);
                        Canlib.canBusOff(handle);
                        status = Canlib.canReadWait(readHandle, out _EcuID, _EcuData, out _EcuDlc, out _EcuFlags, out _EcuTime, 100);
                        if (status == Canlib.canStatus.canOK)
                        {
                            errmsg = String.Format("ID={0:X4}  {1}  {2:X2} {3:X2} {4:X2} {5:X2} {6:X2} {7:X2} {8:X2} {9:X2}   {10}\r",
                                                   _EcuID, _EcuDlc, _EcuData[0], _EcuData[1], _EcuData[2], _EcuData[3], _EcuData[4],
                                                   _EcuData[5], _EcuData[6], _EcuData[7], _EcuTime);
                            if (_EcuID == 0x07D9)
                            {
                                _RecvOK = true;
                                CheckStatus("ECU->PC " + errmsg, status);
                            }
                        }
                        _FinalReceiveCheck = false;
                        _EcuLastReceiveOK  = true;
                        _LoopChk           = false;
                        break;
                    }
                }
            }
            if (readHandle >= 0)
            {
                Canlib.canClose(readHandle);
            }
            if (_EcuLastReceiveOK)
            {
                if (_EcuData[1] == 0x62)
                {
                    return(true);
                }
                return(false);
            }
            else
            {
                return(false);
            }
        }
Пример #9
0
        private bool CanShortMsgSend(int id, byte[] data, int datalength, int retrycount, double timeout, ref int EcuID, ref byte[] READDATA, ref string errmsg)
        {
            bool _LoopChk = true;
            bool _Result  = false;
            int  _dlc     = datalength;
            int  _flags   = this.flags;
            int  _channel = this.channel;
            long _time;
            int  _id      = id;
            int  _handle  = this.handle;
            bool _noError = true;

            Canlib.canStatus status;
            string           msg = "";

            byte[]    _ReadData         = new byte[8];
            Stopwatch _stopwatch        = new Stopwatch();
            bool      _OnlyOneReceiveOK = false;

            //Canlib.canBusOn(_handle);
            //status = Canlib.canWrite(_handle, _id, data, _dlc, _flags);
            //Open up a new handle for reading

            /*
             * readHandle = Canlib.canOpenChannel(_channel, Canlib.canOPEN_ACCEPT_LARGE_DLC);
             * status     = Canlib.canAccept(readHandle, 0x7DF, Canlib.canFILTER_SET_CODE_STD);
             * status     = Canlib.canAccept(readHandle, 0x7D0, Canlib.canFILTER_SET_MASK_STD);
             * status     = Canlib.canBusOn(readHandle);
             * if (status == Canlib.canStatus.canOK) onBus = true;
             * else                                  onBus = false;
             * if (readHandle < 0)
             * {
             *  errmsg = "canOpenChannel Handle create error";
             *  return false;
             * }
             * if (!onBus)
             * {
             *  errmsg = "onBus false";
             *  return false;
             * }
             */
            string sndmsg = String.Format("ID={0:X4}  {1}  {2:X2} {3:X2} {4:X2} {5:X2} {6:X2} {7:X2} {8:X2} {9:X2}   {10}\r",
                                          _id, _dlc, data[0], data[1], data[2], data[3], data[4],
                                          data[5], data[6], data[7], 0);

            CheckStatus("PC->ECU " + sndmsg, Canlib.canStatus.canOK);
            Canlib.canBusOn(_handle);
            status = Canlib.canWrite(_handle, _id, data, _dlc, _flags);
            _stopwatch.Reset();
            _stopwatch.Start();
            while (_LoopChk)
            {
                // TimeOut Check
                long   st            = _stopwatch.ElapsedMilliseconds;
                double _durationtime = st / 1000.0;  // ms -> sec
                if (_durationtime > timeout)
                {
                    // 설정시간안에 메세지 및 처리를 못했을 경우 종료
                    msg      = "timeout error";
                    _LoopChk = false;
                    break;
                }
                // Message Recieve Check
                status = Canlib.canReadWait(_handle, out _id, _ReadData, out _dlc, out _flags, out _time, 100); // 100ms timeout
                if (status == Canlib.canStatus.canOK)
                {
                    if ((_flags & Canlib.canMSG_ERROR_FRAME) == Canlib.canMSG_ERROR_FRAME)
                    {
                        msg = "***ERROR FRAME RECEIVED***";
                    }
                    else
                    {
                        msg = String.Format("ID={0:X4}  {1}  {2:X2} {3:X2} {4:X2} {5:X2} {6:X2} {7:X2} {8:X2} {9:X2}   {10}\r",
                                            _id, _dlc, _ReadData[0], _ReadData[1], _ReadData[2], _ReadData[3], _ReadData[4],
                                            _ReadData[5], _ReadData[6], _ReadData[7], _time);
                        if (_id == 0x07D9)
                        {
                            for (int i = 0; i < 8; i++)
                            {
                                READDATA[i] = _ReadData[i];
                            }
                            _Result           = true;
                            _OnlyOneReceiveOK = true;
                        }
                    }
                }
                else if (status != Canlib.canStatus.canERR_NOMSG)
                {
                    msg = "TimeOut:NO MSG";
                }
                if (_id == 0x7D9)
                {
                    CheckStatus("ECU->PC " + msg, status);
                    Canlib.canBusOff(_handle);
                    _LoopChk = false;
                }
                //onBus = false;
            }
            return(_Result);
        }
Пример #10
0
        private bool CanSend_Frame(int id, byte[] data, int datalength, int ReceiveRetryCount, ref int EcuID, ref byte[] READDATA, ref int ReadDataCount, ref string ErrMsg)
        {
            // Real Data Count Only...
            int _handle = this.handle;
            int _flags  = this.flags;

            Canlib.canStatus status;

            byte[] SendData = new byte[8];
            byte[] TempData = new byte[8] {
                0x30, 0x00, 0x02, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
            };                                                                                 // Flow Control Signal

            int RecvID = 0;

            byte[] RecvData       = new byte[8];
            byte[] RecvDataBuffer = new byte[50];
            int    RecvDataIndex  = 0;
            int    RecvDLC;
            int    RecvFlags;
            long   RecvTime;
            long   RecvSetTimeOut     = 50;
            int    _ReceiveRetryCount = 0;


            Canlib.canBusOn(_handle);
            bool SendAll   = false;
            int  DataIndex = 0;

            while (!SendAll)
            {
                for (int i = 0; i < 8; i++)
                {
                    if (DataIndex < datalength)
                    {
                        SendData[i] = data[DataIndex];
                    }
                    else
                    {
                        SendData[i] = 0x00;
                    }
                    DataIndex++;
                }
                // Data Send
                status = Canlib.canWrite(_handle, id, SendData, 8, _flags);
                CheckStatus("PC->ECU " + MsgConv(id, 8, SendData, 0), status);
                bool MsgReceive = false;
                while (!MsgReceive)
                {
                    status = Canlib.canReadWait(_handle, out RecvID, RecvData, out RecvDLC, out RecvFlags, out RecvTime, RecvSetTimeOut);
                    if ((status == Canlib.canStatus.canOK) && (!((_flags & Canlib.canMSG_ERROR_FRAME) == Canlib.canMSG_ERROR_FRAME)))
                    {
                        if (RecvID == 0x07D9)
                        {
                            CheckStatus("ECU->PC " + MsgConv(RecvID, 8, RecvData, 0), status);
                            if ((RecvData[0] & 0xF0) == 0x30)
                            {
                                for (int i = 0; i < 8; i++)
                                {
                                    RecvDataBuffer[RecvDataIndex] = RecvData[i]; RecvDataIndex++;
                                }
                                MsgReceive = true;
                                break;
                            }
                            if ((RecvData[0] & 0xF0) == 0x10)
                            { // FF Respons
                                status = Canlib.canWrite(_handle, id, TempData, 8, _flags);
                                for (int i = 0; i < 8; i++)
                                {
                                    RecvDataBuffer[RecvDataIndex] = RecvData[i]; RecvDataIndex++;
                                }
                            }
                            if ((RecvData[0] & 0xF0) == 0x20)
                            { // CF Respons
                                for (int i = 0; i < 8; i++)
                                {
                                    RecvDataBuffer[RecvDataIndex] = RecvData[i]; RecvDataIndex++;
                                }
                            }
                            if ((RecvData[0] & 0xF0) == 0x00)
                            { // Single Frame
                                MsgReceive = true;
                                break;
                            }
                        }
                    }
                    if (_ReceiveRetryCount > ReceiveRetryCount)
                    {
                        break;
                    }
                    _ReceiveRetryCount++;
                }
                if (DataIndex > datalength)
                {
                    SendAll = true;
                    break;
                }
            }
            Canlib.canBusOff(_handle);
            if (RecvDataIndex > 0)
            {
                EcuID         = RecvID;
                READDATA      = RecvDataBuffer;
                ReadDataCount = RecvDataIndex;
            }
            else
            {
                EcuID         = RecvID;
                READDATA      = RecvData;
                ReadDataCount = 8;
            }
            return(true);
        }
Пример #11
0
        private void canThreadFunct()
        {
            int canHandle;

            Canlib.canStatus status;

            int channel_number = 0;
            int id;

            byte[] msg = new byte[8];
            int    dlc;
            int    flag;
            long   time;
            long   timeout;
            int    val0   = 0;
            int    val1   = 0;
            int    val2   = 0;
            int    val3   = 0;
            long   count0 = 1;
            long   count1 = 2;
            long   count2 = 3;
            long   count3 = 4;

            Canlib.canInitializeLibrary();
            canHandle = Canlib.canOpenChannel(channel_number, Canlib.canOPEN_NO_INIT_ACCESS);
            CheckStatus((Canlib.canStatus)canHandle, "canOpenChannel");

            // Next, take the channel on bus using the canBusOn method. This
            // needs to be done before we can send a message.
            status = Canlib.canBusOn(canHandle);
            CheckStatus(status, "canBusOn");

            timeout = 1000;

            while (!exit_request)
            {
                status = Canlib.canReadWait(canHandle, out id, msg, out dlc, out flag, out time, timeout);
                CheckStatus(status, "canReadWait");
                if (id == 0x700)
                {
                    val0   = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(msg, 0));
                    count0 = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(msg, 4));
                }
                if (id == 0x701)
                {
                    val1   = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(msg, 0));
                    count1 = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(msg, 4));
                }
                if (id == 0x702)
                {
                    val2   = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(msg, 0));
                    count2 = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(msg, 4));
                }
                if (id == 0x703)
                {
                    val3   = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(msg, 0));
                    count3 = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(msg, 4));
                }
                if (count0 == count1 && count0 == count2 && count0 == count3)
                {
                    realtimeChart.DataSample genValue = new realtimeChart.DataSample();
                    genValue.value[0] = val0;
                    genValue.value[1] = val1;
                    genValue.value[2] = val2;
                    genValue.value[3] = val3;
                    mainChart.AddValue(genValue);
                }
            }
            status = Canlib.canBusOff(canHandle);
            CheckStatus(status, "canBusOff");
        }