Structure of a CAN Frame
The CAN Basics Training Course provides a practical approach to understanding how CAN works. By giving real world examples, common practices, and an in-depth look at DBC files, Bryan Hennessy gives a real-world walkthrough of CAN.
Presentation by Bryan Hennessy. Recorded as part of a โliveโ training session in January 2019.
Video Transcript:
Bryan Hennessy: [00:00:01] So the structure of a CAN frame. This is moving from the physical layer to the data link layer. We now know and how very comfortable with getting 1โs and 0โs across the network reliably between devices and how that wiring is hooked up and how itโs all done.
Now weโre going to talk about the frame, how does device A know how to package those 1โs and 0โs and how to understand what they mean when device B sends them a whole block 1โs and 0โs. Itโs beyond 1โs and 0โs at this point. Weโre into the CAN frame or whatโs called the data link layer.
This is a diagram stolen from a Kvaser training on the Kvaser website. This shows what we call Classic CAN 2.0A and 2.0B. Iโm not going to get into a lot of the history on it but I will say 2.0A is simply a [00:01:00] CAN frame with an 11-bit identifier and 2.0B is a CAN frame with a 29-bit identifier. A lot of standard J1939 almost always have a 29-bit identifier. CANopen always uses an 11-bit identifier. They define and break that identifier up differently depending on prioritization and source of drafts and things like that that we will get into to answer your question earlier.
So, after the identifier, thereโs some other bits in here that Iโm not going to get into real specifically because theyโre involved with history and other stuff and you donโt really need to understand them to understand the basics of CAN. In a more advanced course we could talk about every single bit in the CAN frame, but some of these single bits Iโm not going to get into right now. So Iโll talk about, in general, the arbitration field and then Iโll talk a little bit about how the arbitration field may be broken down by J1939 [00:02:00] just as an example.
Again, itโs different CANopen. Iโve been recently studying CANopen and I could talk about the arbitration field and CANopen as well. I havenโt prepared anything on that. Iโll talk a lot about the data field, a little bit about the control field as well and tell you whatโs in there, which is basically the DLC, the data length code. Talk about the data field. Then Iโll mention the CRC without getting into great detail on how a CRC works. Iโll tell you at least what it does. Then thereโs some closeout stuff. Thereโs an acknowledge bit that we can understand, itโs pretty easy to understand. Then thereโs an end of frame sequence. Then youโre done with your CAN frame. Thatโs it.
So, the one thing to understand about CAN frames in general is, a lot of times a CAN bus will be floating, will be just nobody transmitting anything. Youโll be out here somewhere. As weโve seen one or more devices could decide, โHey, I got a CAN frame to transmit. Iโm going to take control of this bus and send it.โ The first thing they send, as I said earlier, is the start of frame bit. [00:03:01] So if you go back to my bit-wise arbitration slide, thatโs where both device A and device B decided theyโre going to go from recessive to dominant with the start of frame bit and then they start sending the beginning bits of their identifier. If two devices or more are on the network, you have an arbitration situation where weโre arbitrating for the bus.
Weโre done with arbitration, and an important thing to know about arbitration is, when you get done with this arbitration field, by definition, by design locked in stone, thereโs only one device transmitting on the bus. One device has one arbitration when you get down with the arbitration field. If you still have two devices that think they want arbitration, you got a faulty bus. You got a problem. That doesnโt exist. It canโt happen because, at least, in J1939 every device is a different source address. Weโll get more into that in a little bit. But different standards have different ways of making sure that only one device has one arbitration by the time arbitration is over.
Then you get data CRC. [00:03:59] Weโll talk more about that in a minute.
Hereโs some CAN data. Makes sense? You know what that is, yes? Easy to read. Well, by the end of the day it will be. Youโll be able to take something out of that data stream and know precisely what it means because Iโll show you how to do that. Thatโs what I call taking the mystery out of CAN. This is a mystery. This is gobbledygook. Nobody knows what this is. I can tell you in great detail what every single bit in that pattern is and show you how to read it. Hopefully, Iโm going to do that in pretty short order.
So this is a grab of data I got from CANKing which is Kvaserโs free bus monitor. In our download section I can show you that, that all of you should, maybe, grab and get somewhat familiar with. But itโs a pretty simple tool. It just shows you whatโs on the canvas. It doesnโt care what the data is; it just shows it to you.
So what we have out here is the channel. [00:05:01] This is a single channel network. CANKing can read from more than one channel if you have more than one CAN bus going at the same time. But here weโre just using channel 0. This is identifier and in this case this is a 29-bit identifier. Now you might say we got one, two, three, four, five, six, seven, we got eight, so we got four bytes, so 4 x 8 is 32. So, well, no you got 32 bits, not 29. Well, no, we have 29 because this one hereโฆ listen, youโre only going to see it as a 0 or a 1. Thatโs the only way we can represent it. You donโt have a clean way in hexadecimal of representing 29 bits. So you got to just say, well, thatโs going to be a 0 or a 1. Thatโs only 1-bit and the rest of these are 4-bit characters. So thatโs 29 bits. So thatโs a 29-bit identifier.
This flag says that itโs an extended identifier. Itโs 29 bits as opposed to 11. [00:06:00] This is your DLC. This is your data length code. If you go back to the previous slide, this is transmitted, or Iโll show you on the next slide, this is transmitted in control field. Thatโs transmitted after arbitration. They need extra data. Depending on the standard, and again Iโll refer to J1939, you can have anywhere from zero to eight data bytes, eight is the maximum that you can have in here. So what are we going to do if we need to send a message more than eight? Well, thatโs an applications question, and thatโs going to be transport protocol.
This is a timestamp. This is never on the data bus. This is applied by CANKing depending on what time that particular frame comes in. This simply says that this frame was received by the Kvaser device as opposed to transmitted. So thatโs, in a nutshell, what everything on that whole diagram means, thereโs no mystery there. I will later, in the presentation, [00:07:01] show you how to identify and take apart any piece of this data. Iโll also show you exactly what this identifier means and how to take that apart depending on what application or what industry youโre in and what high-level protocol youโre in. Iโll show you based on J1939 mostly but I just donโt have time to go into each one of them differently. But youโll get the meaning. Youโll get the gist of it from J1939 as an example.
A lot of this training here, as I mentioned, is training by example because I canโt teach everything in two hours. Thereโs no way. But I can pick an example, I can explain generally how those works in different industries and then I can dive in and show you the very details in one particular example. Hopefully, you can apply that example and understand that enough to apply it to other areas.
So the whole idea of this is just to make you much more comfortable with the fact that hey, I can [00:08:00] look at this data that previously meant absolutely nothing, thatโs gobbledygook and I actually can understand how to trace that to something meaningful, all the way from the voltages on the wire to a meaningful signal is where weโre going today.
Hereโs a blow-up, again based on J1939, or actually NMEA 2000 I think this one is, Iโm not sure, but theyโre both pretty much the same. Yes, this is J1939 of the arbitration field. So bus arbitration is done here. This is the arbitration field. Then we get to the control field which I said has a couple bits, weโre going to ignore, and the data length code. Data length code just tells you how many data bytes you are going to have in that frame. So itโs zero to eight.
Without getting into what a parameter group number is or how it breaks down, I should say, into these because this gets pretty specific to J1939, Iโm just going to say this is the parameter group number. So we have start of frame, [00:09:00] we got priority. Priority is 3 bits. Every message in J1939 has a priority. Zero is the highest[edited] priority. We now know why because of bit-wise arbitration.
You got a recessive bit. Iโm not going to get into here. Then, basically, you got a parameter group number. A parameter group number is an identifier for the data thatโs going to follow. A particular parameter group number is going to say this parameter group number in J1939, youโre going to build a reference suspect that tells you exactly what the data is and how itโs broken down by knowing what this parameter group number is.
Following that, youโre going to get a source address. Thatโs whoโs transmitting the data. Every single frame has one and only one device transmitting it on the network and has one or more, possibly all of the devices, wanting or listening to that data. Now, as we know a network consists of two wires. [00:10:00] Any device sends a message on that network and every device hears that message. Canโt be otherwise. Thatโs just the fact. Thatโs one of the beauties about CAN for control systems, is everything on the network has access to all of the same information by nature, by the way itโs wired. We canโt change that. We donโt want to change that. Thatโs a beauty of CAN.
So, priority, parameter group number, source address. If two devices or more are transmitting messages of the same priority, arbitration continues. If two devices or more are transmitting messages with the same priority and the same parameter group number, we get all the way out to here and we still have two devices on the bus thinking that they want arbitration.
At least in J1939 and the protocols based on J1939, [00:11:01] every device has to have a unique source address. So in that case where everything else is the same, the device with the lowest source address is going to win arbitration. By the time we get here in time, only one device is going to be on the network transmitting. Weโre going to transmit a DLC code, all the receivers know what the parameter group number is, so they know what to look for in the data and theyโre going to read the data. So that is the data link layer. Thatโs the essence of the data link layer.