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
NaveenPosted Jan 22, 2024, 4:42 AM
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 SinghPosted Jan 19, 2024, 4:27 PM
have you tried this
NaveenPosted Jan 19, 2024, 11:59 AM
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 SinghPosted Jan 19, 2024, 11:54 AM
Not sure
NaveenPosted Jan 19, 2024, 11:50 AM
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!
Sachin SinghPosted Jan 19, 2024, 11:46 AM
Maybe this should resolve your issue
NaveenPosted Jan 19, 2024, 10:51 AM
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);
}
}
}
}
Naimish MakwanaPosted Jan 19, 2024, 9:48 AM
Try below
Thanks
Sachin SinghPosted Jan 19, 2024, 9:37 AM
Then there are two options either call canWrite for each message or Concatenate them
OR
NaveenPosted Jan 19, 2024, 7:30 AM
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.
Sachin SinghPosted Jan 19, 2024, 6:30 AM
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.