Initial commit: BMS CAN monitor
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0-windows</TargetFramework>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BmsMonitor.App\BmsMonitor.App.csproj" />
|
||||
<Content Include="..\BmsMonitor.App\Native\x86\ControlCAN.dll">
|
||||
<Link>ControlCAN.dll</Link>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="..\BmsMonitor.App\Native\x86\kerneldlls\usbcan.dll">
|
||||
<Link>kerneldlls\usbcan.dll</Link>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="..\BmsMonitor.App\Native\x86\kerneldlls\kerneldll.ini">
|
||||
<Link>kerneldlls\kerneldll.ini</Link>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,75 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user