Exemplo n.º 1
0
        public bool ConnectIso15765(string RequestID, string ResponseID, BaudRate baudRate)
        {
            List <byte> value = new List <byte>();

            J2534ErrStatus = passThru.Connect(deviceId, ProtocolID.ISO15765, ConnectFlag.NONE, baudRate, ref channelId);
            if (J2534Err.STATUS_NOERROR != J2534ErrStatus)
            {
                return(false);
            }

            PassThruMsg maskMsg        = new PassThruMsg();
            PassThruMsg patternMsg     = new PassThruMsg();
            PassThruMsg flowControlMsg = new PassThruMsg();

            byte i;

            for (i = 0; i < 1; i++)
            {
                maskMsg.ProtocolID = ProtocolID.ISO15765;
                maskMsg.TxFlags    = TxFlag.ISO15765_FRAME_PAD;
                maskMsg.Data       = new byte[] { 0xff, 0xff, 0xff, 0xff }; //4 "FF" for stand ID,5 "FF" for extend ID

                byte ReqID_1 = (byte)Convert.ToUInt16(RequestID.Trim().ToUpper().Substring(0, 1), 16);
                byte ReqID_2 = (byte)Convert.ToUInt16(RequestID.Trim().ToUpper().Substring(1, 2), 16);

                byte RespID_1 = (byte)Convert.ToUInt16(ResponseID.Trim().ToUpper().Substring(0, 1), 16);
                byte RespID_2 = (byte)Convert.ToUInt16(ResponseID.Trim().ToUpper().Substring(1, 2), 16);

                patternMsg.ProtocolID = ProtocolID.ISO15765;
                patternMsg.TxFlags    = TxFlag.ISO15765_FRAME_PAD;
                patternMsg.Data       = new byte[] { 0x00, 0x00, RespID_1, RespID_2 };

                flowControlMsg.ProtocolID = ProtocolID.ISO15765;
                flowControlMsg.TxFlags    = TxFlag.ISO15765_FRAME_PAD;
                flowControlMsg.Data       = new byte[] { 0x00, 0x00, ReqID_1, ReqID_2 };

                J2534ErrStatus = passThru.StartMsgFilter(channelId, FilterType.FLOW_CONTROL_FILTER, ref maskMsg, ref patternMsg, ref flowControlMsg, ref filterId);
                if (J2534Err.STATUS_NOERROR != J2534ErrStatus)
                {
                    passThru.Disconnect(channelId);
                    return(false);
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        /*
         *
         *  Example 2:
         *      Use the J2534 protocol to send and receive a message (w/o error checking)
         *
         */
        private void SendReceiveNoErrorChecking(object sender, EventArgs e)
        {
            J2534 passThru = new J2534();

            // Find all of the installed J2534 passthru devices
            List <J2534Device> availableJ2534Devices = J2534Detect.ListDevices();

            // We will always choose the first J2534 device in the list, if there are multiple devices
            //   installed, you should do something more intelligent.
            passThru.LoadLibrary(availableJ2534Devices[0]);

            // Attempt to open a communication link with the pass thru device
            int deviceId = 0;

            passThru.Open(ref deviceId);

            // Open a new channel configured for ISO15765 (CAN)
            int channelId = 0;

            passThru.Connect(deviceId, ProtocolID.ISO15765, ConnectFlag.NONE, BaudRate.ISO15765, ref channelId);

            // Set up a message filter to watch for response messages
            int         filterId = 0;
            PassThruMsg maskMsg  = new PassThruMsg(
                ProtocolID.ISO15765,
                TxFlag.ISO15765_FRAME_PAD,
                new byte[] { 0xff, 0xff, 0xff, 0xff });
            PassThruMsg patternMsg = new PassThruMsg(
                ProtocolID.ISO15765,
                TxFlag.ISO15765_FRAME_PAD,
                new byte[] { 0x00, 0x00, 0x07, 0xE8 });
            PassThruMsg flowControlMsg = new PassThruMsg(
                ProtocolID.ISO15765,
                TxFlag.ISO15765_FRAME_PAD,
                new byte[] { 0x00, 0x00, 0x07, 0xE0 });

            passThru.StartMsgFilter(channelId, FilterType.FLOW_CONTROL_FILTER, ref maskMsg, ref patternMsg, ref flowControlMsg, ref filterId);

            // Clear out the response buffer so we know we're getting the freshest possible data
            passThru.ClearRxBuffer(channelId);

            // Finally we can send the message!
            PassThruMsg txMsg = new PassThruMsg(
                ProtocolID.ISO15765,
                TxFlag.ISO15765_FRAME_PAD,
                new byte[] { 0x00, 0x00, 0x07, 0xdf, 0x01, 0x00 });
            int numMsgs = 1;

            passThru.WriteMsgs(channelId, ref txMsg, ref numMsgs, 50);

            // Read messages in a loop until we either timeout or we receive data
            List <PassThruMsg> rxMsgs = new List <PassThruMsg>();
            J2534Err           status = J2534Err.STATUS_NOERROR;

            numMsgs = 1;
            while (J2534Err.STATUS_NOERROR == status)
            {
                status = passThru.ReadMsgs(channelId, ref rxMsgs, ref numMsgs, 200);
            }

            // If we received data, we want to extract the data of interest.  I'm removing the reflection of the transmitted message.
            List <byte> responseData;

            if ((J2534Err.ERR_BUFFER_EMPTY == status || J2534Err.ERR_TIMEOUT == status) && rxMsgs.Count > 1)
            {
                responseData = rxMsgs[rxMsgs.Count - 1].Data.ToList();
                responseData.RemoveRange(0, txMsg.Data.Length);
            }

            //
            //
            // Now do something with the data!
            //
            //

            // Disconnect this channel
            passThru.Disconnect(channelId);

            // When we are done with the device, we can free the library.
            passThru.FreeLibrary();
        }