Files
BmsMonitor/BmsMonitor.CanProbe/Program.cs
T

76 lines
3.1 KiB
C#

using System.Runtime.InteropServices;
using BmsMonitor.App.Can;
internal static class Program
{
[StructLayout(LayoutKind.Sequential)]
private struct InitConfig
{
public uint AccCode;
public uint AccMask;
public uint Reserved;
public byte Filter;
public byte Timing0;
public byte Timing1;
public byte Mode;
}
[DllImport("ControlCAN.dll", CallingConvention = CallingConvention.StdCall)]
private static extern uint VCI_OpenDevice(uint deviceType, uint deviceIndex, uint reserved);
[DllImport("ControlCAN.dll", CallingConvention = CallingConvention.StdCall)]
private static extern uint VCI_CloseDevice(uint deviceType, uint deviceIndex);
[DllImport("ControlCAN.dll", CallingConvention = CallingConvention.StdCall)]
private static extern uint VCI_InitCAN(uint deviceType, uint deviceIndex, uint channel, ref InitConfig config);
[DllImport("ControlCAN.dll", CallingConvention = CallingConvention.StdCall)]
private static extern uint VCI_StartCAN(uint deviceType, uint deviceIndex, uint channel);
[DllImport("ControlCAN.dll", CallingConvention = CallingConvention.StdCall)]
private static extern uint VCI_ResetCAN(uint deviceType, uint deviceIndex, uint channel);
private static int Main()
{
Console.WriteLine("Testing the production receiver...");
using (var receiver = new ControlCanReceiver())
{
var frames = 0;
var ids = new Dictionary<uint, int>();
receiver.FrameReceived += (_, frame) =>
{
Interlocked.Increment(ref frames);
lock (ids) ids[frame.Id] = ids.GetValueOrDefault(frame.Id) + 1;
};
receiver.Open(0, 0);
Console.WriteLine("ControlCanReceiver CAN1: Open/Init/Start=OK");
Thread.Sleep(10_000);
Console.WriteLine($"Capture10s: Frames={frames}, IDs={string.Join(", ", ids.OrderBy(x => x.Key).Select(x => $"0x{x.Key:X}({x.Value})"))}");
receiver.Close();
}
var found = false;
foreach (var type in new uint[] { 4, 3, 20, 21 })
foreach (var index in new uint[] { 0, 1, 2, 3 })
{
var result = VCI_OpenDevice(type, index, 0);
Console.WriteLine($"DeviceType={type}, DeviceIndex={index}: Open={result}");
if (result != 1) continue;
found = true;
if (type == 4 && index == 0)
{
var config = new InitConfig { AccMask = 0xFFFFFFFF, Filter = 1, Timing0 = 0x01, Timing1 = 0x1C, Mode = 1 };
foreach (var channel in new uint[] { 0, 1 })
{
var init = VCI_InitCAN(type, index, channel, ref config);
var start = init == 1 ? VCI_StartCAN(type, index, channel) : 0;
Console.WriteLine($" CAN{channel + 1}: Init250KListen={init}, Start={start}");
if (start == 1) VCI_ResetCAN(type, index, channel);
}
}
Console.WriteLine($" Close={VCI_CloseDevice(type, index)}");
}
return found ? 0 : 2;
}
}