C#_RS485库移植

维修电工:祁成

调试于台达伺服B3系列-400W

文件主要内容是获取出CRC校验码

CRC为16bit     低字节+高字节

单字节为D7~D0

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B3sever
{
    class HelpRS485
    {
        #region 转换
        public string getCRC(string str)
        {
            //字符串-》CRC字符串
            return getCRC_Modbus(str, false);
        }
        #endregion
        #region Modbus,getCRCstring
        public string getCRC_Modbus(string data, bool b)//change转换    b=false//小端
        {
            return getStringHex(getCRC16(getBytesHex(data), false));//小端
            //1 data转  字节Hex
            //2 获取CRC
            //3 获取crc字符串
        }

        public static byte[] getCRC16(byte[] bytes, bool b)//数组,端向
        {
            int len = bytes.Length;
            if (len > 0)
            {
                ushort crc = 0xFFFF;

                for (int i = 0; i < len; i++)
                {
                    crc = (ushort)(crc ^ (bytes[i]));
                    for (int j = 0; j < 8; j++)
                    {
                        crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
                    }
                }
                byte hi = (byte)((crc & 0xFF00) >> 8);  //高位置
                byte lo = (byte)(crc & 0x00FF);         //低位置
                if (b)//端向
                {
                    return new byte[] { hi, lo };//大端

                }
                else
                {
                    return new byte[] { lo, hi, };//小端

                }

            }
            return new byte[] { 0, 0 };
        }

        #endregion
        #region getBytesHex
        public byte[] getBytesHex(string data)//change转换
        {
            //清除所有空格
            string str = data.Replace(" ", "");

            //若字符个数为奇数,补一个0
            str += str.Length % 2 != 0 ? "0" : "";

            byte[] bytes = new byte[str.Length / 2];
            for (int i = 0, c = bytes.Length; i < c; i++)//获取bytes值
            {
                bytes[i] = Convert.ToByte(str.Substring(i * 2, 2), 16);
            }
            return bytes;
        }
        #endregion
        #region getStringHex
        public string getStringHex(byte[] data)//change转换
        {
            string str = string.Empty;
            for (int i = 0; i < data.Length; i++)
            {
                str += data[i].ToString("X2");
            }
            return str;
        }
        #endregion
        #region getTxHex
        public byte[] TxHex(string data)//change转换
        {
          string str=data+  getCRC_Modbus(data, false);//命令+CRC
          return   getBytesHex(str);//TX的bytes
        }
        #endregion

        #region 全局变量

        #endregion

        #region 全局属性

        #endregion

        #region 窗体构造

        #endregion
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值