using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kvaser.CanLib;
namespace SendMessage
{
class Program
{
static void Main(string[] args)
{
int handle;
Canlib.canStatus status;
int channelNumber = 0;
byte[] msg = {0, 1, 2, 3, 4, 5, 6, 7};
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.WriteLine("Initializing Canlib");
Canlib.canInitializeLibrary();
Console.WriteLine("Opening channel {0}", channelNumber);
handle = Canlib.canOpenChannel(channelNumber, Canlib.canOPEN_ACCEPT_VIRTUAL);
CheckStatus((Canlib.canStatus)handle, "canOpenChannel");
Console.WriteLine("Setting channel bitrate");
status = Canlib.canSetBusParams(handle, Canlib.canBITRATE_250K, 0, 0, 0, 0);
CheckStatus(status, "canSetBusParams");
Console.WriteLine("Going on bus");
status = Canlib.canBusOn(handle);
CheckStatus(status, "canBusOn");
Console.WriteLine("Writing a message to the channel");
status = Canlib.canWrite(handle, 123, msg, 8, 0);
CheckStatus(status, "canWrite");
Console.WriteLine("Waiting for the message to be transmitted");
status = Canlib.canWriteSync(handle, 1000);
CheckStatus(status, "canWriteSync");
Console.WriteLine("Going off bus");
status = Canlib.canBusOff(handle);
CheckStatus(status, "canBusOff");
Console.WriteLine("Closing channel {0}", channelNumber);
status = Canlib.canClose(handle);
CheckStatus(status, "canClose");
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
private static void CheckStatus(Canlib.canStatus status, string method)
{
if (status < 0)
{
string errorText;
Canlib.canGetErrorText(status, out errorText);
Console.WriteLine(method + " failed: " + errorText);
}
}
}
}