Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using jnsoft.Comm.CAN;
- using System;
- using System.Linq;
- using System.Windows.Forms;
- namespace jnsoft.DBC.Examples
- {
- /// <summary>
- /// ASAP2ibrary DBC handling demonstration.
- ///
- /// - Loading an DBC file
- /// - Register to receive CAN frames on a kvaser (as an example) CAN hardware.
- /// - Print a first valid signal while receiving CAN frames.
- ///
- /// Usage: DBCExample DBCFile.dbc
- /// </summary>
- class Program
- {
- static DBCFile mDBCFile;
- static int Main(string[] args)
- {
- var orgColor = Console.ForegroundColor;
- try
- {
- Console.ForegroundColor = ConsoleColor.White;
- if (args.Length != 1)
- { // no args -> present usage
- Console.WriteLine($"{Application.ProductName}({Application.ProductVersion})");
- Console.WriteLine("Read a DBC file, interprete values received from the CAN Bus and print them...");
- Console.WriteLine("Required input: a DBC file");
- Console.WriteLine($"Usage: {Application.ProductName} DBCFile.dbc");
- return -1;
- }
- // Open the DBC file
- mDBCFile = DBCFile.open(args[0]);
- // Create a CAN provider on Port 0 with 1 Mit/s and
- ICANProvider canProvider = new Comm.CAN.Kvaser.KvaserCAN();
- canProvider.open(new CANConfiguration(0, CANBaudrate._1MBit));
- // register to receive any CAN frame from the CAN BUS.
- canProvider.registerClient((o, e) => { mDBCFile.onCANReceived(e.Frame); });
- // Get a DBC source with at least one valid CAN message defined
- var source = mDBCFile.Sources.Values.FirstOrDefault(s => s.Messages.Count > 0 && s.Messages.FirstOrDefault(x => x.IsValid) != null);
- // Get a message defined with a valid CAN Id and at least one signal defined
- var message = source.Messages.FirstOrDefault(m => m.IsValid && m.Signals.Count > 0);
- // Get a signal with the message
- var signal = message?.Signals.FirstOrDefault();
- if (signal != null)
- {
- // signal is valid
- Console.WriteLine($"Press ESC to quit, any other key to refresh current values...");
- Console.ForegroundColor = ConsoleColor.Green;
- int count = 0;
- do
- { // refresh signal data
- var strId = CANProvider.toCANIDString(message.ID);
- var time = message.LastUpdate;
- var strValue = signal.toStringValue();
- Console.Write($"\rRequest {count++}: Time:{time:HH:mm:ss} Source:{source} Message:{message.Name}({strId}) Signal:{signal.Name} Value={strValue}");
- } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
- return 0;
- }
- }
- catch (Exception ex)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine($"Something failed, file or signal not found or unsupported!\nDetails:\n{ex.Message}");
- }
- finally { Console.ForegroundColor = orgColor; }
- return -1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement