Exemplo n.º 1
0
        /*
         * Connect to GMLan SW CAN Protocol (PIN 1)
         */
        private bool ConnectSWCan(int deviceId, ref int channelId)
        {
            m_status = m_j2534Interface.PassThruConnect(deviceId, ProtocolID.SW_CAN, ConnectFlag.CAN_ID_BOTH, BaudRate.CAN_33300, ref channelId);
            if (J2534Err.STATUS_NOERROR != m_status)
            {
                m_j2534Interface.PassThruDisconnect(channelId);
                return(false);
            }

            SConfig sConfig = new SConfig();

            sConfig.Parameter = ConfigParameter.J1962_PINS;
            sConfig.Value     = 0x0100;
            List <SConfig> sConfigs = new List <SConfig>();

            sConfigs.Add(sConfig);

            SConfigList sConfigList = new SConfigList();

            sConfigList.Count   = 1;
            sConfigList.ListPtr = sConfigs.ToIntPtr();

            m_status = m_j2534Interface.PassThruIoctl(channelId, (int)Ioctl.SET_CONFIG, sConfigList.ToIntPtr(), IntPtr.Zero);
            if (J2534Err.STATUS_NOERROR != m_status)
            {
                m_j2534Interface.PassThruDisconnect(channelId);
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// The close method closes the device.
        /// </summary>
        /// <returns>CloseResult.OK on success, otherwise CloseResult.CloseError.</returns>
        override public CloseResult close()
        {
            if (m_deviceIsOpen)
            {
                m_deviceIsOpen = false;
                m_endThread    = true;

                Thread.Sleep(1200);

                m_status = passThru.PassThruDisconnect(m_channelId);
                m_status = passThru.PassThruClose(m_deviceId);
                if (m_status != J2534Err.STATUS_NOERROR)
                {
                    return(CloseResult.CloseError);
                }

                passThru.FreeLibrary();
            }
            return(CloseResult.OK);
        }
Exemplo n.º 3
0
        /*
         *
         *  Example 2:
         *      Use the J2534 protocol to send and receive a message (w/o error checking)
         *
         */
        private void SendReceiveNoErrorChecking(object sender, EventArgs e)
        {
            J2534Extended passThru = new J2534Extended();

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

            J2534Device j2534Device;
            var         sd = new SelectDevice();

            if (sd.ShowDialog() == DialogResult.OK)
            {
                j2534Device = sd.Device;
            }
            else
            {
                return;
            }

            // 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(j2534Device);

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

            passThru.PassThruOpen(IntPtr.Zero, ref deviceId);

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

            passThru.PassThruConnect(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 });

            IntPtr maskMsgPtr        = maskMsg.ToIntPtr();
            IntPtr patternMsgPtr     = patternMsg.ToIntPtr();
            IntPtr flowControlMsgPtr = flowControlMsg.ToIntPtr();

            passThru.PassThruStartMsgFilter(channelId, FilterType.FLOW_CONTROL_FILTER, maskMsgPtr, patternMsgPtr, flowControlMsgPtr, 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 });
            var txMsgPtr = txMsg.ToIntPtr();
            int numMsgs  = 1;

            passThru.PassThruWriteMsgs(channelId, txMsgPtr, ref numMsgs, 50);

            // Read messages in a loop until we either timeout or we receive data
            numMsgs = 1;
            IntPtr   rxMsgs = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PassThruMsg)) * numMsgs);
            J2534Err status = J2534Err.STATUS_NOERROR;

            while (J2534Err.STATUS_NOERROR == status)
            {
                status = passThru.PassThruReadMsgs(channelId, 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.
            if ((J2534Err.ERR_BUFFER_EMPTY == status || J2534Err.ERR_TIMEOUT == status) && numMsgs > 0)
            {
                foreach (PassThruMsg msg in rxMsgs.AsList <PassThruMsg>(numMsgs))
                {
                    //
                    //
                    // Now do something with the data!
                    //
                    //
                }
            }


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

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