-
Notifications
You must be signed in to change notification settings - Fork 601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Two byte alignment issue in the structure #534
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,12 +9,12 @@ namespace S7.Net.Types | |||||||||||||||
/// </summary> | ||||||||||||||||
public static class Struct | ||||||||||||||||
{ | ||||||||||||||||
/// <summary> | ||||||||||||||||
/// Gets the size of the struct in bytes. | ||||||||||||||||
/// </summary> | ||||||||||||||||
/// <param name="structType">the type of the struct</param> | ||||||||||||||||
/// <returns>the number of bytes</returns> | ||||||||||||||||
public static int GetStructSize(Type structType) | ||||||||||||||||
/// <summary> | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: keep the spaces, so undo this change. |
||||||||||||||||
/// Gets the size of the struct in bytes. | ||||||||||||||||
/// </summary> | ||||||||||||||||
/// <param name="structType">the type of the struct</param> | ||||||||||||||||
/// <returns>the number of bytes</returns> | ||||||||||||||||
public static int GetStructSize(Type structType) | ||||||||||||||||
{ | ||||||||||||||||
double numBytes = 0.0; | ||||||||||||||||
|
||||||||||||||||
|
@@ -24,10 +24,15 @@ public static int GetStructSize(Type structType) | |||||||||||||||
#else | ||||||||||||||||
.GetFields(); | ||||||||||||||||
#endif | ||||||||||||||||
|
||||||||||||||||
foreach (var info in infos) | ||||||||||||||||
int count = 0; | ||||||||||||||||
foreach (var info in infos) | ||||||||||||||||
{ | ||||||||||||||||
switch (info.FieldType.Name) | ||||||||||||||||
count++; | ||||||||||||||||
var type = info.FieldType; | ||||||||||||||||
string name = info.FieldType.Name; | ||||||||||||||||
if (type.BaseType == typeof(System.Enum)) | ||||||||||||||||
name = Enum.GetUnderlyingType(type).Name; | ||||||||||||||||
switch (name) | ||||||||||||||||
{ | ||||||||||||||||
case "Boolean": | ||||||||||||||||
numBytes += 0.125; | ||||||||||||||||
|
@@ -75,11 +80,15 @@ public static int GetStructSize(Type structType) | |||||||||||||||
break; | ||||||||||||||||
default: | ||||||||||||||||
numBytes += GetStructSize(info.FieldType); | ||||||||||||||||
if (count != infos.Count()) { | ||||||||||||||||
if(numBytes % 2 != 0) | ||||||||||||||||
numBytes++;//word align | ||||||||||||||||
Comment on lines
+83
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a block of the PLC, two structures A and B are defined. Structure A occupies 3-byte space, and structure B also occupies 3-byte space. However, in reality, structure B starts from the fourth byte because the PLC is two-byte aligned internally. 当在PLC一个块中,定义一个结构体A,B,其中A占用3个字节空间,B也占3个字节空间,但实际上B是从第4字节开始的,因为PLC内部是两字节对齐的。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The above code has been verified by the project and the test is normal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My suggestion is about code style (see the spacing and newline). |
||||||||||||||||
} | ||||||||||||||||
break; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
return (int)numBytes; | ||||||||||||||||
} | ||||||||||||||||
return (int)Math.Ceiling(numBytes); //For example: 9 bool data, length = 1.125, but actually occupies 2 bytes | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/// <summary> | ||||||||||||||||
/// Creates a struct of a specified type by an array of bytes. | ||||||||||||||||
|
@@ -111,7 +120,11 @@ public static int GetStructSize(Type structType) | |||||||||||||||
|
||||||||||||||||
foreach (var info in infos) | ||||||||||||||||
{ | ||||||||||||||||
switch (info.FieldType.Name) | ||||||||||||||||
var type = info.FieldType; | ||||||||||||||||
string name = info.FieldType.Name; | ||||||||||||||||
if (type.BaseType == typeof(System.Enum)) | ||||||||||||||||
name = Enum.GetUnderlyingType(type).Name; | ||||||||||||||||
switch (name) | ||||||||||||||||
{ | ||||||||||||||||
case "Boolean": | ||||||||||||||||
// get the value | ||||||||||||||||
|
@@ -238,6 +251,8 @@ public static int GetStructSize(Type structType) | |||||||||||||||
Buffer.BlockCopy(bytes, (int)Math.Ceiling(numBytes), buffer, 0, buffer.Length); | ||||||||||||||||
info.SetValue(structValue, FromBytes(info.FieldType, buffer)); | ||||||||||||||||
numBytes += buffer.Length; | ||||||||||||||||
if(numBytes %2 != 0) //word align | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When no user-defined structure is in a block of the PLC, there are no such problems. When a structure exists, there is a two-byte alignment issue. 当在PLC一个块中没有自定义结构体时,这些没有问题,当有结构体存在时,就有2字节对齐问题。 |
||||||||||||||||
numBytes++; | ||||||||||||||||
break; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know the policy for supporting older targets, but the same could apply to .NET 5 too.
My feeling is that for PLCs quite a lot of older targets are around, because they run and an running system isn't touched (in that area).
So maybe NS 1.3 should be kept too (except everything from these API are covered by net452 or net462 -- which I don't know as NS 1.3 is soo old 😉).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, this is not necessary. This happened when I was testing another code and the compilation failed. Later, I forgot to restore it.