Home > Software engineering >  Communication protocol
Communication protocol

Time:02-20

This is a control card communication protocol

Protocol format:
Byte header (2 -) + function code (1 -) + byte length (1 -) + data bits (nbit) + parity bit (2) + word goeldi (2 -)

Byte header: C1 C2
Word goeldi: 0 a 0 d
Data bytes (1 -)=(nbit) + parity bit (2)

Check digit:
Calculation method: first the length byte header + function code + + data a cumulative sum, accumulate the value of the 16 bit high value added to the low 16 bits, that is the accumulation of highest carry to its lowest level, finally to invert,

Can make the output Settings: (check digit 2 byte) are shown in brackets
Issued by: C1 C2 08 04 xx aa (00 00 0 a 0 d (xx: 1:1-4 X 2: Y 3: Z axis 4: B axis aa: 0 output low level 1) output level
Returns: C1 C2 08 00 04 xx (00 00 0 a 0 d
Note: a command parsing failure: C9 C8 FF 02 (00 00 0 a 0 d


How do I write control according to the above these data can make switch function of the driver?

CodePudding user response:

Void CCard: : Off (short axis)
{
//C1 C2 08 04 xx aa (00 00 0 a 0 d (xx: 1:1-4 X 2: Y 3: Z axis 4: B axis aa: zero output low level 1 output high level)
Int a;
If (axis==1)
A=0 xc1c2080401000a0d;
Else if (axis==2)
A=0 xc1c2080402000a0d;
Else if (axis==3)
A=0 xc1c2080403000a0d;
Else if (axis==4)
A=0 xc1c2080404000a0d;
}
//the above I wrote to you, then how to do, do not know how to start

CodePudding user response:

Issued by: C1 C2 08 04 xx aa (00 00 0 a 0 d (xx: 1:1-4 X 2: Y 3: Z axis 4: B axis aa: 0 output low level 1) output level
Returns: C1 C2 08 00 04 xx (00 00 0 a 0 d

Issued, the returns are respectively in where? How do you use the check digit in the bracket? I then delete the check digit in the bracket above right?

CodePudding user response:

Are the corresponding bytes, you write directly to the corresponding inside, generally don't use such a variable to hold the, one is a package had 8 bytes, 16 bytes, another reason is some packet length,
Unsigned char * package=new unsigned char [10].
Package [0]=0 xc1;
Package [1]=0 xc2;
Package [3]=0 x08;
.
.
.

CodePudding user response:

Then how come out 0 xc1c2080401000a0d this string of Numbers, and the check digit in the bracket, and returns the is how to return a responsibility

CodePudding user response:

It is as you say so?
Unsigned char * package=new unsigned char [10].
Package [0]=0 xc1;
Package [1]=0 xc2;
Package [2]=0 x08;
Package [3]=0 x04;
Package [4]=0 x00;
Package [5]=0 x00 + axis;
Package [6]=0 x00;
Package [7]=0 x00;//check digit
Package [8]=0 x0a;
Package [9]=0 x0d;

Check digit is to also write in it?

CodePudding user response:

You carefully look at instructions, packet generally include: head, length, calibration, tail sign, you here at the end of which is to enter a new line, check should have is how to calculate, is the most simple xor check and CRC

CodePudding user response:

The communication protocol assembled into an array of bytes to use

Don't be so use 0 xc1c2080401000a0d computer or microcontroller can't represent special big number

CodePudding user response:

Data bytes (1 -)=(nbit) + parity bit (2) it should be a bit, and should be byte bytes

Check two bytes

CodePudding user response:

Calibration method of know, but I don't know how to use the

Check digit:
Calculation method: first the length byte header + function code + + data a cumulative sum, accumulate the value of the 16 bit high value added to the low 16 bits, that is the accumulation of highest carry to its lowest level, finally to invert,

This is a routine:
Under-16 checksum (u8 * buffer, under-16 size)
{
U32 cksum=0;
U8 I=0;
for(i=0; i{
Cksum +=* buffer++;
}
Cksum=(cksum & gt;> 16) + (cksum & amp; 0 XFFFF);
Cksum +=(cksum & gt;> 16);
Return ~ cksum;
}

CodePudding user response:

refer to the eighth floor yeah2000 response:
bytes (1 -)=data bits (nbit) + parity bit (2) it should be a bit, and should be byte bytes

Check two bytes


Uh huh, the learned, now with this method the
Unsigned char * package=new unsigned char [10].
Package [0]=0 xc1;
Package [1]=0 xc2;

CodePudding user response:


Cs=under-16 checksum (data, size);

Calculate the check digit after according to the requirements for assignment
Such as check the results after 0 xaabb
The
Big end mode
Package [n]=cs> 8;//check digit high
Package [n + 1)=cs& 0 XFF.//check digit position

The small end mode
Package [n]=cs& 0 XFF.//check digit high
Package [n + 1)=cs> 8;//check digit position

CodePudding user response:

Package [n]=cs& 0 XFF.//check digit high
Package [n + 1)=cs> 8;//check digit position

It used to do

CodePudding user response:

Check the result is unsigned int16, 2 bytes, save with memcpy call stack redundant waste of time, so directly in a save operation take out the 2 bytes to an array,

CodePudding user response:

Package [n]=cs& 0 XFF.
Package [n + 1)=cs> 8;

Check digit is the two package?

11 references yeah2000 response:
under-16 cs=checksum (data, size);

Calculate the check digit after according to the requirements for assignment
Such as check the results after 0 xaabb
The
Big end mode
Package [n]=cs> 8;//check digit high
Package [n + 1)=cs& 0 XFF.//check digit position

The small end mode
Package [n]=cs& 0 XFF.//check digit high
Package [n + 1)=cs> 8;//check digit position

CodePudding user response:

General definition to byte stream communication protocol between equipment, mainly because of different CPU architecture/compiler there are differences between the storage size end

 

//send data frame
Int TransFrame (BYTE bFun/* */function,
BYTE bDataLen/* */data length,
BYTE * pbData/* */data)
{
//note here * * * * * * * * * (choose big end according to agreement, the small side)
# define _BIG_ENDIAN//big end model?

//check parameter
ASSERT (bDataLen & gt; 0);
ASSERT (pbData);
If (bDataLen==0 | | pbData=https://bbs.csdn.net/topics/=NULL)
return 0;

//total target send length
Int iTxLen=0
+ 2//byte header
+ 1//function code
+ 1//bytes
+ bDataLen//data length
+ 2//check
+ 2//word goeldi
+ 0;

//application buffer
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related