How to read channel2 candata and write in channel3.I am configuring 2 handles but handlewrite value is getting 1 but it needs to be 0.How we can solve this. please look at my below code.
Thanks!
using System;
using canlibCLSNET;
using System.Threading;
namespace CanData06
{
class Program
{
private static int HexConvert(string z)
{
int m = Convert.ToInt32(z, 2);
return m;
}
private static void DumpMessageLoop(int handleRead, int handleWrite)
{
Canlib.canStatus status;
bool finished = false;
byte[] data = new byte[8];
int id;
int dlc;
int flags;
long timestamp;
Console.WriteLine("Channel opened. Press Escape to close. ");
Console.WriteLine("ID DLC DATA Timestamp");
while (!finished)
{
status = Canlib.canReadWait(handleRead, out id, data, out dlc, out flags, out timestamp, 100);
//Console.WriteLine(id);
string idBinary = Convert.ToString(id, 2).PadLeft(29, '0');
//Console.WriteLine(idBinary);
string hex1 = idBinary.Substring(0, 3);
string hex2 = idBinary.Substring(3, 18);
string hex3 = idBinary.Substring(21, 8);
//Console.WriteLine("{0} {1} {2}",hex1,hex2,hex3);
int h1 = HexConvert(hex1);
int h2 = HexConvert(hex2);
int h3 = HexConvert(hex3);
//Console.WriteLine("{0} {1} {2}", h1, h2, h3);
int combinedID = (h1 << 26) | (h2 << 8) | (h3);
//Console.WriteLine(combinedID);
status = Canlib.canWrite(handleWrite, combinedID, data, data.Length, Canlib.canMSG_EXT);
CheckStatus(status, "canWrite");
Thread.Sleep(10);
}
}
static void Main(string[] args)
{
// Holds a handle to the CAN channel
int handleRead;
int handleWrite;
// Status returned by the Canlib calls
Canlib.canStatus status;
// The CANlib channel number we would like to use
int channelNumber1 = 2;
int channelNumber2 = 3;
Console.WriteLine("Initializing Canlib");
// Initialize the Canlib library with a call to
// Canlib.initializeLibrary(). This always needs to be done before
// doing anything with the library.
Canlib.canInitializeLibrary();
handleRead = Canlib.canOpenChannel(channelNumber1, Canlib.canOPEN_ACCEPT_VIRTUAL);
CheckStatus((Canlib.canStatus)handleRead, "canOpenChannel(Read)");
status = Canlib.canSetBusParams(handleRead, Canlib.canBITRATE_250K | Canlib.canOPEN_REQUIRE_EXTENDED, 0, 0, 0, 0, 0);
CheckStatus(status, "canSetBusParams(Read)");
handleWrite = Canlib.canOpenChannel(channelNumber2, Canlib.canOPEN_ACCEPT_VIRTUAL);
CheckStatus((Canlib.canStatus)handleWrite, "canOpenChannel(Write)");
// Once we have successfully opened a channel, we need to set its bitrate. We
// do this using canSetBusParams. CANlib provides a set of predefined bus parameter
// settings in the form of canBITRATE_XXX constants. For other desired bus speeds
// bus paramters have to be set manually.
// See CANlib documentation for more information on parameter settings.
status = Canlib.canSetBusParams(handleWrite, Canlib.canBITRATE_250K | Canlib.canOPEN_REQUIRE_EXTENDED, 0, 0, 0, 0, 0);
CheckStatus(status, "canSetBusParams(Write)");
// Next, take the channel on bus using the canBusOn method. This
// needs to be done before we can send a message.
status = Canlib.canBusOn(handleRead);
CheckStatus(status, "canBusOn(Read)");
status = Canlib.canBusOn(handleWrite);
CheckStatus(status, "canBusOn(Write");
DumpMessageLoop(handleRead,handleWrite);
}
// The check method takes a canStatus (which is an enumerable) and the method
// name as a string argument. If the status is an error code, it will print it.
// Most Canlib method return a status, and checking it with a method like this
// is a useful practice to avoid code duplication.
private static void CheckStatus(Canlib.canStatus status, string method)
{
if (status < 0)
{
string errorText;
Canlib.canGetErrorText(status, out errorText);
Console.WriteLine(method + " failed: " + errorText);
}
}
}
}