cmnd =“?Flow"之间没有区别.和“!Flow" [英] no difference between cmnd = "?Flow" and "!Flow"

查看:92
本文介绍了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的结果字节数组
           返回字节;
        }
    }
}

解决方案

尚不清楚 cmdn 数组是否包含是否不是最后一个零字节.并且重写该函数可能更容易.检查下一个实现:

公共静态字节[] CalcLRC(字节[]命令)
{
    int len = command.Length;

    //如果'command'包含零终止符,则取消注释下一行:
    //-len;

    字节[]输出=新字节[len + 5];
    字节lrc = 0;

    for(int i = 0; i< len; ++ i)未选中{lrc + =(output [i] = command [i]); }

    lrc =未选中((byte)-(sbyte)(lrc));

    output [len + 0] =(字节)('0'+((lrc> 4)& 0x0F));
    输出[len + 1] =(byte)('0'+(lrc& 0x0F));
    输出[len + 2] = 0x0D;
    输出[len + 3] = 0x0A;
    输出[len + 4] = 0;

    返回输出;
}



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;
        }
    }
}

解决方案

It is not clear whether the cmdn array contains or not the last zero byte. And it is probably easier to rewrite the function. Check the next implementation:

public static byte[] CalcLRC( byte[] command )
{
    int len = command.Length;

    // Uncomment the next line if 'command' contains zero terminator:
    //--len;

    byte [] output = new byte[len + 5];
    byte lrc = 0;

    for( int i = 0; i < len; ++i ) unchecked { lrc += ( output[i] = command[i] ); }

    lrc = unchecked((byte)-(sbyte)( lrc ));

    output[len + 0] = (byte)( '0' + ( ( lrc >> 4 ) & 0x0F ) );
    output[len + 1] = (byte)( '0' + ( lrc & 0x0F ) );
    output[len + 2] = 0x0D;
    output[len + 3] = 0x0A;
    output[len + 4] = 0;

    return output;
}



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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆