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 abstractionsSuucha.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 typedeviceIndex: 0- Usually 0 for the first deviceusing- Ensures proper cleanup
2. Channel Configuration
var config = ChannelConfig.Standard250K();
using var channel = device.OpenChannel(channelIndex: 0, config);
Preset configurations available:
Standard125K()- 125 kbpsStandard250K()- 250 kbpsStandard500K()- 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
- API Reference - Explore the complete API
- Sending Messages - Learn more about sending
- Receiving Messages - Event-driven patterns
- Examples - More code examples
Troubleshooting
"Unable to load DLL 'zlgcan.dll'"
Install Visual C++ 2013 Redistributable:
Device Not Found
- Install ZLG device drivers
- Verify device appears in Device Manager
- Check the
deviceIndexparameter (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