组帧封包-20221108

42                     byte address = Convert.ToByte( tbxAddress.Text.Trim(), 16);//地址码
43                     byte cmd = Convert.ToByte(tbxCmd.Text.Trim(),16);//命令帧
44                     byte regAddr = Convert.ToByte(tbxRegAddr.Text.Trim(), 16);//寄存器地址
45                     byte regNum = Convert.ToByte(tbxRegNum.Text.Trim(), 16);//寄存器数量
46 
47                     
48                     //Modbus相关处理对象
49                     MyModbus modbus = new MyModbus();
50                     byte[] text = modbus.GetReadFrame(address, cmd, regAddr, regNum, 8);
51 
52                     sp.Write(text, 0, 8);
53                     tbxRecvData.Text += "Send:" + BitConverter.ToString(text)+ "\r\n";

 

/// <summary>
        /// 【获取读数据命令,返回命令帧】
        /// </summary>
        /// <param name="mdaddr">地址码</param>
        /// <param name="R_CMD">功能码</param>
        /// <param name="min_reg">寄存器地址</param>
        /// <param name="data_len">寄存器个数</param>
        /// <param name="R_CMD_LEN">命令长度</param>
        /// <returns></returns>
        public byte[] GetReadFrame(byte mdaddr, byte R_CMD, ushort min_reg, ushort data_len, int R_CMD_LEN)
        {
            //主机命令帧格式
            //  字节    功能描述            例子
            //
            //  1         地址码             0x01
            //  2         功能码             0x03
            //  3         寄存器地址高       0x00
            //  4         寄存器地址低       0x00
            //  5         寄存器个数高       0x00
            //  6         寄存器个数低       0x02
            //  7         CRC检验码低        0xC4
            //  8         CRC校验码高        0x0B

            ushort crc;
            byte[] message = new byte[8];

            //设置模块号
            message[0] = mdaddr;
            //设置命令字
            message[1] = R_CMD;

            //设置开始寄存器
            message[2] = WORD_HI(min_reg);
            message[3] = WORD_LO(min_reg);

            //设置数据长度
            message[4] = WORD_HI(data_len);
            message[5] = WORD_LO(data_len);

            //设置 CRC
            crc = CRC16(message, 0, R_CMD_LEN - 3);

            message[6] = WORD_HI(crc);//CRC校验码高位
            message[7] = WORD_LO(crc);//CRC校验码低位


            return message;
        }

 

126         /// <summary>
127         /// 【格式化输出,校验读取的数据】
128         /// </summary>
129         /// <param name="readBuffer"></param>
130         /// <returns></returns>
131         public string SetText(byte[] readBuffer)
132         {
133             //将byte 转换成string 用于显示
134             //string readstr = string.Empty;
135             if (readBuffer != null)
136             {
137                 ushort crc = CRC16(readBuffer, 0, readBuffer.Length - 3);
138                 if (readBuffer[readBuffer.Length - 2] == WORD_HI(crc) && readBuffer[readBuffer.Length - 1] == WORD_LO(crc))//crc校验
139                 { 
140                     return ToHexString(readBuffer);
141                 }
142                 else
143                 {
144                     return "CRC校验错误";
145                 }
146             }
147 
148             return "程序出错";
149         }
/// <summary>
177         /// 【获取大写字母】
178         /// </summary>
179         /// <param name="bytes"></param>
180         /// <returns></returns>
181         public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF "
182         {
183             string hexString = string.Empty;
184 
185             if (bytes != null)
186             {
187 
188                 StringBuilder strB = new StringBuilder();
189 
190                 for (int i = 0; i < bytes.Length; i++)
191                 {
192 
193                     strB.Append(bytes[i].ToString("X2"));
194 
195                 }
196 
197                 hexString = strB.ToString();
198 
199             } return hexString;

 

 

https://www.cnblogs.com/reader/p/6561119.html

posted @ 2022-11-08 23:56  流水江湖  阅读(53)  评论(0编辑  收藏  举报