CAN-bus msg validate, how? - Page 1
Forum section: electronics / projects

author: Chriss
author_rank: Frequent Contributor
timestamp: November 01, 2019, 09:20:15 pm
content: Hi!I'm planning to make a device or PC software which can listen to a CAN device like a car diagnostic tool or flasher tool also for carsover CAN-bus.The theory how the project should work is this:- the diag/flasher tool sends starts to do the job ( accessing an ecu or sending data out to try to connect to an ecu... )- the gadget I should make is listening on the CAN bus and identifying if any CAN msg is present on the CAN bus- if a msg is detected a green LED will blink otherwise a red LDV will blink.Actually, ,y gadget should validate if a valid CAN msg is present on the CAN network.I never played with CAN network but I have a beginner level knowledge about CAN network.I would use an Atmel 8bit uC with an UART and I have a CJMCU-230 CAN transceiver.The Atmel uC should be connected to the Rx/Tx on the CJMCU-230 Tx/Rx and the CJMCU-230 CAN-H and CAN-L should be connected to the diag/flasher tool I assume.Is this the basic schematic how all this should be set up?If I understand correctly the use of the CJMCU-230 or any other CAN transreceiver, they are actually like the MAX 232 ic's or the FTDI ft232 ic's just for CAN-bus and not for USB to RS232, but RS232 to CAN...If somebody could give me a direction with a schematic or some documents/info which are related to my task I would reallyappreciate that.I think this is a really basic task to start to playing with CAN-bus and I have all this tools already on my desk, so I assume I canbuild for me a small CAN-playground for startup...Thank you.My best regards.

author: zzattack
author_rank: Regular Contributor
timestamp: November 01, 2019, 09:29:56 pm
content: Ideally you use a microcontroller with built-in CAN peripheral and external transceiver. If your microcontroller lacks CAN you can use something like a MCP2515 instead, but that seems like needless effort.

author: Syntax Error
author_rank: Frequent Contributor
timestamp: November 01, 2019, 09:42:49 pm
content: For starters there is an Indestructable on this subject:https://www.instructables.com/id/Yes-We-CAN-BUS-With-Arduino-in-30-Seconds/Plus there is a good tutorial on interfacing the MCP2515 with an Arduino:https://circuitdigest.com/microcontroller-projects/arduino-can-tutorial-interfacing-mcp2515-can-bus-module-with-arduino

author: Rerouter
author_rank: Super Contributor
timestamp: November 01, 2019, 09:44:04 pm
content: a large number of microcontrollers from every brand have built in CAN trancievers, go with one of these if you can, and pick one your compfortable with, e.g. there are arduino models that have CAN libraries.This way the micro has built in hardware that rejects invalid messages (every message has a CRC) and can also filter only certain values if you want to reduce what your searching forNow with the heavy lifting out of the way, its just up to sending it out to the PC, as the messages get queued to a FIFO in most of these micros, it would be not much more than a loop of code that checks if the FIFO has any contents, and then sends it via serial,Speed wise, you would be using the serial buffer or similar, just so you dont have to wait for things to happen. normal canbus is up to 250 kbps (assuming your recovering the identifier) so you should plan your serial speeds to be close to that, equally modern vehicles can be 500Kbps, with some very new european cars starting to have 1mbps CAN

author: Chriss
author_rank: Frequent Contributor
timestamp: November 02, 2019, 01:56:48 am
content: Thanks for the fast replay, I summarized what I have at the moment at home and how can I build something for start.I have:- vehicle diag and flashing tools with CAN-bus communication ( this would be my CAN transmitter )- Atmel uC's Attiny2313, ATMEGA16 ( this could maybe be the receiver side or I put a PC as a receiver )- the CJMCU-230 Bus Transceiver ( this would be my transreceiver between the uC or PC and diag/flasher tool )Is it possible to build the connection between my transmitter and PC like this:The PC RS232 port to the Rx/Tx line to the CJMCU-230 Bus Transceiver Tx/Rx lineThe CAN-L/CAN-H line from the CJMCU-230 Bus Transceiver to the CAN-L/CAN-H on my diag/flasher toolAnd now to write the software on the PC so the PC receives directly the CAN messages from the transmitter?Will this concept of my idea about the hardware setup working on a primitive level of PC to CAN network interface?I could also check with my logic analyzer the package from my transmitter so I could be sure does my PC software really works?Earlier I made many projects interacting between PC and uC's through USB and RS232 and I'm familiar with that but CAN issomething new for me...I'm trying to build up something from what I already have...

author: Rerouter
author_rank: Super Contributor
timestamp: November 02, 2019, 02:48:15 am
content: the CJMCU-230 (what appears to actually be a SN65HVD230 from Texas Instruments) does not spit out normal serial data,Rather it acts like a level converter, between the differential canbus, and a microcontroller CAN peripheral,so for normal car 11 bit canbus, a PC serial port would not know what to do with this info, it does not fall within the boundaries of how you can configure a serial port.You need something else in between, a common choice is something like a ELM327, this is what is already used as a drop in CAN-Serial option in most diagnostic tools, downside being it is a little slow if you want to capture every last packet.

author: jhpadjustable
author_rank: Frequent Contributor
timestamp: November 02, 2019, 04:40:17 am
content: Be aware that CAN defines only a data link layer. Physical layers, bit rates, and addressing schemes can vary widely between model years, manufacturers, weight classes, and fuel types.

author: Rerouter
author_rank: Super Contributor
timestamp: November 02, 2019, 06:20:19 am
content: not really... can for vehicles falls under 2 normal standards, 11 bit and 29 bit, there is CANFD, but its barely used.Bit rates, its going to be 250K for almost everything, 500K for a very small number, and I have only seen 1, 1M vehicle so far,for 11 bit, there is a standard for diagnostics, but other than that it is up to the manufacturer to specify what address is for what datafor 29 bit, almost everything follows the standard, It tends to be that earth moving crams most of its data in the "Proprietary" address range.Cars tend to be 11 bit, Trucks/ Buses / Heavy equipment is generally 29 bit. and they are not commonly mixed on the same bus (Mercedes had fever dreams with some of there buses a decade ago)for 29 bit, your looking for an excel file online called "J1939DA", for 11 bit, you would be looking at resources like Torque forum, as many different car codes are documented there.For flashing tools, it gets harder, this tends to be common among manufacturers of a given ECU or cluster, rather than the car manufacturer, some are stupidly simple, some need you to use the vin as an encrytion key of sorts, once in the mode it really is keys of the kingdom level control and you can do proper damage.

author: Chriss
author_rank: Frequent Contributor
timestamp: November 02, 2019, 11:13:28 am
content: Thank you guys for the very very very valuable infos, I appreciate this and it is much clearer to me now...Here is a picture I made what I wish to achieve.I would not use the gadget/tool whatever we call my crap on a car, just between two PC and a diag/flashing tool connected to.On the pc where the diag/flashing tool is connected I would send a CAN msg through the correspondent software of the connected tool something like "Read ECU ID" or "Scan for connected modules" or similar, depend of the ability of the tool.I assume, if I connect a diag tool and select a car model which ECU is with CAN bus and I press the button "Read ECU ID",on the CAN pin of the diag tool, there would be a correct CAN msg what I could catch on the Notebook with my software.My software on the notebook should just be sure does the msg which was captured are a correct CAN msg or not, don't care aboutthe msg in the CAN data package.I know, this will lead at the end in an error lik "Can not connect to ECU" or similar, but I don't care about that now.I also realized from a post above, does I can't direct read the Rx Tx line through a COM port cos the standar of the bitsbetween COM and CAN protocols. But I assume that is also possible to override, therefore I wrote in my first post, maybe I wouldintegrate a uC.But let's think keep simple as possible, for now during I'm entering to this world of CAN.Other way to ask:How can I check does the CAN bus is reacting correctly let's say on my vehicle diag tool? but without a real vehicle or ecu...Many times I can't test it on a vehicle, ecu or whatever cos I don't have one beside.Something like on RS232 port on a PC where I can interconnect the Rx & Tx lines and send a character out through an app likeHyperterminal?What's the cas on CAN bus with the "Echoing"?

author: max_torque
author_rank: Super Contributor
timestamp: November 02, 2019, 02:31:52 pm
content: So assuming your project is not about developing an CAN interface, and actually about being simply able to interface a PC to a CAN bus, why would you waste your time developing your own hardware?There are plenty of reasonable cost CAN to USB interfaces avaliable, and pretty much all of them coe with a matching APi eaning you can very quickly get the CAN message data into your PC's software environment. For example, Peak have their PCANusb dongle, which has an API and working examples of OC sofrware in pretty much all languages (python, C, C++, .Net etc etc)There is also a helpful forum too:https://www.peak-system.com/forum/You might look at the cost of such an existing dongle and think "yes, but i can do it much cheaper" but actually, when you factor in your time, and the actual aim of your project, you can't..............If you really want to go low cost, then an arduino wih existing CAN shield is already available hardware with matching software:http://wiki.seeedstudio.com/CAN-BUS_Shield_V2.0/but of course you will be at the mercy of who ever wrote the librarries, and in the Arduino world the quality of such libraries can be rather, er, variableI'd buy the decent tool and concentrate on the project aims!

author: max_torque
author_rank: Super Contributor
timestamp: November 02, 2019, 02:34:16 pm
content: BTW, it sounds like you are effectively trying to reverse engineer the CAN reflash protocol, and if so, the best thing to do is to buy a cheap s/h ecu and power it up on the bench from a power supply, so you an play around trying to gets coms working to that unit without either having to be sat in an actual car, and without other risks (such as bricking the ecu, or setting off the airbags un-expectidly......)

author: HwAoRrDk
author_rank: Super Contributor
timestamp: November 02, 2019, 03:38:05 pm
content: Yes, if all OP really wants to do is capture and inspect the CAN bus traffic generated by diagnostic software or standalone tool, then it's not worth going to the effort of messing around building custom hardware.OP mentions that they have a logic analyser. Does it have capability to do CAN decoding? If so, just hook one of its channels up to the CAN_L line and capture the traffic.I also second the very cheap option of using an ELM327-based USB/Bluetooth OBD adapter (even the $5 clones). No special software needed, just use a serial terminal to connect to the COM port and issue AT commands to have the ELM327 monitor CAN bus messages. You can even use one outside of a vehicle by just supplying 12V power to the appropriate pins, plus the CAN lines from the diagnostic tool. But as Rerouter said, can be difficult trying to log a busy bus with limited serial bandwidth between the ELM327 and PC - it won't be able to spit out the messages it captures fast enough.P.S. do not try to directly connect a PC's RS232 port to a CAN transceiver chip. RS232 signal voltage levels can be up to +/-15V, although I would expect a PC's RS232 port to be somewhere between +/-5V and +/-12V. Because of the voltage levels it will potentially damage the transceiver, as they commonly expect signal levels on the RX/TX pins of 0-5V. And, in fact, those CJMCU-230 breakout boards using an SN65HVD230 operate at 3.3V, so are not suitable full-stop if the RS232 signal levels are at any voltage above that.

author: Jeroen3
author_rank: Super Contributor
timestamp: November 02, 2019, 03:51:07 pm
content: Why does everyone want to reinvent this!Just go buy a CAN to USB interface that is supported inBusmaster.https://github.com/rbei-etas/busmaster/wiki/Hardware-supportKvaser is easy to use with their driver, and you can run multiple programs on one interface via their shared driver.I think PEAK made something similar via some server type app recently as well.

author: Chriss
author_rank: Frequent Contributor
timestamp: November 02, 2019, 06:55:17 pm
content: The major task of my project is to learn something, something new to me, about the CAN-bus.I don't wish to reversengeneer anything, don't want to make any commercial device or similar.I wish to setup a CAN "playground" for me on the bench and study it, programming it, playing with it during the winter time.As I described before, I wish to build up the stuff from what I have in my pocket right now, but ok, I learned something new again.My transreceiver is not a version which I should have. Maybe a better option will be a MCP2515 CAN Bus Module TJA1050 receiver.I'm familiar to write programs for uC ( my optionis AVR ) in C language.I played around with Arduino and have one Arduino UNO R3 boar laying around, but I don't like Arduino too much.I'm not an Arduino hater.Yes, I have a logic analyzer where I can easy hook up the CAN bus ad select CAN bus package analyzing option in the logic analyzer,but it is, again, more fun to make something from what I can learn.Anyway, what hardware would I need to setup my project for CAN similarly described on my pic?Even if you say I should go to arduino, ok I will do it with it.Many thanks for every words, I really appreciate it.My best regards.

author: Jeroen3
author_rank: Super Contributor
timestamp: November 02, 2019, 07:53:36 pm
content: To get your own interface you need the layer 1 tranceiver (eg: MCP2551) to get from a differential shared line to single ended split lines TX and RX.These go into a CAN controller which is often embedded in the microcontroller. It is difficult to do in software due to the CRC and arbitration timings, don't try.Then from your CAN controller you can send and receive messages by software. Often trough some form of "mailboxes" of fifo architecture. as well as reading the error counters and such.This is your layer 2.There are no more layers in "CAN bus" strictly ISO 11898.Further layers are J1939, CANopen or DeviceNET. These give meaning to the data inside the CAN frame. But a vendor can also use something entirely proprietary!Even when using one of above protocols the actual implementation will vary widely between vendors and devices.To get started with CAN bus just get any ST-NUCLEO, I think all ST chips have a CAN controller. You only need to add the transceiver.Or an MBED, many also include a CAN controller.Off-chip CAN controller like the MCP2515 might work as well, but is software-architecturally more difficult to get running. And quickly limited in messages per seconds due to the SPI link.

author: jhpadjustable
author_rank: Frequent Contributor
timestamp: November 02, 2019, 10:35:01 pm
content: Quote from: Jeroen3 on November 02, 2019, 07:53:36 pmFor the lab, this might be good enough.

author: Chriss
author_rank: Frequent Contributor
timestamp: November 04, 2019, 02:00:27 am
content: Ok.I will give a try to read the CAN bus of one of my ecu flaher.I never deal with CAN stuff on this level before.What is the proper way to connect the logic analyzer to the flasher tool?Do I need a 120 Ohm resistor first between CAN-H & CAN-L?Do I have to connect CAN-H & CAN-L to the logic analyanalyzer?Thank you.

author: Jeroen3
author_rank: Super Contributor
timestamp: November 04, 2019, 06:37:44 am
content: You are not going to receive anything useful is you do not haveat least twoCAN controllers on the bus.Connecting only a logic analyzer will not work.CAN will error not-acknowledge.Yes, at least one 120 ohm is required for operation on your desk. In production each end of the bus requires the 120 ohm resistor.

author: Chriss
author_rank: Frequent Contributor
timestamp: November 04, 2019, 10:30:21 am
content: Ok.Here is my new idea how to setup a bench CAN simulator, will this work or not?I have an Arduino Uno R3If I buy this shield:https://s.click.aliexpress.com/e/4qkMTal2I found this ecu simulator on github:https://github.com/AppSaloon/ecu-can-bus-simulator/blob/master/README.mdSo, if I would all this stich together and connect lets say one of my diag tool like VCDS or maybe a better option the ELM327, could I use my Loguc analyzer and play with this gadget? Or I need something more?Thanks.

author: Jeroen3
author_rank: Super Contributor
timestamp: November 04, 2019, 12:55:28 pm
content: ATMega based Arduino do not support this. You need aCAN controller.Available inSTM32F042K6,STM32F446,LPC1768, AT90CAN32, PIC18F458 and many other. But not ATMega.CPU -> CAN Controller ->GPIO-> CAN Transceiver -> CAN Bus H/L.Only the underlined bit is suitable for your logic analyzer. But a logic analyzer of little to no value with CAN bus, the signals are never wrong. The data is, and that is only visible in more complex software, likeBusmaster.

author: zzattack
author_rank: Regular Contributor
timestamp: November 04, 2019, 02:01:48 pm
content: The shield he linked includes a CAN controller (2515).But to reiterate what's been said a few times now: pick a microcontroller with built-in CAN controller. There's dozens and they're all more capable and easier to work with than a glue solution involving an SPI-CAN 'bridge'.Logic analyser to look at CAN traffic is possible, but you'll always need at least 2 non-passive nodes on your bus.

author: DBecker
author_rank: Frequent Contributor
timestamp: November 04, 2019, 04:26:25 pm
content: Yes, that 2515-based CAN shield will be sufficient. And there are a bunch of sketches that will respond to OBD2 PIDs. I wrote similar code almost a decade ago, before switching to the STM32F103.As everyone else says, but is easy to miss, you need both terminating resistors and a peer on the CAN bus to communicate. You cannot communicate into an empty bus. The transceiver needs a little bit of a load. The controller expects to see at least one peer pull the bus low just after it has finished transmitting (typically all active peers will do that).One termination resistor is typically mounted on the ECU, with a second on the module at the far end of the bus. A single resistor works fine for a short bus. CAN bus is so robust you rarely need to be concerned about having one or two (or even three), but you do need at least one.Even if you have better tools, buy a bluetooth ELM327 OBD2 reader. They are the cheapest CAN diagnostic tool out there, and have a huge amount of support. For $5 you can buy one and a OBD2 connector (or an extension cable). It takes only a few minutes to learn the 'AT' commands to manually send and receive individual CAN packets. Their down-side is a few bugs and dropping with a burst of back-to-back packets, but you'll be pretty far along before that's a limitation.Another inexpensive diagnostic tool is Sigrok with one of the 8 channel USB logic analyzers. CANL typically has enough voltage swing to look like a single-ended logic signal, and Sigrok can decode it.

author: Chriss
author_rank: Frequent Contributor
timestamp: November 04, 2019, 06:41:30 pm
content: QuoteIf I understand your writing correct, the mentioned CAN Bus shield + my Uno R3 + the sketch what I found would be enough to make a lets say fake ecu who will respond to my ELM327 diag tool if I use the AT commands?QuoteLets say, I have a car ecu on bench and connected the ELM327 to the ecu through the CAN bus.I can read/clear DTC's etc...Which one of my two devices are the peer?QuoteI have a good quality ELM327 OBD2 reader tool with BT. So, I don't have to buy one.I was playing with my ELM327 diag tool on the car through HyperTerminal, sent and analyzed the RPM, Coolant Temp etc.It was fun and worked well. But it bother me to sitting in the car for hours, drain down the battery etc.It would be most convenient to have something on the bench, especial in the winter time.QuoteI have an original Saleae logic analyzer and a clone one too, both of them are working great but I never tested them on CAN bus.I heard/read about the Sigrok but did not tried for now, I think the PulsView app from Sigrok is compatible with both of my tools.