public static bool IsPackageCorrect(byte[] package) { const int numberBytePosition = 2; if (package[numberBytePosition] < 20) { //ushort calculatedCrc16Value; //ushort receivedCrc16Value; byte[] tmpPackage = new byte[package[numberBytePosition]]; for (byte i = 0; i < package[numberBytePosition]; ++i) { tmpPackage[i] = package[i + 1]; } if (CRC16Calculation.ComputeCrc(tmpPackage) == GetCRC16FromPackage(package)) { return(true); } //else // return false; } //else //{ return(false); //} }
public static byte[] MakePackage(byte deviceAddress, byte modbusCommand) { const byte numberOfBytes = 2; const byte packageLenght = 5; byte[] tmpPackage = new byte[numberOfBytes]; byte[] tmpSendPackage = new byte[packageLenght]; tmpPackage[0] = modbusCommand; tmpPackage[1] = numberOfBytes; ushort crc16Value = CRC16Calculation.ComputeCrc(tmpPackage); tmpSendPackage[0] = deviceAddress; tmpSendPackage[1] = modbusCommand; tmpSendPackage[2] = numberOfBytes; tmpSendPackage[3] = Convert.ToByte(crc16Value >> 8); tmpSendPackage[4] = Convert.ToByte(crc16Value & 0xFF); return(tmpSendPackage);// }
public static byte[] MakePackage(byte deviceAddress, byte modbusCommand, byte channel, UInt32 data) { const byte numberOfBytes = 8; const byte packageLenght = 11; const byte crcLenght = 2; const UInt32 maxPeriod = 255; UInt32 overflow; overflow = data / maxPeriod; byte[] tmpPackage = new byte[numberOfBytes]; byte[] tmpSendPackage = new byte[packageLenght]; tmpPackage[0] = modbusCommand; tmpPackage[1] = numberOfBytes; tmpPackage[2] = channel; tmpPackage[3] = Convert.ToByte(overflow >> 24); tmpPackage[4] = Convert.ToByte((overflow >> 16) & 0xFF); tmpPackage[5] = Convert.ToByte((overflow >> 8) & 0xFF); tmpPackage[6] = Convert.ToByte(overflow & 0xFF); tmpPackage[7] = Convert.ToByte(data - overflow * maxPeriod); ushort crc16Value = CRC16Calculation.ComputeCrc(tmpPackage); tmpSendPackage[0] = deviceAddress; for (int i = 1; i < (packageLenght - crcLenght); ++i) { tmpSendPackage[i] = tmpPackage[i - 1]; } tmpSendPackage[9] = Convert.ToByte(crc16Value >> 8); tmpSendPackage[10] = Convert.ToByte(crc16Value & 0xFF); return(tmpSendPackage);// }