예제 #1
0
        /// <summary>
        /// sendMessage send a CANMessage.
        /// </summary>
        /// <param name="a_message">A CANMessage.</param>
        /// <returns>true on success, othewise false.</returns>
        override protected bool sendMessageDevice(CANMessage a_message)
        {
            if (m_endThread)
            {
                return(false);
            }
            byte[] msg = a_message.getHeaderAndData();

            PassThruMsg txMsg = new PassThruMsg();

            txMsg.ProtocolID = ProtocolID.CAN;
            txMsg.TxFlags    = TxFlag.NONE;
            txMsg.SetBytes(msg);

            int       numMsgs = 1;
            const int timeout = 0;

            m_status = passThru.PassThruWriteMsgs(m_channelId, txMsg.ToIntPtr(), ref numMsgs, timeout);

            if (J2534Err.STATUS_NOERROR != m_status)
            {
                logger.Debug(String.Format("tx failed with status {0} {1}", m_status, BitConverter.ToString(msg)));
                return(false);
            }
            return(true);
        }
예제 #2
0
        public void SendMessage(byte[] payload, bool headerSuppled = false)
        {
            J2534Status = J2534Interface.ClearTxBuffer(ChannelId);

            var txMsg = new PassThruMsg();
            int timeout;

            if (headerSuppled)
            {
                txMsg.SetBytes(payload);
            }
            else
            {
                //Use the standard header
                //First 4 bytes are 00 00 07 E8
                byte[] txMsgBytes = new byte[4 + payload.Length];
                txMsgBytes[2] = 0x07;
                txMsgBytes[3] = 0xE0;
                Array.Copy(payload, 0, txMsgBytes, 4, payload.Length);
                txMsg.SetBytes(txMsgBytes);
            }


            txMsg.ProtocolID = ProtocolId;


            if (ProtocolId != ProtocolID.ISO15765)
            {
                throw new J2534Exception(J2534Err.ERR_NOT_SUPPORTED);
            }

            txMsg.TxFlags = TxFlag.ISO15765_FRAME_PAD;
            timeout       = 50;


            J2534Interface.ClearRxBuffer(ChannelId);

            var numMsgs = 1;

            J2534Status = J2534Interface.PassThruWriteMsgs(ChannelId, txMsg.ToIntPtr(), ref numMsgs, timeout);
            if (J2534Err.STATUS_NOERROR != J2534Status)
            {
                throw new J2534Exception(J2534Status);
            }
        }
예제 #3
0
        public bool ResetECU()
        {
            PassThruMsg txMsg = new PassThruMsg();
            int         timeout;
            var         value = new byte[0];

            txMsg.ProtocolID = ProtocolId;
            switch (ProtocolId)
            {
            case ProtocolID.ISO15765:
                txMsg.TxFlags = TxFlag.ISO15765_FRAME_PAD;

                txMsg.SetBytes(new byte[] { 0, 0, 0x07, 0xE0, 0x11, 0x02, 0, 0 });

                timeout = 50;
                break;

            case ProtocolID.J1850PWM:
            case ProtocolID.J1850VPW:
            case ProtocolID.ISO9141:
            case ProtocolID.ISO14230:
            default:
                return(false);
            }

            J2534Interface.ClearRxBuffer(ChannelId);

            int numMsgs = 1;

            J2534Status = J2534Interface.PassThruWriteMsgs(ChannelId, txMsg.ToIntPtr(), ref numMsgs, timeout);
            if (J2534Err.STATUS_NOERROR != J2534Status)
            {
                return(false);
            }

            //Attempt to read at least 1 message as a reply
            List <PassThruMsg> messages;

            J2534Status = J2534Interface.ReadAllMessages(ChannelId, 1, _defaultTimeout, out messages, true);

            if (messages.Count <= 0)
            {
                return(false);
            }
            var response1 = messages[0].GetBytes();
            var response2 = messages[1].GetBytes();   //needs to respond with 00 00 07 e8 67 03 xx xx xx
            var code      = response2[6];

            if (response2[4] != 0x7F)
            {
                return(true);
            }
            return(false);
        }
예제 #4
0
        /// <summary>
        /// Convert a Message to an J2534 formatted transmit, and send to the interface
        /// </summary>
        private Response <J2534Err> SendNetworkMessage(Message message, TxFlag Flags)
        {
            //this.Logger.AddDebugMessage("Trace: Send Network Packet");

            PassThruMsg TempMsg = new PassThruMsg();

            TempMsg.ProtocolID = Protocol;
            TempMsg.TxFlags    = Flags;
            TempMsg.SetBytes(message.GetBytes());

            int NumMsgs = 1;

            IntPtr MsgPtr = TempMsg.ToIntPtr();

            OBDError = J2534Port.Functions.PassThruWriteMsgs((int)ChannelID, MsgPtr, ref NumMsgs, WriteTimeout);
            Marshal.FreeHGlobal(MsgPtr);
            if (OBDError != J2534Err.STATUS_NOERROR)
            {
                //Debug messages here...check why failed..
                return(Response.Create(ResponseStatus.Error, OBDError));
            }
            return(Response.Create(ResponseStatus.Success, OBDError));
        }
예제 #5
0
        public bool SendDwMessage(String id, bool extended, bool highVoltage, String data)
        {
            PassThruMsg txMsg   = new PassThruMsg();
            int         timeout = 200;
            String      rawMessage;

            if (extended)
            {
                rawMessage = CreateExtendedMessage(id, data);
            }
            else
            {
                rawMessage = CreateStandardMessage(id, data);
            }

            txMsg.ProtocolID = ProtocolID.DW_CAN;
            txMsg.SetBytes(StringToHex(rawMessage));
            if (extended)
            {
                txMsg.TxFlags |= TxFlag.CAN_29BIT_ID;
            }
            if (highVoltage)
            {
                txMsg.TxFlags |= TxFlag.SW_CAN_HV_TX;
            }

            int numMsgs = 1;

            m_status = m_j2534Interface.PassThruWriteMsgs(m_dwChannelId, txMsg.ToIntPtr(), ref numMsgs, timeout);
            if (J2534Err.STATUS_NOERROR != m_status)
            {
                return(false);
            }

            return(true);
        }
예제 #6
0
        private bool ReadObdPid(byte mode, byte pid, ProtocolID protocolId, ref List <byte> value)
        {
            PassThruMsg txMsg = new PassThruMsg();
            int         timeout;

            txMsg.ProtocolID = protocolId;
            switch (protocolId)
            {
            case ProtocolID.ISO15765:
                txMsg.TxFlags = TxFlag.ISO15765_FRAME_PAD;
                if (mode == 0x03 || mode == 0x04)
                {
                    txMsg.SetBytes(new byte[] { 0x00, 0x00, 0x07, 0xdf, mode });
                }
                else
                {
                    txMsg.SetBytes(new byte[] { 0x00, 0x00, 0x07, 0xdf, mode, pid });
                }
                timeout = 50;
                break;

            case ProtocolID.J1850PWM:
            case ProtocolID.J1850VPW:
            case ProtocolID.ISO9141:
            case ProtocolID.ISO14230:
                byte protocolByte = (byte)((protocolId == ProtocolID.J1850PWM) ? 0x61 : 0x68);
                txMsg.TxFlags = TxFlag.NONE;
                txMsg.SetBytes(new byte[] { protocolByte, 0x6A, 0xF1, mode, pid });
                timeout = 100;
                break;

            default:
                return(false);
            }

            m_j2534Interface.ClearRxBuffer(m_channelId);

            int numMsgs = 1;

            m_status = m_j2534Interface.PassThruWriteMsgs(m_channelId, txMsg.ToIntPtr(), ref numMsgs, timeout);
            if (J2534Err.STATUS_NOERROR != m_status)
            {
                return(false);
            }

            IntPtr rxMsgs = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PassThruMsg)) * numMsgs);

            numMsgs = 1;
            while (J2534Err.STATUS_NOERROR == m_status)
            {
                m_status = m_j2534Interface.PassThruReadMsgs(m_channelId, rxMsgs, ref numMsgs, timeout * 4);
            }

            if (J2534Err.ERR_BUFFER_EMPTY == m_status || J2534Err.ERR_TIMEOUT == m_status)
            {
                if (numMsgs > 0)
                {
                    // Select the last value
                    PassThruMsg msg = rxMsgs.AsMsgList(numMsgs).Last();
                    value = msg.GetBytes().ToList();
                    value.RemoveRange(0, txMsg.GetBytes().Length);
                    return(true);
                }
                return(false);
            }
            return(false);
        }