Checksum calculation for telegrams added, Telegram class added

This commit is contained in:
Milan Hanajik
2015-02-18 18:05:32 +01:00
parent 5e4eec4d70
commit abd636b137
4 changed files with 169 additions and 79 deletions
@@ -144,7 +144,7 @@ namespace TBF.BenchControl.Danfoss.VLT2800
string response = responseBuilder.ToString(); string response = responseBuilder.ToString();
responseBuilder.Clear(); responseBuilder.Clear();
log.Info(Utils.LogMessage("Received ", response)); log.Info(Telegram.LogTelegram("Received ", response));
} }
} }
@@ -157,7 +157,7 @@ namespace TBF.BenchControl.Danfoss.VLT2800
dataToSend = data; dataToSend = data;
dataToSendReady = true; dataToSendReady = true;
log.Info(Utils.LogMessage("Sending ", data)); log.Info(Telegram.LogTelegram("Sending ", data));
return true; return true;
} }
@@ -213,7 +213,7 @@ namespace TBF.BenchControl.Danfoss.VLT2800
msg[9] = (byte)(serialComReference & 0xFF); /// Serial Com Reference Lo msg[9] = (byte)(serialComReference & 0xFF); /// Serial Com Reference Lo
msg[10] = (byte)(serialComReference >> 8); /// Serial Com Reference Hi msg[10] = (byte)(serialComReference >> 8); /// Serial Com Reference Hi
Utils.CalculateCRC(msg); Telegram.UpdateTelegramCRC(msg);
SendData(msg); SendData(msg);
} }
@@ -248,7 +248,7 @@ namespace TBF.BenchControl.Danfoss.VLT2800
msg[9] = (byte)(serialComReference & 0xFF); /// Serial Com Reference Lo msg[9] = (byte)(serialComReference & 0xFF); /// Serial Com Reference Lo
msg[10] = (byte)(serialComReference >> 8); /// Serial Com Reference Hi msg[10] = (byte)(serialComReference >> 8); /// Serial Com Reference Hi
Utils.CalculateCRC(msg); Telegram.UpdateTelegramCRC(msg);
SendData(msg); SendData(msg);
} }
+1
View File
@@ -856,6 +856,7 @@
<Compile Include="Screens\ResultsTabPageCtrl.Designer.cs"> <Compile Include="Screens\ResultsTabPageCtrl.Designer.cs">
<DependentUpon>ResultsTabPageCtrl.cs</DependentUpon> <DependentUpon>ResultsTabPageCtrl.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Telegram.cs" />
<Compile Include="TestProgressControls.cs" /> <Compile Include="TestProgressControls.cs" />
<Compile Include="TestWizard\FlowSelection.cs"> <Compile Include="TestWizard\FlowSelection.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
+164
View File
@@ -0,0 +1,164 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TBF
{
public class Telegram
{
const UInt16 crcSeed = 0xFFFF; // Initial crc value
const UInt16 crcGP = 0xA001; // Generating polynomial
/// <summary>
/// For standard CRC16 (remainder of division)
/// to start a new crc, set CRC16 = SEED
/// then for each byte call UpdateCRC(byte, &CRC16);
/// CRC16 will contain the result
/// (if you calculate all of the incoming data
/// INCLUDING the crc, the result should be 0x0000
/// and if you are sending the crc be sure to
/// send the bytes in the correct order)
/// <summary>
static void UpdateCRC(byte b, ref UInt16 crc)
{
crc ^= (UInt16)b;
for (int i = 0; i < 8; i++)
{
bool carry = (crc & 0x0001) != 0;
crc >>= 1;
if (carry) crc ^= crcGP;
}
}
/// <summary>
/// Calculates the CRC from a data telegram (an array of bytes).
/// A complete telegram including two bytes reserved for the CRC
/// must be supplied as an argument.
/// </summary>
/// <param name="data">Data telegram</param>
/// <returns>Calculated CRC</returns>
public static UInt16 ClaculateTelegramCRC(byte[] data)
{
UInt16 crc = crcSeed;
for (int i = 0; i < data.Length - 2; i++) UpdateCRC(data[i], ref crc);
return crc;
}
/// <summary>
/// This function modifies the last 2 bytes in the message buffer
/// by the CRC calculated from the remaining first (Lenght - 2) bytes.
/// A complete telegram including two bytes reserved for the CRC
/// must be supplied as an argument.
/// </summary>
/// <param name="data">Data telegram</param>
public static void UpdateTelegramCRC(byte[] data)
{
int len = data.Length;
if (len < 2) return;
UInt16 crc = ClaculateTelegramCRC(data);
data[len - 2] = (byte)(crc & 0xFF);
data[len - 1] = (byte)(crc >> 8);
}
/// <summary>
/// This function compares the last 2 bytes in the message buffer
/// with the CRC calculated from the remaining first (Lenght - 2) bytes.
/// A complete telegram including two bytes reserved for the CRC
/// must be supplied as an argument.
/// </summary>
/// <param name="data">Data telegram</param>
/// <returns>true if the CRC in the telegram is correct</returns>
public static bool VerifyTelegramCRC(byte[] data)
{
int len = data.Length;
if (len < 2) return false;
UInt16 crc = ClaculateTelegramCRC(data);
return (data[len - 2] == (byte)(crc & 0xFF)) && (data[len - 1] == (byte)(crc >> 8));
}
/// <summary>
/// Calculates the checksum from a data telegram (an array of bytes).
/// A complete telegram including one bytes reserved for the checksum
/// must be supplied as an argument.
/// </summary>
/// <param name="data">Data telegram</param>
/// <returns>Calculated CRC</returns>
public static byte ClaculateTelegramChecksum(byte[] data)
{
byte checksum = 0;
for (int i = 0; i < data.Length - 1; i++) checksum = (byte)(checksum ^ data[i]);
return checksum;
}
/// <summary>
/// This function modifies the last byte in the message buffer
/// by the checksum calculated from the remaining first (Lenght - 1) bytes.
/// A complete telegram including one bytes reserved for the checksum
/// must be supplied as an argument.
/// </summary>
/// <param name="data">Data telegram</param>
public static void UpdateTelegramChecksum(byte[] data)
{
int len = data.Length;
if (len < 1) return;
data[len - 1] = ClaculateTelegramChecksum(data);
}
/// <summary>
/// This function compares the last byte in the message buffer
/// with the checksum calculated from the remaining first (Lenght - 1) bytes.
/// A complete telegram including two bytes reserved for the checksum
/// must be supplied as an argument.
/// </summary>
/// <param name="data">Data telegram</param>
/// <returns>true if the CRC in the telegram is correct</returns>
public static bool VerifyTelegramChecksum(byte[] data)
{
int len = data.Length;
if (len < 1) return false;
return (data[len - 1] == ClaculateTelegramChecksum(data));
}
public static string LogTelegram(string intro, byte[] data)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(80);
sb.Append(intro);
sb.Append(data.Length.ToString());
sb.Append(" bytes:");
foreach (var d in data)
{
sb.Append(" ");
sb.Append(((int)d).ToString("X2"));
}
return sb.ToString();
}
public static string LogTelegram(string intro, string data)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(80);
sb.Append(intro);
sb.Append(data.Length.ToString());
sb.Append(" bytes:");
foreach (var d in data)
{
sb.Append(" ");
sb.Append(((int)d).ToString("X2"));
}
return sb.ToString();
}
}
}
-75
View File
@@ -250,80 +250,5 @@ namespace TBF
} }
else return value.ToString(); else return value.ToString();
} }
public static string LogMessage(string intro, byte[] data)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(80);
sb.Append(intro);
sb.Append(data.Length.ToString());
sb.Append(" bytes:");
foreach (var d in data)
{
sb.Append(" ");
sb.Append(((int)d).ToString("X2"));
}
return sb.ToString();
}
public static string LogMessage(string intro, string data)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(80);
sb.Append(intro);
sb.Append(data.Length.ToString());
sb.Append(" bytes:");
foreach (var d in data)
{
sb.Append(" ");
sb.Append(((int)d).ToString("X2"));
}
return sb.ToString();
}
const UInt16 crcSeed = 0xFFFF; // Initial crc value
const UInt16 crcGP = 0xA001; // Generating polynomial
/// <summary>
/// For standard CRC16 (remainder of division)
/// to start a new crc, set CRC16 = SEED
/// then for each byte call UpdateCRC(byte, &CRC16);
/// CRC16 will contain the result
/// (if you calculate all of the incoming data
/// INCLUDING the crc, the result should be 0x0000
/// and if you are sending the crc be sure to
/// send the bytes in the correct order)
/// <summary>
static void UpdateCRC(byte b, ref UInt16 crc)
{
crc ^= (UInt16)b;
for (int i = 0; i < 8; i++)
{
bool carry = (crc & 0x0001) != 0;
crc >>= 1;
if (carry) crc ^= crcGP;
}
}
/// <summary>
/// This function modifies the last 2 bytes in the message buffer
/// by the crc calculated from the remainin first (Lenght - 2) bytes.
/// </summary>
/// <param name="data"></param>
public static void CalculateCRC(byte[] data)
{
int len = data.Length;
if (len < 2) return;
UInt16 crc = crcSeed;
for (int i = 0; i < len - 2; i++) UpdateCRC(data[i], ref crc);
data[len - 2] = (byte)(crc & 0xFF);
data[len - 1] = (byte)(crc >> 8);
}
} }
} }