Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当结构包含字节变量时,如何避免字节过多?(VB.NET)_.net_Vb.net_Byte - Fatal编程技术网

当结构包含字节变量时,如何避免字节过多?(VB.NET)

当结构包含字节变量时,如何避免字节过多?(VB.NET),.net,vb.net,byte,.net,Vb.net,Byte,VB.NET 4.5 我定义了一个包含单个字节的结构。我需要通过串口发送一个字节数组 Public Structure ExampleStructure Public variable1 As Byte Public variable2 As UInt16 Public variable3 As UInt16 Public Function getBytes() As Byte() Dim binaryBytes(5) As Byte

VB.NET 4.5

我定义了一个包含单个字节的结构。我需要通过串口发送一个字节数组

Public Structure ExampleStructure
    Public variable1 As Byte
    Public variable2 As UInt16
    Public variable3 As UInt16

    Public Function getBytes() As Byte()
        Dim binaryBytes(5) As Byte
        Dim pointerCommand As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Me))
        Marshal.StructureToPtr(Me, pointerCommand, False)
        Marshal.Copy(pointerCommand, binaryBytes, 0, Marshal.SizeOf(Me))
        Marshal.FreeHGlobal(pointerCommand)
        Return binaryBytes
    End Function
End Structure
问题是:

当我使用
Marshal.AllocHGlobal
Marshal.StructureToPtr
Marshal.Copy
时,返回的字节数组是6个字节。NET为variable1创建2个字节,因此variable1和variable2数据之间有多余的字节

我可以通过使用
LayoutKind.Explicit
和定义fieldoffset来解决这个问题

<StructLayout(LayoutKind.Explicit)> _
Public Structure ExampleStructure
    <FieldOffset(0)> Public variable1 As Byte
    <FieldOffset(1)> Public variable2 As UInt16
    <FieldOffset(3)> Public variable3 As UInt16

    Public Function getBytes() As Byte()
        Dim binaryBytes(5) As Byte
        Dim pointerCommand As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Me))
        Marshal.StructureToPtr(Me, pointerCommand, False)
        Marshal.Copy(pointerCommand, binaryBytes, 0, Marshal.SizeOf(Me))
        Marshal.FreeHGlobal(pointerCommand)
        Return binaryBytes
    End Function
End Structure
_
公共结构示例结构
公共变量1作为字节
公共变量2作为UInt16
公共变量3作为UInt16
公共函数getBytes()作为字节()
Dim二进制字节(5)作为字节
Dim pointerCommand As IntPtr=Marshal.AllocHGlobal(Marshal.SizeOf(Me))
Marshal.StructureToPtr(Me,pointerCommand,False)
Copy(指针命令,二进制字节,0,Marshal.SizeOf(Me))
FreeHGlobal元帅(pointerCommand)
返回二进制字节
端函数
端部结构
现在当我得到字节时,variable1和variable2之间不再有多余的字节


不过,这似乎是一种笨拙的方式。有没有更好的选项,我不必手动设置FieldOffset?此结构很简单,但可能会变得更复杂。

修复非常简单-只需添加标记LayoutKind。Sequential,Pack:=1

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Public Structure ExampleStructure
    Public variable1 As Byte
    Public variable2 As UInt16
    Public variable3 As UInt16

    Public Function getBytes() As Byte()
        Dim binaryBytes(5) As Byte
        Dim pointerCommand As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Me))
        Marshal.StructureToPtr(Me, pointerCommand, False)
        Marshal.Copy(pointerCommand, binaryBytes, 0, Marshal.SizeOf(Me))
        Marshal.FreeHGlobal(pointerCommand)
        Return binaryBytes
    End Function
End Structure
_
公共结构示例结构
公共变量1作为字节
公共变量2作为UInt16
公共变量3作为UInt16
公共函数getBytes()作为字节()
Dim二进制字节(5)作为字节
Dim pointerCommand As IntPtr=Marshal.AllocHGlobal(Marshal.SizeOf(Me))
Marshal.StructureToPtr(Me,pointerCommand,False)
Copy(指针命令,二进制字节,0,Marshal.SizeOf(Me))
FreeHGlobal元帅(pointerCommand)
返回二进制字节
端函数
端部结构

编译器正试图通过对齐16位边界上的数据来优化代码。因此,您正在与编译器优化器进行斗争。如今,当代码在更宽的边界上对齐时,微处理器运行得更快。不确定是否有编译器选项强制代码在字节边界上对齐。Try:LayoutKind.sequentials我刚试过LayoutKind.sequentials,但运气不好。Try-pack选项:[StructLayout(LayoutKind.sequentials,pack=8)]是神奇的标签,谢谢!