Author Topic: CAN-bus msg validate, how?  (Read 2802 times)

0 Members and 1 Guest are viewing this topic.

Offline ChrissTopic starter

  • Frequent Contributor
  • **
  • Posts: 534
  • Country: 00
CAN-bus msg validate, how?
« on: November 01, 2019, 09:20:15 pm »
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 cars
over 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 really
appreciate 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 can
build for me a small CAN-playground for startup...  :-DD

Thank you.
My best regards.


« Last Edit: November 01, 2019, 09:26:22 pm by Chriss »
 

Offline zzattack

  • Regular Contributor
  • *
  • Posts: 136
  • Country: nl
Re: CAN-bus msg validate, how?
« Reply #1 on: November 01, 2019, 09:29:56 pm »
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.
 
The following users thanked this post: Chriss

Offline Syntax Error

  • Frequent Contributor
  • **
  • Posts: 584
  • Country: gb
Re: CAN-bus msg validate, how?
« Reply #2 on: November 01, 2019, 09:42:49 pm »
 
The following users thanked this post: Chriss

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4705
  • Country: au
  • Question Everything... Except This Statement
Re: CAN-bus msg validate, how?
« Reply #3 on: November 01, 2019, 09:44:04 pm »
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 for

Now 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
 
The following users thanked this post: Chriss

Offline ChrissTopic starter

  • Frequent Contributor
  • **
  • Posts: 534
  • Country: 00
Re: CAN-bus msg validate, how?
« Reply #4 on: November 02, 2019, 01:56:48 am »
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 line
The CAN-L/CAN-H line from the CJMCU-230 Bus Transceiver to the CAN-L/CAN-H on my diag/flasher tool
And 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 is
something new for me...

I'm trying to build up something from what I already have...
« Last Edit: November 02, 2019, 01:58:20 am by Chriss »
 

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4705
  • Country: au
  • Question Everything... Except This Statement
Re: CAN-bus msg validate, how?
« Reply #5 on: November 02, 2019, 02:48:15 am »
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.
 
The following users thanked this post: Chriss

Offline jhpadjustable

  • Frequent Contributor
  • **
  • Posts: 295
  • Country: us
  • Salt 'n' pepper beard
Re: CAN-bus msg validate, how?
« Reply #6 on: November 02, 2019, 04:40:17 am »
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.
"There are more things in heaven and earth, Arduino, than are dreamt of in your philosophy."
 
The following users thanked this post: Chriss

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4705
  • Country: au
  • Question Everything... Except This Statement
Re: CAN-bus msg validate, how?
« Reply #7 on: November 02, 2019, 06:20:19 am »
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 data
for 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.
 
The following users thanked this post: Chriss

Offline ChrissTopic starter

  • Frequent Contributor
  • **
  • Posts: 534
  • Country: 00
Re: CAN-bus msg validate, how?
« Reply #8 on: November 02, 2019, 11:13:28 am »
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 about
the 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 bits
between COM and CAN protocols. But I assume that is also possible to override, therefore I wrote in my first post, maybe I would
integrate 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 like
Hyperterminal?

What's the cas on CAN bus with the "Echoing"?

« Last Edit: November 02, 2019, 11:19:52 am by Chriss »
 

Offline max_torque

  • Super Contributor
  • ***
  • Posts: 1327
  • Country: gb
    • bitdynamics
Re: CAN-bus msg validate, how?
« Reply #9 on: November 02, 2019, 02:31:52 pm »
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, variable



I'd buy the decent tool and concentrate on the project aims!
 
The following users thanked this post: Chriss

Offline max_torque

  • Super Contributor
  • ***
  • Posts: 1327
  • Country: gb
    • bitdynamics
Re: CAN-bus msg validate, how?
« Reply #10 on: November 02, 2019, 02:34:16 pm »
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......)
 
The following users thanked this post: Chriss

Offline HwAoRrDk

  • Super Contributor
  • ***
  • Posts: 1607
  • Country: gb
Re: CAN-bus msg validate, how?
« Reply #11 on: November 02, 2019, 03:38:05 pm »
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.
 
The following users thanked this post: Chriss

Online Jeroen3

  • Super Contributor
  • ***
  • Posts: 4209
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: CAN-bus msg validate, how?
« Reply #12 on: November 02, 2019, 03:51:07 pm »
Why does everyone want to reinvent this!  :palm:

Just go buy a CAN to USB interface that is supported in Busmaster.
https://github.com/rbei-etas/busmaster/wiki/Hardware-support

Kvaser 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.
 

Offline ChrissTopic starter

  • Frequent Contributor
  • **
  • Posts: 534
  • Country: 00
Re: CAN-bus msg validate, how?
« Reply #13 on: November 02, 2019, 06:55:17 pm »
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.
 

Online Jeroen3

  • Super Contributor
  • ***
  • Posts: 4209
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: CAN-bus msg validate, how?
« Reply #14 on: November 02, 2019, 07:53:36 pm »
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.
 
The following users thanked this post: Chriss

Offline jhpadjustable

  • Frequent Contributor
  • **
  • Posts: 295
  • Country: us
  • Salt 'n' pepper beard
Re: CAN-bus msg validate, how?
« Reply #15 on: November 02, 2019, 10:35:01 pm »
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.
For the lab, this might be good enough.
"There are more things in heaven and earth, Arduino, than are dreamt of in your philosophy."
 
The following users thanked this post: Chriss

Offline ChrissTopic starter

  • Frequent Contributor
  • **
  • Posts: 534
  • Country: 00
Re: CAN-bus msg validate, how?
« Reply #16 on: November 04, 2019, 02:00:27 am »
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. 
 

Online Jeroen3

  • Super Contributor
  • ***
  • Posts: 4209
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: CAN-bus msg validate, how?
« Reply #17 on: November 04, 2019, 06:37:44 am »
You are not going to receive anything useful is you do not have at least two CAN 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.
« Last Edit: November 04, 2019, 06:39:21 am by Jeroen3 »
 
The following users thanked this post: Chriss

Offline ChrissTopic starter

  • Frequent Contributor
  • **
  • Posts: 534
  • Country: 00
Re: CAN-bus msg validate, how?
« Reply #18 on: November 04, 2019, 10:30:21 am »
Ok.
Here is my new idea how to setup a bench CAN simulator, will this work or not?

I have an Arduino Uno R3
If I buy this shield:
https://s.click.aliexpress.com/e/4qkMTal2
I found this ecu simulator on github:
https://github.com/AppSaloon/ecu-can-bus-simulator/blob/master/README.md

So, 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.
 

Online Jeroen3

  • Super Contributor
  • ***
  • Posts: 4209
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: CAN-bus msg validate, how?
« Reply #19 on: November 04, 2019, 12:55:28 pm »
ATMega based Arduino do not support this. You need a CAN controller.
Available in STM32F042K6, 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, like Busmaster.
« Last Edit: November 04, 2019, 12:57:42 pm by Jeroen3 »
 
The following users thanked this post: Chriss

Offline zzattack

  • Regular Contributor
  • *
  • Posts: 136
  • Country: nl
Re: CAN-bus msg validate, how?
« Reply #20 on: November 04, 2019, 02:01:48 pm »
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.
 
The following users thanked this post: Chriss

Offline DBecker

  • Frequent Contributor
  • **
  • Posts: 326
  • Country: us
Re: CAN-bus msg validate, how?
« Reply #21 on: November 04, 2019, 04:26:25 pm »
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.

 
The following users thanked this post: Chriss

Offline ChrissTopic starter

  • Frequent Contributor
  • **
  • Posts: 534
  • Country: 00
Re: CAN-bus msg validate, how?
« Reply #22 on: November 04, 2019, 06:41:30 pm »

Quote
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.
If 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?

Quote
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).
Lets 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?

Quote
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.
I 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.  :D

Quote
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.
I 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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf