BrianHumlicek / J2534-Sharp

A .Net interface for J2534 vehicle communications.
Other
43 stars 14 forks source link

J2534-Sharp

J2534-Sharp handles all the details of operating with unmanaged SAE J2534 spec library and lets you deal with the important stuff.

Available on NuGet! NuGet Gallery: J2534-Sharp

Features

TODO's

This project is very active and still somewhat fluid. Breaking changes may occur and I wont apologize for them.

Feedback

I want to hear from you if you have ideas or thoughts about this library. Anything from features to critiques are welcome. Also, I would love to know what kinds of things its being used for!

Usage Paradigm

Traditional usage

The traditional usage will use explicit filter definition and using disposables within using's'.

using System;
using System.Linq;
using SAE.J2534;

namespace J5234Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            MessageFilter FlowControlFilter = new MessageFilter()
            {
                FilterType = Filter.FLOW_CONTROL_FILTER,
                Mask = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF },
                Pattern = new byte[] { 0x00, 0x00, 0x07, 0xE8 },
                FlowControl = new byte[] { 0x00, 0x00, 0x07, 0xE0 }
            };

            string DllFileName = APIFactory.GetAPIinfo().First().Filename;

            using (API API = APIFactory.GetAPI(DllFileName))
            using (Device Device = API.GetDevice())
            using (Channel Channel = Device.GetChannel(Protocol.ISO15765, Baud.ISO15765, ConnectFlag.NONE))
            {
                Channel.StartMsgFilter(FlowControlFilter);
                Console.WriteLine($"Voltage is {Channel.MeasureBatteryVoltage() / 1000}");
                Channel.SendMessage(new byte[] { 0x00, 0x00, 0x07, 0xE0, 0x01, 0x00 });
                GetMessageResults Response = Channel.GetMessage();
            }
        }
    }
}

Simplified usage

The simplified usage take advantage of built in 'templates' for creating the filters

using System;
using System.Linq;
using SAE.J2534;

namespace J5234Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            string DllFileName = APIFactory.GetAPIinfo().First().Filename;

            using (API API = APIFactory.GetAPI(DllFileName))
            using (Device Device = API.GetDevice())
            using (Channel Channel = Device.GetChannel(Protocol.ISO15765, Baud.ISO15765, ConnectFlag.NONE))
            {
                Channel.StartMsgFilter(new MessageFilter(UserFilterType.STANDARDISO15765, new byte[] { 0x00, 0x00, 0x07, 0xE0}));
                Console.WriteLine($"Voltage is {Channel.MeasureBatteryVoltage() / 1000}");
                Channel.SendMessage(new byte[] { 0x00, 0x00, 0x07, 0xE0, 0x01, 0x00 });
                GetMessageResults Response = Channel.GetMessage();
            }
        }
    }
}

Alternate usage of the APIFactory

Alternately, the API factory can be instantiated as an instance, and when disposed, will dispose all children with it. This negates the need for explicit using's' except for the initial one for the APIFactory. NOTE: The APIFactory instance is only used to facilitate the disposal, and the instance does not need to be passed around.

using System;
using System.Linq;
using SAE.J2534;

namespace J5234Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var factory = new APIFactory())
            {
                Run();
            }
        }
        static void Run()
        {
            string DllFileName = APIFactory.GetAPIinfo().First().Filename;

            Channel Channel = APIFactory.GetAPI(DllFileName).GetDevice().GetChannel(Protocol.ISO15765, Baud.ISO15765, ConnectFlag.NONE);

            Channel.StartMsgFilter(new MessageFilter(UserFilterType.STANDARDISO15765, new byte[] { 0x00, 0x00, 0x07, 0xE0 }));
            Console.WriteLine($"Voltage is {Channel.MeasureBatteryVoltage() / 1000}");
            Channel.SendMessage(new byte[] { 0x00, 0x00, 0x07, 0xE0, 0x01, 0x00 });
            GetMessageResults Response = Channel.GetMessage();
        }
    }
}