Пример #1
0
        private void CalCheckData()
        {
            try
            {
                UInt32 checkResult = 0;


                if (_DataBytes == null)
                {
                    return;
                }

                gbCRCParam.Enabled = false;

                int index = cbDataCheckType.SelectedIndex;
                if (index == -1)
                {
                    return;
                }
                if (index == 0)
                {
                    checkResult = Convert.ToUInt16(DataCheck.GetCheckSum(_DataBytes) & 0xFFFF);
                    dataWidth   = 16;
                }
                else if (index == 1)
                {
                    checkResult = Convert.ToByte(DataCheck.GetCheckSum(_DataBytes) & 0xFF);
                    dataWidth   = 8;
                }
                else if (index == 2)
                {
                    checkResult = DataCheck.GetXor(_DataBytes);
                    dataWidth   = 8;
                }
                else
                {
                    gbCRCParam.Enabled = true;

                    CRCType crcType = (CRCType)(index - 3);


                    CRCInfo crcInfo = DataCheck.GetCRCInfo(crcType);
                    crcInfo.Poly   = Convert.ToUInt32(txtPoly.Text, 16);
                    crcInfo.Init   = Convert.ToUInt32(txtInit.Text, 16);
                    crcInfo.RefIn  = chkRefIn.Checked;
                    crcInfo.RefOut = chkRefOut.Checked;
                    crcInfo.XorOut = chkXorOut.Checked ? 0xFFFFFFFF : 0x00000000;



                    if (crcType >= CRCType.CRC16_IBM && crcType <= CRCType.CRC16_DNP)
                    {
                        checkResult = DataCheck.GetCRC16(crcInfo, _DataBytes);
                        dataWidth   = 16;
                    }
                    else if (crcType >= CRCType.CRC32 && crcType <= CRCType.CRC32_MPEG2)
                    {
                        checkResult = DataCheck.GetCRC32(crcInfo, _DataBytes);
                        dataWidth   = 32;
                    }
                }

                CheckResultValue = checkResult;
                txtResult.Text   = getStringByUint32(CheckResultValue, chkIsLittleEndian.Checked, dataWidth);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }