请这种正确的形式 [英] Please is this correct formation

查看:79
本文介绍了请这种正确的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将以下数据包格式发送到计算机:-

i have to send the following packet format to a machine:-

byte     byte     bit         bit
                       2       2         1           1
                 +---------+-------+-------------+----------+
                 |  CM     |   00  |  check sum  |  CR      |
                 +---------+-------+-------------+----------+



我有以下指示:-

所有数据均为不带校验和和CR的ASCII字符代码.
计算从顶部到校验和之前的总和(1个咬合单位),然后将此1的补码识别为校验和.
格式的结尾为CR(十六进制:OD)

因此要实施此
我拿了
这是主要的.cpp



I have the following instructions:-

All data isASCII character code without checksumand CR.
Calculate sum total from top till before checksum ( 1 bite unit ),then recognize this complement of 1 as checksum.
Finish of format is CR (Hexadecimal: OD)

So to implement this
i took
This is main .cpp

char data[6]="CM00";
typedef unsigned short int word16;
word16      check1;
int main()
{
int h=2;
check1 =calc_check(hh);
	data[4]= check1;
	data[5]=0x0D;
}



这是文件校验和.cpp



this is the file checksum .cpp

typedef unsigned char      byte;    // Byte is a char
typedef unsigned short int word16;  // 16-bit word is a short int
typedef unsigned int       word32;  // 32-bit word is an int

//----- Defines ---------------------------------------------------------------
#define BUFFER_LEN        6      // Length of buffer
extern char data[6];

word16      check;  
//----- Prototypes ------------------------------------------------------------
word16 checksum(char *addr, word32 count);
//int calc_check(int);
//===== Main program ==========================================================
int calc_check(int w)
{
	//byte        buff[BUFFER_LEN]; // Buffer of packet bytes

	// 16-bit checksum value
	word32      i;                // Loop counter

	// Load buffer with BUFFER_LEN random bytes
	for (i=0; i<BUFFER_LEN; i++)
	{
		//buff[i] = (byte) rand();
		data[i]=(byte) rand();
	}

	// Compute the 16-bit checksum
	//check = checksum(buff, BUFFER_LEN);
	check = checksum(data, BUFFER_LEN);

	// Output the checksum
	printf("checksum = %04X \n", check);
	return check;

}

//=============================================================================
//=  Compute Internet Checksum for count bytes beginning at location addr     =
//=============================================================================
word16 checksum(char *addr, word32 count)
{
	register word32 sum = 0;
	word16 * waddr = reinterpret_cast<word16*>(addr);//added
	// Main summing loop
	while(count > 1)
	{
		//sum = sum + *((word16 *) addr)++;//main
		sum = sum + *waddr++;
		//sum += *((word16 *) addr)++;
		count = count - 2;
	}

	// Add left-over byte, if any
	if (count > 0)
		sum = sum + *((byte *) addr);

	// Fold 32-bit sum to 16 bits
	while (sum>>16)
		sum = (sum & 0xFFFF) + (sum >> 16);

	return(~sum);
}


我认为我在checksum.cpp中做错了,因为我给出了以下语句
计算从顶部到校验和之前的总和,因此根据此语句,我应该做


I think i am doing something wrong in checksum.cpp as i have given the statement
Calculate sum total from top till before checksum so acc to this statement i should do

for (i=0; i<2; i++)
    {
        //buff[i] = (byte) rand();
        data[i]=(byte) rand();
    }


那我做对了吗


Then am i doing this correct

data[4]= check1;
	data[5]=0x0D;

推荐答案

否.
计算校验和时,将覆盖购买者中的所有内容,然后将所有字符包括在消息中:
No.
When you calculate your checksum, you overwrite everything in the buyffer, and then you are including all the characters in the message:
#define BUFFER_LEN        6      // Length of buffer
extern char data[6];
int calc_check(int w)
{
...
	// Load buffer with BUFFER_LEN random bytes
	for (i=0; i<BUFFER_LEN; i++)
	{
		data[i]=(byte) rand();
	}

	// Compute the 16-bit checksum
	check = checksum(data, BUFFER_LEN);
... 
}

因为循环将覆盖调用之前加载的数据.
然后,您尝试将校验和字节本身以及CR终止符包括在内!

而且这忽略了您的代码将无法编译,因为"h"和"hh"是不一样的...


为什么不将缓冲区和长度交给检查例程,然后让它返回检查字节?如果这样做,则可以对传入消息和传出消息使用相同的功能...

Since the loop will overwrite the data you loaded before you called it.
Then you try to include the checksum byte in itself, and the CR terminator!

And that''s ignoring that your code won''t compile, because "h" and "hh" are not the same...


Why not hand the buffer and a length to the check routine, and have it return the check byte? If you did that, you could use the same function for incoming messages as well as outgoing...


这篇关于请这种正确的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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