台达B3 系列伺服串口命令破解

发布时间:2024年01月22日

1、安装台达软件?ASDA SOFT V6.5

2、安装串口监控工具 CEWEI?CommMonitor?

3、监控 ASDA SOFT V6.5 操作 将写入串口数据拷贝到软件中点击破解 即可查看原始命令

using System;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;

namespace DeltaCommandCreack
{
    public class DeltaE3Help
    {
        
        public int Delay { get; set; } = 20;            //发送命令后延迟时间毫秒
        public string ReceivedData = string.Empty;     //接收的数据
        private bool Listening = false;                 //是否没有执行完invoke相关操作
        public SerialPort mySerialPort = new SerialPort();

        public string PortName { get; set; } = "COM1";          //端口名称
        public int BaudRate { get; set; } = 9600;               //波特率
        public Parity Parity { get; set; } = Parity.None;       //校验位
        public int DataBits { get; set; } = 8;                  //数据位
        public StopBits StopBits { get; set; } = StopBits.One;  //停止位
        public int ReadTimeout { get; set; } = 15;              //读取超时

        #region 初始化串口
        //**************************************************************************
        //初始化 mySerialPort Com 口
        //**************************************************************************
        private void InitmySerialPort()
        {
            mySerialPort.PortName = PortName;           //端口名称
            mySerialPort.BaudRate = BaudRate;           //设置串行波特率。
            mySerialPort.Parity = Parity;               //设置奇偶校验位。
            mySerialPort.DataBits = DataBits;           //设置每个字节的标准数据位长度。
            mySerialPort.StopBits = StopBits;           //设置每个字节的标准停止位数
            mySerialPort.Handshake = Handshake.None;    //设置串行端口数据传输的握手协议。
            mySerialPort.RtsEnable = true;              //设置一个值,该值指示在串行通信中是否启用请求发送 (RTS) 信号。
            mySerialPort.ReadTimeout = ReadTimeout;     //设置读取操作未完成时发生超时之前的毫秒数。
            mySerialPort.ReceivedBytesThreshold = 1;
            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived);
        }
        #endregion

        #region 打开与关闭串口
        //**************************************************************************
        //打开 mySerialPort 串口
        //**************************************************************************   
        public bool OpenPort()
        {
            if (mySerialPort.IsOpen)
            {
                return true;
            }
            else
            {
                try
                {
                    InitmySerialPort();
                    mySerialPort.Open();
                    return true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
            }
        }

        //**************************************************************************
        //关闭 mySerialPort 串口
        //**************************************************************************   
        public void ClosePort()
        {
            if (mySerialPort.IsOpen)
            {
                while (Listening) Application.DoEvents();
                mySerialPort.Close();
            }
        }
        #endregion

        #region 发送命令
        public void SendCommand(string command)
        {
            byte[] bytes = HexStringToBytes(command);
            mySerialPort.Write(bytes, 0, bytes.Length);
            Thread.Sleep(20);
        }
        #endregion

        #region 接收数据
        //**************************************************************************
        //接收数据 mySerialPort Com
        //**************************************************************************
        private void mySerialPort_DataReceived(Object sender, SerialDataReceivedEventArgs e)
        {
            if (mySerialPort.IsOpen == false) return;
            try
            {
                Listening = true;                       //设置标记,说明我已经开始处理数据,一会儿要使用系统UI的。
                int dataLength = mySerialPort.BytesToRead;       //要读取的串口数据大小
                if (dataLength > 0)
                {
                    byte[] dataBuffer = new byte[dataLength];        //数组存储串口数据              
                    mySerialPort.Read(dataBuffer, 0, dataLength);    //读取串口数据
                    ReceivedData = BytesToHexString(dataBuffer);
                }
            }
            finally
            {
                Listening = false;                      //我用完了,ui可以关闭串口了。
            }
        }
        #endregion

        #region 字符串转字节数组
        public static byte[] HexStringToBytes(string hex)
        {
            int length = hex.Length;
            byte[] bytes = new byte[length / 2];

            for (int i = 0; i < length; i += 2)
            {
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            }
            return bytes;
        }
        #endregion

        #region 字节数组转字符串
        public static string BytesToHexString(byte[] data)
        {
            StringBuilder hexString = new StringBuilder();

            foreach (byte b in data)
            {
                hexString.Append(b.ToString("X2"));
            }

            return hexString.ToString();
        }
        #endregion

        #region 命令破解 
        public string ExtractRegisterAddressAndData(string command)
        {
            // Removing spaces and converting to byte array
            byte[] bytes = HexStringToBytes(command);

            // Extracting the register address (at bytes 2 and 3)
            byte[] regAddressBytes = new byte[2];
            Array.Copy(bytes, 2, regAddressBytes, 0, 2);
            int regAddress = BitConverter.ToInt16(new byte[] { regAddressBytes[1], regAddressBytes[0] }, 0);
            string register = ConvertToRegisterFormat(regAddress);

            // Extracting the data part (assuming it's a 16-bit value at bytes 7 and 8)
            byte[] dataBytes = new byte[2];
            Array.Copy(bytes, 7, dataBytes, 0, 2);
            int data = BitConverter.ToInt16(new byte[] { dataBytes[1], dataBytes[0] }, 0);

            string Command = "Register Address: " + register.Replace("X", string.Empty) + "  Data: " + data.ToString();
            return Command;
        }

        private string ConvertToRegisterFormat(int regAddress)
        {
            int hiPart = (regAddress & 0xFF00) >> 8;
            int loPart = (regAddress & 0x00FF) >> 1; // Divide by 2 to reverse the left shift by 1 in GetRegAddress

            return $"P{hiPart}.X{loPart:D3}"; // "D3" ensures the low part is a three-digit number
        }
        #endregion
    }
}
文章来源:https://blog.csdn.net/shadowsky/article/details/135739092
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。