/// <summary> /// Set filter /// </summary> private Response <J2534Err> SetFilter(UInt32 Mask, UInt32 Pattern, UInt32 FlowControl, TxFlag txflag, FilterType Filtertype) { PassThruMsg maskMsg; PassThruMsg patternMsg; IntPtr MaskPtr; IntPtr PatternPtr; IntPtr FlowPtr; maskMsg = new PassThruMsg(Protocol, txflag, new Byte[] { (byte)(0xFF & (Mask >> 16)), (byte)(0xFF & (Mask >> 8)), (byte)(0xFF & Mask) }); patternMsg = new PassThruMsg(Protocol, txflag, new Byte[] { (byte)(0xFF & (Pattern >> 16)), (byte)(0xFF & (Pattern >> 8)), (byte)(0xFF & Pattern) }); MaskPtr = maskMsg.ToIntPtr(); PatternPtr = patternMsg.ToIntPtr(); FlowPtr = IntPtr.Zero; int tempfilter = 0; OBDError = J2534Port.Functions.PassThruStartMsgFilter((int)ChannelID, Filtertype, MaskPtr, PatternPtr, FlowPtr, ref tempfilter); Marshal.FreeHGlobal(MaskPtr); Marshal.FreeHGlobal(PatternPtr); Marshal.FreeHGlobal(FlowPtr); if (OBDError != J2534Err.STATUS_NOERROR) { return(Response.Create(ResponseStatus.Error, OBDError)); } Filters.Add((ulong)tempfilter); return(Response.Create(ResponseStatus.Success, OBDError)); }
/// <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); }
public void ConnectISO15765() { Connect(); byte[] value; J2534Status = J2534Interface.PassThruConnect(DeviceId, ProtocolID.ISO15765, ConnectFlag.NONE, BaudRate.ISO15765, ref ChannelId); if (J2534Err.STATUS_NOERROR != J2534Status) { throw new J2534Exception(J2534Status); } //List<SConfig> configBits = new List<SConfig>(); //SConfig conf = new SConfig(); //conf.Parameter = ConfigParameter.LOOPBACK; //conf.Value = 0; //SConfig conf1 = new SConfig(); //conf1.Parameter = ConfigParameter.DATA_RATE; //conf1.Value = 0; //configBits.Add(conf); //configBits.Add(conf1); //m_status = m_j2534Interface.GetConfig(m_channelId, ref configBits); int filterId = 0; byte i; for (i = 0; i < 1; i++) { 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, (byte)(0xE8 + i) }); PassThruMsg flowControlMsg = new PassThruMsg(ProtocolID.ISO15765, TxFlag.ISO15765_FRAME_PAD, new byte[] { 0x00, 0x00, 0x07, (byte)(0xE0 + i) }); J2534Status = J2534Interface.PassThruStartMsgFilter( ChannelId, FilterType.FLOW_CONTROL_FILTER, maskMsg.ToIntPtr(), patternMsg.ToIntPtr(), flowControlMsg.ToIntPtr(), ref filterId); if (J2534Err.STATUS_NOERROR != J2534Status) { J2534Interface.PassThruDisconnect(ChannelId); throw new J2534Exception(J2534Status); } } //Check we can read some PIDs back ReadObdPid(OBDcmd.Mode.REQUEST_CURRENT_DATA, out value); if (value.Length <= 0) { //m_status = j2534Interface.PassThruDisconnect(channelId); //throw new OBDException(OBDcmd.Response.NEGATIVE_RESPONSE); } ProtocolId = ProtocolID.ISO15765; }
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); }
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); } }
public bool ConnectIso15765() { List <byte> value = new List <byte>(); m_status = m_j2534Interface.PassThruConnect(m_deviceId, ProtocolID.ISO15765, ConnectFlag.NONE, BaudRate.ISO15765, ref m_channelId); if (J2534Err.STATUS_NOERROR != m_status) { return(false); } int filterId = 0; byte i; for (i = 0; i < 1; i++) { 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, (byte)(0xE8 + i) }); PassThruMsg flowControlMsg = new PassThruMsg(ProtocolID.ISO15765, TxFlag.ISO15765_FRAME_PAD, new byte[] { 0x00, 0x00, 0x07, (byte)(0xE0 + i) }); m_status = m_j2534Interface.PassThruStartMsgFilter( m_channelId, FilterType.FLOW_CONTROL_FILTER, maskMsg.ToIntPtr(), patternMsg.ToIntPtr(), flowControlMsg.ToIntPtr(), ref filterId); if (J2534Err.STATUS_NOERROR != m_status) { m_j2534Interface.PassThruDisconnect(m_channelId); return(false); } } if (!ReadObdPid(0x01, 0x00, ProtocolID.ISO15765, ref value)) { m_j2534Interface.PassThruDisconnect(m_channelId); return(false); } return(true); }
/// <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)); }
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); }
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); }
/* * * 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(); }
public void SetNullFilters(int swChannelId, int dwChannelId) { // Set up a message filter to watch for response messages int filterId = 0; PassThruMsg swMask1 = new PassThruMsg( ProtocolID.SW_CAN, TxFlag.CAN_29BIT_ID, new byte[] { 0x00, 0x00, 0x00, 0x00 }); PassThruMsg swMask2 = new PassThruMsg( ProtocolID.SW_CAN, TxFlag.CAN_29BIT_ID, new byte[] { 0x10, 0x00, 0x00, 0x00 }); PassThruMsg swMask3 = new PassThruMsg( ProtocolID.SW_CAN, TxFlag.CAN_29BIT_ID, new byte[] { 0x0f, 0x00, 0x00, 0x00 }); PassThruMsg dwMask1 = new PassThruMsg( ProtocolID.DW_CAN, TxFlag.CAN_29BIT_ID, new byte[] { 0x00, 0x00, 0x00, 0x00 }); PassThruMsg dwMask2 = new PassThruMsg( ProtocolID.DW_CAN, TxFlag.CAN_29BIT_ID, new byte[] { 0x10, 0x00, 0x00, 0x00 }); PassThruMsg dwMask3 = new PassThruMsg( ProtocolID.DW_CAN, TxFlag.CAN_29BIT_ID, new byte[] { 0x0f, 0x00, 0x00, 0x00 }); m_j2534Interface.PassThruStartMsgFilter(swChannelId, FilterType.PASS_FILTER, swMask1.ToIntPtr(), swMask1.ToIntPtr(), IntPtr.Zero, ref filterId); m_j2534Interface.PassThruStartMsgFilter(swChannelId, FilterType.PASS_FILTER, swMask2.ToIntPtr(), swMask2.ToIntPtr(), IntPtr.Zero, ref filterId); m_j2534Interface.PassThruStartMsgFilter(swChannelId, FilterType.PASS_FILTER, swMask3.ToIntPtr(), swMask3.ToIntPtr(), IntPtr.Zero, ref filterId); m_j2534Interface.PassThruStartMsgFilter(dwChannelId, FilterType.PASS_FILTER, dwMask1.ToIntPtr(), dwMask1.ToIntPtr(), IntPtr.Zero, ref filterId); m_j2534Interface.PassThruStartMsgFilter(dwChannelId, FilterType.PASS_FILTER, dwMask2.ToIntPtr(), dwMask2.ToIntPtr(), IntPtr.Zero, ref filterId); m_j2534Interface.PassThruStartMsgFilter(dwChannelId, FilterType.PASS_FILTER, dwMask3.ToIntPtr(), dwMask3.ToIntPtr(), IntPtr.Zero, ref filterId); }
/// <summary> /// </summary> /// <returns>OpenResult.OK is returned on success. Otherwise OpenResult.OpenError is /// returned.</returns> override public OpenResult open() { if (isOpen()) { close(); } m_deviceId = 0; m_status = passThru.PassThruOpen(IntPtr.Zero, ref m_deviceId); if (m_status != J2534Err.STATUS_NOERROR) { return(OpenResult.OpenError); } m_readThread = new Thread(readMessages) { Name = "J2534CANDevice.m_readThread" }; m_endThread = false; if (TrionicECU == API.ECU.TRIONIC5) { m_status = passThru.PassThruConnect(m_deviceId, ProtocolID.CAN, ConnectFlag.NONE, BaudRate.CAN_615000, ref m_channelId); } else { m_status = passThru.PassThruConnect(m_deviceId, ProtocolID.CAN, ConnectFlag.NONE, BaudRate.CAN_500000, ref m_channelId); } if (J2534Err.STATUS_NOERROR != m_status) { return(OpenResult.OpenError); } uint acpFilt = 0xFFFF; uint acpMask = 0x0000; foreach (var id in AcceptOnlyMessageIds) { acpFilt &= id; acpMask |= id; } acpMask = (~acpMask & 0x7FF) | acpFilt; logger.Debug("Filter: " + acpFilt.ToString("X8")); logger.Debug("Mask: " + acpMask.ToString("X8")); byte[] maskBytes = new byte[4]; byte[] patternBytes = new byte[4]; for (int i = 0; i < 4; i++) { maskBytes[i] = (byte)(acpMask >> (i * 8)); patternBytes[i] = (byte)(acpFilt >> (i * 8)); } //PassThruMsg maskMsg = new PassThruMsg(ProtocolID.CAN, TxFlag.NONE, maskBytes); //PassThruMsg patternMsg = new PassThruMsg(ProtocolID.CAN, TxFlag.NONE, patternBytes); PassThruMsg maskMsg = new PassThruMsg(ProtocolID.CAN, TxFlag.NONE, new byte[] { 0x00, 0x00, 0x00, 0x00 }); PassThruMsg patternMsg = new PassThruMsg(ProtocolID.CAN, TxFlag.NONE, new byte[] { 0x00, 0x00, 0x00, 0x00 }); int filterId = 0; m_status = passThru.PassThruStartMsgFilter( m_channelId, FilterType.PASS_FILTER, maskMsg.ToIntPtr(), patternMsg.ToIntPtr(), IntPtr.Zero, ref filterId); if (J2534Err.STATUS_NOERROR != m_status) { return(OpenResult.OpenError); } m_status = passThru.PassThruIoctl(m_channelId, (int)Ioctl.CLEAR_RX_BUFFER, IntPtr.Zero, IntPtr.Zero); if (J2534Err.STATUS_NOERROR != m_status) { return(OpenResult.OpenError); } logger.Debug("P bus connected"); if (m_readThread.ThreadState == ThreadState.Unstarted) { m_readThread.Start(); } m_deviceIsOpen = true; return(OpenResult.OK); }