i am writing candata on kvaser canking the canwrite method can write only 1 hex value but i need to write 3 hex values. is there any other methgod for that.
Canlib.canWrite(int handle,int id,byte[] msg ,int dlc,int flag) this is my method.now i want to give 3 hex deciamls instead of id but it is not accepting.isd there any way?
Thanks!
Naveen

Sachin SinghRank #66 · 26.2K reputationPosted Jan 19, 2024, 1:30 AM EST
Then you will have to combine them, cause The
Canlib.canWritemethod in Kvaser CANlib expects a single integer ID value for the CAN message identifier.int combinedId = (hexValue1 << 16) | (hexValue2 << 8) | hexValue3; Canlib.canWrite(handle, combinedId, msg, dlc, flag);Sachin SinghRank #66 · 26.2K reputationPosted Jan 19, 2024, 4:37 AM EST
Then there are two options either call canWrite for each message or Concatenate them
int handle = ...; // Your handle int flag = ...; // Your flag int dlc = ...; // Your data length code // Hex values int hexValue1 = 0x04; int hexValue2 = 0x1F801; int hexValue3 = 0x7F; // Convert hex values to byte arrays byte[] msg1 = BitConverter.GetBytes(hexValue1); byte[] msg2 = BitConverter.GetBytes(hexValue2); byte[] msg3 = BitConverter.GetBytes(hexValue3); // Call canWrite for each message Canlib.canWrite(handle, hexValue1, msg1, dlc, flag); Canlib.canWrite(handle, hexValue2, msg2, dlc, flag); Canlib.canWrite(handle, hexValue3, msg3, dlc, flag);OR
int handle = ...; // Your handle int flag = ...; // Your flag int dlc = ...; // Your data length code // Hex values int hexValue1 = 0x04; int hexValue2 = 0x1F801; int hexValue3 = 0x7F; // Convert hex values to byte arrays byte[] msg1 = BitConverter.GetBytes(hexValue1); byte[] msg2 = BitConverter.GetBytes(hexValue2); byte[] msg3 = BitConverter.GetBytes(hexValue3); // Concatenate byte arrays byte[] concatenatedMsg = msg1.Concat(msg2).Concat(msg3).ToArray(); // Call canWrite with the concatenated message Canlib.canWrite(handle, hexValue1, concatenatedMsg, dlc, flag);Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 19, 2024, 4:48 AM EST
Try below
int handle = ...; // your handle int id = ...; // your id byte[] msg = new byte[4]; msg[0] = (byte)0x04; // your first hex value msg[1] = (byte)(0x1F801 >> 16); // upper byte of your second hex value msg[2] = (byte)(0x1F801 & 0xFF); // lower byte of your second hex value msg[3] = (byte)0x7F; // your third hex value int dlc = msg.length; // length of your message int flag = ...; // your flag Canlib.canWrite(handle, id, msg, dlc, flag);Thanks
NaveenRank #6,081 · 277 reputationPosted Jan 19, 2024, 5:51 AM EST
Hi sachin , please go through my code, can you tell me where i did wrong ? i am getting id as 7,1F801,02 INSTEAD OF 2,1F801,7F. i am thinking there is wrong in left shift operator.
// This tutorial walks you through how to open a channel and send a CAN message on it.
using System;
using canlibCLSNET;
using System.Threading;
namespace CanData05
{
class Program
{
private static int HexConvert(string z)
{
int m = Convert.ToInt32(z, 2);
return m;
}
private static void DumpMessageLoop(int handle)
{
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(handle, out id, data, out dlc, out flags, out timestamp, 100);
Console.WriteLine(id);
string idBinary = Convert.ToString(id, 2);
if(idBinary.Length != 29)
{
idBinary = "0" + idBinary;
}
//Console.WriteLine(idBinary);
string hex1 = idBinary.Substring(0, 3);
string hex2 = idBinary.Substring(3, 18);
string hex3 = idBinary.Substring(21, 8);
Console.WriteLine(hex1);
int h1 = HexConvert(hex1);
int h2 = HexConvert(hex2);
int h3 = HexConvert(hex3);
Console.WriteLine("{0} {1} {2}",h1,h2,h3);
int combinedID = (h1) | (h2 << 8) | (h3 <<25);
//Console.WriteLine(combinedID);
status = Canlib.canWrite(handle, combinedID, data, data.Length, 0);
CheckStatus(status, "canWrite");
Thread.Sleep(10);
}
}
static void Main(string[] args)
{
// Holds a handle to the CAN channel
int handle;
// Status returned by the Canlib calls
Canlib.canStatus status;
// The CANlib channel number we would like to use
int channelNumber = 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();
handle = Canlib.canOpenChannel(channelNumber, Canlib.canOPEN_ACCEPT_VIRTUAL);
CheckStatus((Canlib.canStatus)handle, "canOpenChannel");
// 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(handle, Canlib.canBITRATE_250K, 0, 0, 0, 0, 0);
CheckStatus(status, "canSetBusParams");
// Next, take the channel on bus using the canBusOn method. This
// needs to be done before we can send a message.
status = Canlib.canBusOn(handle);
CheckStatus(status, "canBusOn");
DumpMessageLoop(handle);
}
// 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);
}
}
}
}
NaveenRank #6,081 · 277 reputationPosted Jan 19, 2024, 2:30 AM EST
Hi sachin,
your answer is responding but not per my requirement. my hexvalue1 is 4 and hexvalue 2 is 1F801 and hexvalue 3 is 7F. please make changes for above.
NaveenRank #6,081 · 277 reputationPosted Jan 19, 2024, 6:50 AM EST
int combinedID = (h1) | (h2 << 3) | (h3 << 21); No its not giving expected output. i tried this. i tried
int combinedID = (h1) | (h2 << 8) | (h3 << 25); its giving output as 7,1F801,02 but my expected is 2,1F801,7F. So i think there is something need to be reverse but i am not getting what.
Thanks!
NaveenRank #6,081 · 277 reputationPosted Jan 19, 2024, 6:59 AM EST
I tried all random cases the major near answer is 7,1F801,7F BUT I WANT IS 2,1F801,7F. its coming as FFF80102 i need to convert this to 2FF8017F.
Sachin SinghRank #66 · 26.2K reputationPosted Jan 19, 2024, 6:54 AM EST
Not sure
int combinedID = (h1 << 25) | (h2 << 7) | h3;Sachin SinghRank #66 · 26.2K reputationPosted Jan 19, 2024, 11:27 AM EST
have you tried this
int combinedID = (h1 << 25) | (h2 << 7) | h3; combinedID &= 0x7FFFFFFF;NaveenRank #6,081 · 277 reputationPosted Jan 21, 2024, 11:42 PM EST
Hi sachin,
int combinedID = (h1 << 25) | (h2 << 7) | h3;
combinedID &= 0x7FFFFFFF; i have tried this but output coimg as 000000 ,any other way? i think we need to use & operator for combininedID?
Thanks!
Sachin SinghRank #66 · 26.2K reputationPosted Jan 19, 2024, 6:46 AM EST
Maybe this should resolve your issue
int combinedID = (h1) | (h2 << 3) | (h3 << 21);