我有一个 J1939 CAN 原始字符串,格式如下:
CAN:0B00FEE99CF002000CEF02000B00FEE81A9A9F60FFFFB8570B00FEE042522500425225000B00FEE5E0530100C89F0400
此字符串包含一些 CAN 消息,这些消息分为 3 个部分,例如
0B 00FEE99 CF002000CEF0200
1) PGN 数据长度(以字节为单位)0B
2)PGN号(3字节长)0FEE99
3) PGN 数据CF002000CEF0200
目前,我正在使用子字符串来解析这 3 个部分,但我没有得到正确的值。我在想我可能犯了一个错误,因为我没有将字符串转换为字节数组。这是我的代码的一部分:
int CANStrLength = 24;
int CANIndex = CANDataIndex(rawDataElements);
int CANStrIndex = rawDataElements[CANIndex].IndexOf("CAN:");
string CANmessage = rawDataElements[CANIndex].Substring(CANStrIndex + 4).Split(',').First();
Console.WriteLine("\nIn rawDataElements[{0}]: {1}\nLength of CAN data: {2}", CANIndex, CANmessage, CANmessage.Length);
int numberOfCANMessages = CANmessage.Length / CANStrLength;
Console.WriteLine("There are {0} CAN messages", numberOfCANMessages);
List<string> CANMessages = SplitIntoParts(CANmessage, CANStrLength);
for (int i = 0; i < numberOfCANMessages; i++)
{
int pgnDataLength = Convert.ToInt32(CANMessages[i].Substring(0, 2), 16);
int pgnNumber = Convert.ToInt32(CANMessages[i].Substring(2, 6), 16);
long CANData = Convert.ToInt64(CANMessages[i].Substring(8), 16);
Console.WriteLine();
Console.WriteLine(CANMessages[i]);
switch (pgnNumber)
{
// fuel consumption */
case 65257:
string totalFuelUsedStr = CANMessages[i].Substring(8).Substring(8, 4);
double totalFuelUsed = Convert.ToInt32(totalFuelUsedStr, 16) * 0.5;
Console.WriteLine("Total Fuel Used: {0}L, {1}gal", totalFuelUsed, (int)(totalFuelUsed* 0.26));
break;