alesi2000

DBCExample

Jan 27th, 2019 (edited)
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. using jnsoft.Helpers;
  2. using jnsoft.Comm.CAN;
  3.  
  4. namespace jnsoft.DBC.Examples;
  5.  
  6. /// <summary>
  7. /// ASAP2ibrary DBC handling demonstration.
  8. ///
  9. /// - Loading an DBC file
  10. /// - Register to receive CAN frames on a kvaser (as an example) CAN hardware.
  11. /// - Print a first valid signal while receiving CAN frames.
  12. ///
  13. /// Usage: DBCExample DBCFile.dbc
  14. /// </summary>
  15. class Program
  16. {
  17.   static int Main(string[] args)
  18.   {
  19.     var orgColor = Console.ForegroundColor;
  20.     try
  21.     {
  22.       Console.ForegroundColor = ConsoleColor.White;
  23.       if (args.Length != 1)
  24.       { // no args -> present usage
  25.         var appName = Extensions.AppName;
  26.         Console.WriteLine($"{appName}({Extensions.AppVersion})");
  27.         Console.WriteLine("Read a DBC file, interprete values received from the CAN Bus and print them...");
  28.         Console.WriteLine("Required input: a DBC file");
  29.         Console.WriteLine($"Usage: {appName} DBCFile.dbc");
  30.         return -1;
  31.       }
  32.  
  33.       // Open the DBC file
  34.       var dbcFile = DBCFile.open(args[0]);
  35.  
  36.       // Create a CAN provider on Port 0 with 1 Mit/s and
  37.       var canProvider = new Comm.CAN.Kvaser.KvaserCAN();
  38.       canProvider.open(new CANConfiguration(0, CANBaudrate._1MBit));
  39.  
  40.       // register to receive any CAN frame from the CAN BUS.
  41.       canProvider.registerClient((o, e) => { dbcFile.onCANReceived(e.Frame, out var msg); });
  42.  
  43.       // Get a DBC source with at least one valid CAN message defined
  44.       var source = dbcFile.Sources.Values.FirstOrDefault(s => s.Messages.Count > 0 && s.Messages.FirstOrDefault(x => x.IsValid) != null);
  45.  
  46.       // Get a message defined with a valid CAN Id and at least one signal defined
  47.       var message = source.Messages.FirstOrDefault(m => m.IsValid && m.Signals.Count > 0);
  48.  
  49.       // Get a signal with the message
  50.       var signal = message?.Signals.FirstOrDefault();
  51.  
  52.       if (signal != null)
  53.       {
  54.         // signal is valid
  55.         Console.WriteLine($"Press ESC to quit, any other key to refresh current values...");
  56.         Console.ForegroundColor = ConsoleColor.Green;
  57.  
  58.         int count = 0;
  59.         do
  60.         { // refresh signal data
  61.           var strId = CANProvider.toCANIDString(message.ID);
  62.           var time = message.LastUpdate;
  63.           var strValue = signal.toStringValue();
  64.           Console.Write($"\rRequest {count++}: Time:{time:HH:mm:ss} Source:{source} Message:{message.Name}({strId}) Signal:{signal.Name} Value={strValue}");
  65.         } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
  66.  
  67.         return 0;
  68.       }
  69.     }
  70.     catch (Exception ex)
  71.     {
  72.       Console.ForegroundColor = ConsoleColor.Red;
  73.       Console.WriteLine($"Something failed, file or signal not found or unsupported!\nDetails:\n{ex.Message}");
  74.     }
  75.     finally { Console.ForegroundColor = orgColor; }
  76.     return -1;
  77.   }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment