Table of Contents

Quick Start

This guide will help you get started with Suucha.Can in just 5 minutes.

Prerequisites

Before you begin, ensure you have:

  • ✅ Windows 10/11 (x86 or x64)
  • ✅ .NET 8.0 SDK or Runtime
  • ✅ ZLG device drivers installed - Download here
  • ✅ Visual C++ 2013 Redistributable - x64 / x86
  • ✅ ZLG CAN device connected

Important: You must install the official ZLG device drivers before using this library.

Installation

Add the NuGet package to your project:

dotnet add package Suucha.Can.Zlg.Devices

This automatically includes:

  • Suucha.Can.Core - Core abstractions
  • Suucha.Can.Zlg.Native - Native DLLs (x86 + x64)

Your First CAN Application

Step 1: Create a Console Application

dotnet new console -n MyCanApp
cd MyCanApp
dotnet add package Suucha.Can.Zlg.Devices

Step 2: Write the Code

Replace the content of Program.cs:

using Suucha.Can.Core.Models;
using Suucha.Can.Zlg.Devices;

Console.WriteLine("Suucha.Can Quick Start Demo");
Console.WriteLine("============================\n");

try
{
    // Step 1: Create and open device
    Console.WriteLine("Opening device...");
    using var device = new ZlgCanDevice(DeviceType.USBCAN2, deviceIndex: 0);
    device.Open();
    Console.WriteLine("✓ Device opened\n");

    // Step 2: Configure channel
    Console.WriteLine("Configuring channel...");
    var config = ChannelConfig.Standard250K(); // 250 kbps
    using var channel = device.OpenChannel(channelIndex: 0, config);
    Console.WriteLine("✓ Channel configured\n");

    // Step 3: Subscribe to events
    Console.WriteLine("Subscribing to events...");
    channel.MessageReceived += (sender, frame) =>
    {
        var dataHex = BitConverter.ToString(frame.Data).Replace("-", " ");
        Console.WriteLine($"[RX] ID: 0x{frame.Id:X3} | Data: {dataHex}");
    };

    channel.ErrorOccurred += (sender, e) =>
    {
        Console.WriteLine($"[ERROR] {e.ErrorInfo}");
    };
    Console.WriteLine("✓ Events subscribed\n");

    // Step 4: Start channel
    Console.WriteLine("Starting channel...");
    channel.Start();
    Console.WriteLine("✓ Channel started\n");

    // Step 5: Send a test message
    Console.WriteLine("Sending test message...");
    var message = new CanFrame
    {
        Id = 0x123,
        Data = new byte[] { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 },
        IsExtended = false
    };

    int sent = channel.Send(message);
    if (sent == 1)
    {
        Console.WriteLine("✓ Message sent successfully\n");
    }
    else
    {
        Console.WriteLine("✗ Failed to send message\n");
    }

    // Step 6: Wait for messages
    Console.WriteLine("Listening for messages... (Press any key to exit)");
    Console.ReadKey();

    // Step 7: Cleanup (automatic via 'using')
    Console.WriteLine("\nStopping channel...");
    channel.Stop();
    Console.WriteLine("✓ Channel stopped");
}
catch (DllNotFoundException ex)
{
    Console.WriteLine($"\n✗ ERROR: CAN driver DLL not found");
    Console.WriteLine($"  {ex.Message}");
    Console.WriteLine("\nSolution:");
    Console.WriteLine("  1. Install Visual C++ 2013 Redistributable");
    Console.WriteLine("  2. Ensure zlgcan.dll is in the application directory");
}
catch (Exception ex)
{
    Console.WriteLine($"\n✗ ERROR: {ex.Message}");
    Console.WriteLine($"\nStack Trace:\n{ex.StackTrace}");
}

Console.WriteLine("\nPress any key to exit...");
Console.ReadKey();

Step 3: Run the Application

dotnet run

Understanding the Code

1. Device Creation

using var device = new ZlgCanDevice(DeviceType.USBCAN2, deviceIndex: 0);
device.Open();
  • DeviceType.USBCAN2 - Specify your device type
  • deviceIndex: 0 - Usually 0 for the first device
  • using - Ensures proper cleanup

2. Channel Configuration

var config = ChannelConfig.Standard250K();
using var channel = device.OpenChannel(channelIndex: 0, config);

Preset configurations available:

  • Standard125K() - 125 kbps
  • Standard250K() - 250 kbps
  • Standard500K() - 500 kbps

Or create custom:

var config = new ChannelConfig(baudrate: 250000);

3. Event-Driven Receiving

channel.MessageReceived += (sender, frame) =>
{
    // Handle received message
    Console.WriteLine($"Received: 0x{frame.Id:X3}");
};

4. Sending Messages

Synchronous:

int sent = channel.Send(message);

Asynchronous (recommended for UI apps):

await channel.SendAsync(message);

Next Steps

Troubleshooting

"Unable to load DLL 'zlgcan.dll'"

Install Visual C++ 2013 Redistributable:

Device Not Found

  1. Install ZLG device drivers
  2. Verify device appears in Device Manager
  3. Check the deviceIndex parameter (usually 0)

Architecture Mismatch

Ensure your application platform target matches:

  • AnyCPU (recommended) - Works on both x86 and x64
  • x64 - For 64-bit systems
  • x86 - For 32-bit systems