Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: Teensy 4.1 CAN logger

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    3

    Teensy 4.1 CAN logger

    Dear forum members,

    I have busted my butt while working on my CAN logger project. My main task is to capture all messages transmitted over CAN and not to lose one. This is crucial. More crucial is the need to save logged CAN frames into an SD card. I have tried few techniques of fulfilling this task, but this is the point where the lack of programming experience hits hard. The main problem is that I loose many CAN frames after a while of logging. I don't know how to implement writing data to SD card by 512 byte blocks. Could this increase speed? And what happens with CAN bus monitoring during data transferring to SD? Should I use multitasking here?
    I would appreciate if my humble code would be overviewed by someone more experienced than I am.
    Source code attached.

    CAN_logger_internal_SD.ino

  2. #2
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    4,095
    you shouldn't loose frames unless you're polling with slow code or some calls are blocking (SD?). You could create 2 512 byte buffers and when one is full, write it but keep logging to the free buffer till thats full, then cycle again. You could also setup a circular queue of 1024 bytes and if it fills to more than 512 bytes dequeue the 512 into a buffer and write it to SD, meanwhile the circular queue is still adding bytes from the log.

  3. #3
    Junior Member
    Join Date
    Sep 2021
    Posts
    3
    Thank you for reply.
    This is what I already thought about, but both of cases you mentioned sound like multitasking...? If so, how to implement it? Where could I get more info of multitasking syntax and other tips?

    And another thing - is there a certain pattern to follow in order to write the 512 byte block into an SD? Or just File.printf() will do the trick? Because currently my code calls printf() function to every byte. Should I make my own array of characters with the size of [512] and load it into an SD with File.printf(*char[512])?

    Quite unclear for now, but I am learning

  4. #4
    Senior Member
    Join Date
    Aug 2018
    Location
    Brisbane, Australia
    Posts
    185
    try calling size_t write(const void *buf, size_t size) instead of printing each character

  5. #5
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    4,095
    yes better to write it as one huge block than individual bytes

  6. #6
    Junior Member
    Join Date
    Sep 2021
    Posts
    3
    Ok guys thanks for advice, will try it out and report the results. But my concern is what happens with CAN bus monitoring while I write data to an SD card... ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •