本文介绍了cmnd =“?Flow"之间没有区别.和“!Flow"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我正在尝试从以下代码中计算出冗余校验字节,当?与!.交换

希望您能提供帮助!

步骤如下

计算LRC字节.

生成LRC的过程是:

添加消息中的所有字节,但开头的冒号"除外.所有进位都被丢弃.
LCR =-(8位值);两个人的补语
示例:
Packet [10]是一个10字节的char数组,分配如下:
数据包[0] =‘?'= 0x3F
封包[1] ="F" = 0x46
封包[2] =‘l’= 0x6C
封包[3] ='o'= 0x6F
封包[4] =’w’= 0x77
Packet [5] = 0//将是LRC的高字节
Packet [6] = 0//将是LRC的低字节
Packet [7] = 0//将返回回车; 0x0D
Packet [8] = 0//将作为换行符; 0x0A
Packet [9] = 0//C可选字节中的字符串终止符
packetLength = strlen(Packet)//packetLength = 5;
C解析为数组无符号字节LRC = 0的前0////以0 LRC值开始

For(i = 0; i <packetLength; i ++)LRC = LRC + Packet [i]; //将所有字节添加到LRC
对于此示例:LRC = 0x3F + 0x46 + 0x6C + 0x6F + 0x77 = 0x1D7(如果未舍弃进位)

LRC = 0xD7放弃了进位LRC = -LRC//两个补码
二进制补码是〜(0xD7)+ 1 = 0x28 +1 = 0x29(〜是非运算符)(有关非运算符或二进制补码的更多信息,请参阅Wikipedia)
现在将结果分为高位和低位2和9.
将这些值中的每一个都转换为等效的ASCII十六进制.
2-> 0x32
9-> 0x39

Packet [5] =转换为ASCII的LRC的高4位,2-> 0x32
数据包[6] = LRC的低4位转换为ASCII,9-> 0. 0x39
Packet [7] = 0x0D//回车Packet [8] = 0x0A//换行

回顾:
1)在消息中添加所有字节
2)将NOT应用于结果并添加1(2的补数)
3)将字节分成高段和低段
4)将高低段转换为ASCII十六进制值

代码:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;

命名空间LabVIEWInterface
{
   公共类Calcbyte
    {
       公共静态字节[] CalcLRC(字节[] cmnd)
        {

            int i,j,bSize = cmnd.Length;
            uint lrc,高,低,CR,LF;
           字符串temp;
            byte []个字节=新的字节[(((bSize)+ 4)];
            lrc = 0x00;
            CR = 0x0d;
            LF = 0x0a;
            //循环遍历输入字节数组
           对于(i = 0; i< cmnd.Length; i ++)
            {
                //求和消息字节
                lrc = lrc +(uint)cmnd [i];

                //将每个消息字节插入到结果中
                bytes [i] = cmnd [i];
            }
            //删除剩余部分
           如果(lrc> 256)
            {
                lrc = lrc-(uint)(256 * Math.Floor((double)lrc/256));
            }

            //对和执行2的补码
            lrc =(ushort)(〜lrc);
            lrc = lrc + 1;
            //删除剩余部分
           如果(lrc> 256)
            {
                lrc = lrc-(uint)(256 * Math.Floor((double)lrc/256));
            }
            //分为高低位
            temp = lrc.ToString("X");
           高=(uint)temp [0];
           低=(uint)temp [1];

            //将高,低,回车符和换行符插入结果
            bytes [[bSize)] =(byte)high;
            bytes [(bSize +1)] =(低)字节;
            bytes [(bSize + 2)] =(byte)CR;
            bytes [(bSize + 3)] =(byte)LF;

            //返回具有输入,LRC,CR,LF的结果字节数组
           返回字节;
        }
    }
}

解决方案


Hello 

I am trying to calculate redundancy check bytes from following code, the bytes does not change when ? is exchanged with !. 

I hope you can help!

the procedure is as follows

Calculating the LRC bytes.

A procedure for generating an LRC is:

Add all bytes in the message, excluding the starting ‘colon’. All carries are discarded.
LCR = -(8 bit value ) ; two’s complement
Example:
Packet[10] is a 10 byte char array assigned as follows
Packet[0] = ‘?’ = 0x3F
Packet[1] =’F’ = 0x46
Packet[2]=’l’ =0x6C
Packet[3]=’o’ =0x6F
Packet[4]=’w’ =0x77
Packet[5]=0 // will be high byte of LRC
Packet[6]=0 // will be low byte of LRC
Packet[7]=0 // will be carriage return; 0x0D
Packet[8]=0 // will be line feed; 0x0A
Packet[9]=0 // string terminator in C optional byte
packetLength = strlen(Packet) // packetLength= 5;
C parses to first 0 in the array unsigned byte LRC=0 // start with 0 LRC value

For(i=0; i<packetLength; i++) LRC = LRC + Packet[i]; // add all bytes into LRC
For this example: LRC = 0x3F + 0x46 + 0x6C + 0x6F + 0x77 = 0x1D7 (if carries are not discarded)

LRC = 0xD7 discarding the carry LRC = -LRC // two’s complement
Two’s complement is ~(0xD7) + 1 = 0x28 + 1 = 0x29 (The ~ is the not operator) (see Wikipedia for more on not operator or two’s complement)
Now split the result into high and low bits, 2 and 9.
Convert each of these values to their ASCII hex equivalent.
2 -> 0x32
9 -> 0x39

Packet[5] = high 4 bits of LRC converted to ASCII, 2 -> 0x32
Packet[6] = low 4 bits of LRC converted to ASCII, 9 -> 0x39
Packet[7] = 0x0D // carriage return Packet[8]= 0x0A // line feed

Recap:
1) Add all bytes in message
2) Apply NOT to result and add 1 (2’s complement)
3) Split byte into high and low segments
4) Convert high and low segments to ASCII Hex value</packetlength;>

code:

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

namespace LabVIEWInterface
{
    public class Calcbyte
    {
        public static byte[] CalcLRC(byte[] cmnd)
        {

            int i, j, bSize = cmnd.Length;
            uint lrc, high, low, CR, LF;
            string temp;
            byte[] bytes = new byte[((bSize) + 4)];
            lrc = 0x00;
            CR = 0x0d;
            LF = 0x0a;
            //loop through input byte array
            for (i = 0; i < cmnd.Length; i++)
            {
                //sum message bytes
                lrc = lrc + (uint)cmnd[i];

                //insert each message byte into result
                bytes[i] = cmnd[i];
            }
            //remove remainder
            if (lrc > 256)
            {
                lrc = lrc - (uint)(256 * Math.Floor((double)lrc / 256));
            }

            //perform 2's complement on sum
            lrc = (ushort)(~lrc);
            lrc = lrc + 1;
            //remove remainder
            if (lrc > 256)
            {
                lrc = lrc - (uint)(256 * Math.Floor((double)lrc / 256));
            }
            //split into high and low bits
            temp = lrc.ToString("X");
            high = (uint)temp[0];
            low = (uint)temp[1];

            //insert high, low, carriage return, and line feed into result
            bytes[(bSize)] = (byte)high;
            bytes[(bSize + 1)] = (byte)low;
            bytes[(bSize + 2)] = (byte)CR;
            bytes[(bSize + 3)] = (byte)LF;

            //return result byte array with input, LRC, CR, LF
            return bytes;
        }
    }
}

解决方案


这篇关于cmnd =“?Flow"之间没有区别.和“!Flow"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 18:32