Detailed explanation of the support of weaving-socket (Internet of Things, industrial equipment) binary byte communication protocol

The code to get the address of the architecture

http://t.cn/R5pS8up

 

Detailed explanation of the (binary) communication protocol of the IoT client

This article explains how to write a device language whose architecture does not provide a client module and what protocol standards should be used to write the transport content.

And how the server parses and sends these content.

1. Detailed protocol

header instruction

length of N segment

N segment

content

1bit (fixed length)

1bit (fixed length)

Unfixed length

Unfixed length

 

Head command:

From 0x00-0xff, where 0xff and 0x99 are reserved fields and shall not be used.

The length of the N segment:

 It is the length of the data bits of the N segment. For example, the data of the N segment is FF, and this data occupies 1 bit, then the length of the N segment is 1. If the N segment data is, 0x100~0xffff, this data occupies 2BIT, then The length of the N segment is 2,

If the data of N segment is 0x10000~0xffffff, it occupies 3 bits, and the length of N segment is 3, and so on.

N segment:

1 This segment of data is the length of the content data. If the content is 1BIT, the value of the N segment is 1. If the content is N bits, the N segment data is N.

content:

This section can define the desired content by itself, which can be hexadecimal content or other format content. The form is not fixed and the content is not fixed.

(In the production of IoT projects, at least the device ID should be reflected in the content)

 

 

2 special agreement

Heartbeat packets: Heartbeat packets are sent from the server, so the client does not need to send heartbeat packets.

The client needs to accept the heartbeat packets and filter the heartbeat packets.

The heartbeat packet is single-byte 0x99. If the client encounters single-byte 0x99, it needs to adjust the receiving time to the latest. The reception time is also updated when non-heartbeat packets are received.

If the client does not update the latest reception time for more than 60-90 seconds, it considers the connection timed out and needs to reconnect.

Built-in directives:

0xff, it is a built-in command for the service, the command header contains the function of

1. The service is full and needs to connect to other services,

2. Obtain the tokenID of the intermediate layer. If the intermediate forwarding layer is used, the tokenid is the unique identifier. Otherwise, the information cannot be received without forwarding.

3. Other notices or content of the server. (customizable)

 

 

Example

A brief description is:

The header instruction represents a CLASS class. Each time a header instruction is defined, the background processing corresponds to a CLASS

, I here for convenience, all defined as 0X01, that is, the background only needs to use a CLASS to process.

length of N segment

N segment

All are obtained by calculation and are not fixed.

The content can be freely defined.

The first two bytes I define here are defined as specific methods. You can think of it as a method that your backend handles specific calls of the CLASS class.

 

Login (sent by client):

header instruction

length of N segment

N segment

content

0x01

By calculating the length of N segments

By calculating the length of the content

(Fixed 2BIT - represents the login method of the backend)

0X000A

(N bit - device ID number)

For example, the device ID number is a fixed 8-bit 4BIT

0x00000001

 

Example of login combination:

0x01 (header command) 0X01 (length of 0x0C) 0X6 (length of following content, decimal is 12) 0X000A00000001

 

Return authentication and token (received by client):

header instruction

length of N segment

N segment

content

0x01

By calculating the length of N segments

By calculating the length of the content

(Fixed 2BIT - represents the login method of the backend)

0X000A

(N bit - the returned TOKEN)

For example fixed 8-bit 4BIT

0x10000011

 

Example:

0x01 (header command) 0X01 (length of 0x0C) 0X6 (length of following content, decimal is 12) 0X000A10000011

 

 

Query (client/server-send):

header instruction

length of N segment

N segment

content

0x01

By calculating the length of N segments

By calculating the length of the content

(fixed 2BIT - some method representing the query)

0X00A1

(N bit - login token TOKEN)

For example fixed 8 bits

0x10000011

 

 

Query result (client/server-receive):

header instruction

length of N segment

N segment

content

0x01

By calculating the length of N segments

By calculating the length of the content

(fixed 2BIT - some method representing the query)

0X00A1

(N bit - the result returned)

Can be the binary form of UTF-8 characters. Can be a self-defined value

 

 

 

 

Heartbeat: The server architecture comes with it, no need to define:

The client only needs to follow the special protocol and the analysis method of the heartbeat packet.

3 Packet Examples

Among them, ConvertToByteList converts the INT type into a long BTYE[] array, and then obtains the length of the array (placeholder).

Very simple assembly. 2 is the fixed 2BIT of the header, one is the header instruction, and the other is the length of the N segment.

 

 

Unpacking example

If the first bit of data received is 0X99, it will be eliminated, and then other contents will be added to the waiting unpacking queue. Update the reception time to prevent errors.

 

unpacking queue method

find the first bit is 0x99, then remove it

If after filtering, the characters are greater than 2, see how long the content of the entire protocol is

If the current value is too small, the next group of data in the queue is merged into the back of this group, if

If the current value is too large, the excess value will be merged to the front of the next group of data. Then store the current value in a temporary variable. Remove this set of data from the queue.

Finally, the correct data is divided into header instructions and specific content.

 

 

   How to use the server to send by bytes

 

When using the server-side architecture first, you must first understand the basic usage, you can refer to

https://my.oschina.net/u/2476624/blog/893783

content of this tutorial.

Again basics, just a few small changes.

In the latest update of the runner, select BYTESCOKET, which means SOCKET sent in bytes.

after selection

You will need to fill in a complete namespace and class name.

, you can create a class file in the runner project

The created class needs to inherit the IDataparsing interface. This interface has 4 methods to be implemented, you can refer to

 

Finally, the back-end processing logic is the same as

https://my.oschina.net/u/2476624/blog/893783

It's exactly the same.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325444011&siteId=291194637