From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I extract the timestamp from a CAN frame

Hi.

 

I am trying to extract the timestamp from a CAN frame. How can I do this? I am able to extract the relevant bits from the raw frame data (http://zone.ni.com/reference/en-XX/help/372841L-01/nixnet/rawframeformat/) but I'm not sure how to convert this into the current time. Any help appreciated. example VI would be a bonus!

 

Many Thanks

0 Kudos
Message 1 of 13
(5,508 Views)

Haven't tried it myself but have you tried using the Type Cast function with the type being a timestamp constant? Also are you using the NI-CAN API or the NI-XNET API?

 

http://zone.ni.com/reference/en-XX/help/371361N-01/glang/type_cast/

Matt J | National Instruments | CLA
0 Kudos
Message 2 of 13
(5,485 Views)

Hi Matt.

Thanks for the quick reply. 

I tried your suggestion to type cast the data using a timestamp constant, but the output doesnt update, even though I can see the relevant bits in the frame change. 

I am using the NI-XNET API.

 

Since the CAN frames gives the nomber of 100ns since 12:00 a.m. January 1 1601, Is there a way to add these together to come up with the current time?

 

Thanks

0 Kudos
Message 3 of 13
(5,480 Views)

Hi Rookie,

 

I got this VI from an old CAN example for the NI9853 module:

check.png

 

Did you actually read the help you linked in your own question?

It says this:

NI-XNET includes a pair of VIs to convert between this U64 timestamp format and the LabVIEW timestamp format. The NI-XNET VIs handle all time format and byte order aspects. For more information, refer to the NI-XNET examples for logfile access.

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 13
(5,460 Views)

Why are you using RAW and not CAN in the frame? 

0 Kudos
Message 5 of 13
(5,442 Views)

If you are using the NI-CAN toolkit the CAN Timestamp is part every data packet and extracting it is trivial

CanCapture.PNG

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 6 of 13
(5,422 Views)

The ordering of the bytes in the payload are all mixed up, so its easier to work with raw to reorder the bytes into the correct order. The suppliers of the equipment generating the CAN signals may have done this to make it more difficult to 'hack' into. They haven't provided me with the database file either, so working with raw seemed like an easier option. 

 

It is actually quite easy to do using case structure, with the different cases determined using the CAN Arbitration ID.

0 Kudos
Message 7 of 13
(5,421 Views)

Unfortunately, I dont have the NI-CAN toolkit 😞

0 Kudos
Message 8 of 13
(5,417 Views)

Thanks GerdW.

 

I'll have a look at this when I'm in the lab next.

0 Kudos
Message 9 of 13
(5,415 Views)

@rookie2017 wrote:

 

It is actually quite easy to do using case structure, with the different cases determined using the CAN Arbitration ID.


I think a for loop (for all frames) and an unbundle by name (unbundle the ID) is easier.  I've honestly never used the raw format except for logging.  There are a few shipped examples on how to flatten and unflatten a frame to an array of bytes.  Look for Replay CAN Frames from TDMS in the example finder to see a (somewhat inefficent) way of converting a U64 time to LabVIEW timestamp.  The main VI that does this is found here: \vi.lib\xnet\xnet.llb\_XNET Convert Time U64 to LV.vi

0 Kudos
Message 10 of 13
(5,411 Views)