Differences between x86 system and Siemens plc200 In the byte sequence

Source: Internet
Author: User

Differences between x86 system and Siemens plc200 In the byte sequence

What is byte order? This also starts from binary. A single byte (8 bits) can only represent 0 ~ The decimal number of 255 (just taking the unsigned char as an example, we will not discuss signed Char for the time being. In fact, the principle is the same). What should we do to indicate a number greater than 255? The solution is to use two bytes or four bytes to represent the decimal number, which has the word and DWORD, and the current memory is compiled by byte, so I encountered a problem. When I store words or double words in the memory, should I put the low byte at the low address or the low byte at the high address? For example, if you want to store a word AABB (hexadecimal) in the memory, and now two consecutive bytes in the memory are given to you, and the addresses are respectively 1234,1235, then you have two storage methods, right? Method 1: store AA in 1234 bytes of memory and BB in 1235 bytes of memory. Method 2: store BB in 1234 bytes of memory address and AA in 1235 bytes of memory address. That is to say, the first method is to store the high byte in the low address of the memory, and the low byte in the high address. This is the byte order before the high level, and the Siemens plc200 adopts this byte order; the second method is to store the low byte in the low address of the memory, and the high byte in the high address of the memory. This is the byte order before the low bit, all the computers based on x86 systems (that is, our commonly used home computers) adopt this sort of byte order.

Big endian and little endian are used in English to indicate that the upper and lower levels are the former.

The word "endian" comes from Gulliver Travel Notes. The civil war in the minor people's country originated from the fact that the big-Endian attack or the little-Endian attack were initiated when the eggs were eaten. As a result, there were six rebels, one of the emperors gave life and the other lost the throne.

We generally translate endian into "byte order".

Although we can find from reference materials that the x86 system's microcomputer adopts the low-front byte order, and the Siemens plc200 adopts the High-front byte order. However, if we can write a program and verify the byte sequence of the above two systems, it will not be fast. OK, start now.

Verify the x86 system first:

Open VB, drag the control (command1), and type the following code:
-----------------------------------------------------------------------
Option explicit

Private declare sub copymemory lib "Kernel32" alias "rtlmovememory "_
(Destination as any, source as any, byval length as long)

Private sub form_load ()
Dim I as long
Dim BYT as byte

I = & haabbccdd

Copymemory BYT, byval varptr (I), 1

Debug. Print hex (BYT)

Copymemory BYT, byval varptr (I) + 1, 1

Debug. Print hex (BYT)

Copymemory BYT, byval varptr (I) + 2, 1

Debug. Print hex (BYT)

Copymemory BYT, byval varptr (I) + 3, 1

Debug. Print hex (BYT)

End sub

-----------------------------------------------------------------------

In VC ++, open VC ++, create a dialog box-based project, and drag a button control,
In the message processing function of the button, enter the following code:
-----------------------------------------------------------------------
Void cbyteorderdlg: onbutton1 ()
{
// Todo: add your control notification handler code here
Int I;
Unsigned char * lp_ I;
Cstring CSTR;

I = 0 xaabbccdd;
Lp_ I = (unsigned char *) & I;

CSTR. Format ("% x", * lp_ I );
Afxmessagebox (CSTR );
 
CSTR. Format ("% x", * (lp_ I + 1 ));
Afxmessagebox (CSTR );

CSTR. Format ("% x", * (lp_ I + 2 ));
Afxmessagebox (CSTR );

CSTR. Format ("% x", * (lp_ I + 3 ));
Afxmessagebox (CSTR );

}
-----------------------------------------------------------------------
Run the code. We can see that the byte dd of the maximum bit is stored at the lowest address, and so on. The highest byte AA exists at the highest address.
From this, we can verify that the x86 system's microcomputer does adopt the low-level prior byte order.

Finally, to verify the Siemens PLC S7-200 in the byte order, open the step7-MicroWin, knock into the following code:
-----------------------------------------------------------------------
LD sm0.0
Movw 16 # ff00, qw0 // The byte order of the words, we will find that qb0 all lights are on, And QB1 all lights are off. This indicates that the byte order in the PLC is high before (low address)

LD sm0.0
Movd 16 # aabbccdd, vd0 // you can use this program to monitor the value of vb0 -- vb3 after running.
-----------------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.