43 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C#
		
	
	
	
		
		
			
		
	
	
			43 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C#
		
	
	
	
| 
								 | 
							
								using System;
							 | 
						|||
| 
								 | 
							
								using System.Collections.Generic;
							 | 
						|||
| 
								 | 
							
								using System.Linq;
							 | 
						|||
| 
								 | 
							
								using System.Text;
							 | 
						|||
| 
								 | 
							
								using System.Threading.Tasks;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								namespace DM_Weight.Common
							 | 
						|||
| 
								 | 
							
								{
							 | 
						|||
| 
								 | 
							
								    public class CRC16MODBUS
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        /// Name: CRC-16/MODBUS    x16+x15+x2+1
							 | 
						|||
| 
								 | 
							
								        /// Poly: 0x8005
							 | 
						|||
| 
								 | 
							
								        /// Init: 0xFFFF
							 | 
						|||
| 
								 | 
							
								        /// Refin: true
							 | 
						|||
| 
								 | 
							
								        /// Refout: true
							 | 
						|||
| 
								 | 
							
								        /// Xorout: 0x0000
							 | 
						|||
| 
								 | 
							
								        ///******************************添加数据CRC16MODBUS校验位*******************************************
							 | 
						|||
| 
								 | 
							
								        public static byte[] CrcModBus(byte[] buffer, int start = 0, int len = 0)
							 | 
						|||
| 
								 | 
							
								        {
							 | 
						|||
| 
								 | 
							
								            if (buffer == null || buffer.Length == 0) return null;
							 | 
						|||
| 
								 | 
							
								            if (start < 0) return null;
							 | 
						|||
| 
								 | 
							
								            if (len == 0) len = buffer.Length - start;
							 | 
						|||
| 
								 | 
							
								            int length = start + len;
							 | 
						|||
| 
								 | 
							
								            if (length > buffer.Length) return null;
							 | 
						|||
| 
								 | 
							
								            ushort crc = 0xFFFF;// Initial value
							 | 
						|||
| 
								 | 
							
								            for (int i = start; i < length; i++)
							 | 
						|||
| 
								 | 
							
								            {
							 | 
						|||
| 
								 | 
							
								                crc ^= buffer[i];
							 | 
						|||
| 
								 | 
							
								                for (int j = 0; j < 8; j++)
							 | 
						|||
| 
								 | 
							
								                {
							 | 
						|||
| 
								 | 
							
								                    if ((crc & 1) > 0)
							 | 
						|||
| 
								 | 
							
								                        crc = (ushort)((crc >> 1) ^ 0xA001);// 0xA001 = reverse 0x8005
							 | 
						|||
| 
								 | 
							
								                    else
							 | 
						|||
| 
								 | 
							
								                        crc = (ushort)(crc >> 1);
							 | 
						|||
| 
								 | 
							
								                }
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            byte[] ret = BitConverter.GetBytes(crc);
							 | 
						|||
| 
								 | 
							
								            //Array.Reverse(ret);
							 | 
						|||
| 
								 | 
							
								            return ret;
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								}
							 |