Advertisement
alesi2000

DBCExample

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