_____ http://www.guffa.com 嗯... 从我上一次的尝试来看,我觉得我的距离越来越近了,不过我认为我的体型只是一点点。 所以我的所有字符串都认为我需要将它们转换为Char()数组,因为它们在结构中只是字符而不是 ''标准''告诉它多久或结束的方式,因为它取决于我正在谈论的设备。所以我需要在那里拥有 潜在字符串的最大长度。 是否有一种简单的方法来复制结构或常规对象的内容 填充到MemoryStream?我之前做的是像 这样的东西: 这是用于解析来自 来自TCP,_SQLCLientData是Bytestream。 因此,为了访问字节流中的数据,我必须索引所有内容 和确切地知道它在哪里。如果我可以将字节流重叠到 结构,我就不必知道参数3是@(256 +(2 * 52)+ 3)。更确切地说,使用:DataToSend.PARMS(2).Data_value 对于i as Integer = 0到19 PLCParms(i).PLCDataType = _SQLCLientData(256 +(i * 52)) Console.Write(" Type:"& PLCParms(i).PLCDataType.ToString&", 价值: ") ''设置SQL参数 Dim mySQLParm As SqlParameter = New SqlParameter mySQLParm.ParameterName =" @ parm" &安培; (i + 1).ToString Select Case PLCParms(i).PLCDataType 案例0 Console.WriteLine(" ; NIL") 案例1 PLCParms(i).PLCParmBool = CType((_ SQLCLientData(256 +(i * 52)+ 3)= 1),布尔值) mySQLParm.SqlDbType = SqlDbType.Bit mySQLParm.Value = PLCParms(i).PLCParmBool 控制台.WriteLine(PLCParms(i).PLCParmBool.ToString) " G?ran Andersson" < gu *** @ guffa.com写信息 新闻:ex ************* @ TK2MSFTNGP02.phx.gbl ... Scott Townsend写道: >所以我需要和一个需要所有位和字节的设备交谈将它发送到特定的地方(尚未100%定义)。 我想创建一个包含所有数据的结构/类,然后转换它到一个字节数组,传递给设备,然后得到一个回复​​,然后将其转换为结构。 我在确定我编码的问题上遇到了问题是正确的。不能弄清楚如何在结构中定义一个固定长度的数组。有人可以告诉我,我是不是想要做什么? 8< >> DataReceived似乎不是我的预期,尽管转换它回到一个结构似乎工作。大小也总是12? 谢谢, Scott< - 试图让数据以特定的方式存储在内存中有点难度,特别是当你尝试将字符串之类的对象作为内联数据放置时 。在一个结构中。 我只想用一个MemoryStream和一个BinaryWriter来写数据 它需要的方式。 - G?跑Andersson _____ http://www.guffa.com So I need to talk to a devices that expects all of the bits and bytes I sentit to be in specific places (not yet 100% defined).I wanted to create a structure/class with all of the data in it and thenconvert that to a byte array, pass it to the device, then get a reply andthen convert that to a structure.I''m having issues with making sure what I''ve coded is correct. Cant figureout how to define an array in structure that is a Fixed length.Could someone tell me if I''m way off in what I''m trying to do?Public Sub initpacketData(ByRef myPacketData As PacketData)myPacketData.MaxCmdLength = 254myPacketData.CmdLength = 0System.Array.Resize(CType(myPacketData.SQLCmd, Char()), 254)System.Array.Resize(CType(myPacketData.padding, Char()), 3840)''myPacketData.SQLCmd = New String(" ", 254)''myPacketData.padding = New String(" ", 3840)End Sub<StructLayout(LayoutKind.Explicit, Size:=4096, CharSet:=CharSet.Ansi)_Structure PacketData<FieldOffset(0)Dim MaxCmdLength As Byte<FieldOffset(1)Dim CmdLength As Byte<FieldOffset(2)Dim SQLCmd As String<FieldOffset(25)Dim padding As StringEnd StructureDim DataToSend As PacketDataDim DataToReceieve As PacketDataDim DataReceived As StringDim iSizeOfStruct As IntegerinitpacketData(DataToSend)initpacketData(DataToReceieve)DataToSend.MaxCmdLength = 254DataToSend.CmdLength = message.LengthDataToSend.SQLCmd = message''Set Up Our Pointer to Some MemoryDim ptrDataToSend As IntPtr = Marshal.AllocHGlobal(4096)''Copy the structure to our memory location @ pointerMarshal.StructureToPtr(DataToSend, ptrDataToSend, True)iSizeOfStruct = Marshal.SizeOf(DataToSend)DataReceived = Marshal.PtrToStringUni(ptrDataToSend, 4096)Dim ptrDataToReceieve As IntPtr = Marshal.AllocHGlobal(4096)ptrDataToSend = Marshal.StringToHGlobalUni(DataReceived)DataToReceieve = CType(Marshal.PtrToStructure(ptrDataToSend,GetType(PacketData)), PacketData)iSizeOfStruct = Marshal.SizeOf(DataToReceieve)------DataReceived does not seem to be what I expected, though converting it backto a struct seemed to work.The Size is always 12 too?Thanks,Scott<- 解决方案 Now I''m getting the Following Error:System.ArgumentException was unhandledMessage="The parameter is incorrect. (Exception from HRESULT: 0x80070057(E_INVALIDARG))"Source="mscorlib"StackTrace:at System.Runtime.InteropServices.Marshal.StructureTo Ptr(Objectstructure, IntPtr ptr, Boolean fDeleteOld) atSQLQueryClient.Form1.btnCopyStruct_Click(Object sender, EventArgs e)Here is some UPdated Code...<StructLayout(LayoutKind.Explicit, CharSet:=CharSet.Ansi, Pack:=1,Size:=54)_Structure SQL_PARMS<FieldOffset(0)Dim Data_Type As Byte<FieldOffset(1)Dim Unused As Byte<FieldOffset(4), VBFixedArray(50)Dim Data_Value As Byte()End Structure<StructLayout(LayoutKind.Explicit, CharSet:=CharSet.Ansi, Pack:=1,Size:=2048)_Structure SQL_COMMAND<FieldOffset(0)Dim PLC_Type As Int16<FieldOffset(4), VBFixedString(256)Dim SQLCmd As String<FieldOffset(260), VBFixedArray(20)Dim PARMS As SQL_PARMS()<FieldOffset(1340), VBFixedArray(708)Dim UNUSED As ByteEnd StructureDim DataToSend As New SQL_COMMANDDim DataToReceieve As New SQL_COMMANDDim DataReceived As StringDim iSizeOfStruct As IntegerDim BytesToSend() As ByteDim mySQLParms(19) As SQL_PARMS'' initpacketData(DataToSend)'' initpacketData(DataToReceieve)''Set up packet to sendDataToSend.PLC_Type = 100DataToSend.SQLCmd = txtMessage.Text''Parm1mySQLParms(0).Data_Type = PLCDataTypes.PLC_SINT16mySQLParms(0).Data_Value = (New SQLParmData_FromValue(18)).Data''Parm2mySQLParms(1).Data_Type = PLCDataTypes.PLC_STRINGmySQLParms(1).Data_Value = (New SQLParmData_FromValue("PaulDeas")).DataDataToSend.PARMS = mySQLParmsiSizeOfStruct = Marshal.SizeOf(DataToSend)''Set Up Our Pointer to Some MemoryDim ptrDataToSend As IntPtr = Marshal.AllocHGlobal(2048)''Copy the structure to our memory location @ pointer- Marshal.StructureToPtr(DataToSend, ptrDataToSend, True)''Chokes here ^^^^^^^^^^^^^iSizeOfStruct = Marshal.SizeOf(DataToSend)DataReceived = Marshal.PtrToStringUni(ptrDataToSend, 2048)BytesToSend = ConvertStringToByteArray(DataReceived)"Scott Townsend" <sc********@community.nospamwrote in messagenews:%2****************@TK2MSFTNGP04.phx.gbl...So I need to talk to a devices that expects all of the bits and bytes Isent it to be in specific places (not yet 100% defined).I wanted to create a structure/class with all of the data in it and thenconvert that to a byte array, pass it to the device, then get a reply andthen convert that to a structure.I''m having issues with making sure what I''ve coded is correct. Cant figureout how to define an array in structure that is a Fixed length.Could someone tell me if I''m way off in what I''m trying to do? Public Sub initpacketData(ByRef myPacketData As PacketData) myPacketData.MaxCmdLength = 254 myPacketData.CmdLength = 0 System.Array.Resize(CType(myPacketData.SQLCmd, Char()), 254) System.Array.Resize(CType(myPacketData.padding, Char()), 3840) ''myPacketData.SQLCmd = New String(" ", 254) ''myPacketData.padding = New String(" ", 3840) End Sub <StructLayout(LayoutKind.Explicit, Size:=4096, CharSet:=CharSet.Ansi)>_ Structure PacketData <FieldOffset(0)Dim MaxCmdLength As Byte <FieldOffset(1)Dim CmdLength As Byte <FieldOffset(2)Dim SQLCmd As String <FieldOffset(25)Dim padding As String End Structure Dim DataToSend As PacketData Dim DataToReceieve As PacketData Dim DataReceived As String Dim iSizeOfStruct As Integer initpacketData(DataToSend) initpacketData(DataToReceieve) DataToSend.MaxCmdLength = 254 DataToSend.CmdLength = message.Length DataToSend.SQLCmd = message ''Set Up Our Pointer to Some Memory Dim ptrDataToSend As IntPtr = Marshal.AllocHGlobal(4096) ''Copy the structure to our memory location @ pointer Marshal.StructureToPtr(DataToSend, ptrDataToSend, True) iSizeOfStruct = Marshal.SizeOf(DataToSend) DataReceived = Marshal.PtrToStringUni(ptrDataToSend, 4096) Dim ptrDataToReceieve As IntPtr =Marshal.AllocHGlobal(4096) ptrDataToSend = Marshal.StringToHGlobalUni(DataReceived) DataToReceieve =CType(Marshal.PtrToStructure(ptrDataToSend, GetType(PacketData)),PacketData) iSizeOfStruct = Marshal.SizeOf(DataToReceieve)------DataReceived does not seem to be what I expected, though converting itback to a struct seemed to work.The Size is always 12 too?Thanks, Scott<- Scott Townsend wrote:So I need to talk to a devices that expects all of the bits and bytes I sentit to be in specific places (not yet 100% defined).I wanted to create a structure/class with all of the data in it and thenconvert that to a byte array, pass it to the device, then get a reply andthen convert that to a structure.I''m having issues with making sure what I''ve coded is correct. Cant figureout how to define an array in structure that is a Fixed length.Could someone tell me if I''m way off in what I''m trying to do?8<>DataReceived does not seem to be what I expected, though converting it backto a struct seemed to work.The Size is always 12 too?Thanks, Scott<-Trying to make data being stored in a specific way in memory is a bittricky, especially when you try to put objects like strings as inlinedata in a structure.I would just use a MemoryStream and a BinaryWriter to write the data theway it needed to be.--G?ran Andersson_____ http://www.guffa.comHmmm...From my last attempt I think I''m m uch closer, though I think my size is abit off.So with all my strings I think I need to convert them to Char() arrays astechnically they would be in the structure as just characters and no''standard'' way to tell how long it is or where it ends as it is dependant onthe device I''m talking to. So I need to have the Max Length of thepotential strings in there.Is there a easy way to copy the contents of a structure or regular objectswith padding to the MemoryStream? What I was doing before was stuff likethis:This is a sip from the code that used to parse the byte stream that came infrom TCP, _SQLCLientData is the Bytestream.So to get access to the Data in the byte stream I had to index everythingand know exactly where it is. If I could just overlay the bytestream to astructure I would not have to know that Parameter 3 is @ (256 + (2 * 52) +3). Much rather use: DataToSend.PARMS(2).Data_valueFor i As Integer = 0 To 19PLCParms(i).PLCDataType = _SQLCLientData(256 + (i * 52))Console.Write("Type: " & PLCParms(i).PLCDataType.ToString & ",Value: ")''setup SQL ParametersDim mySQLParm As SqlParameter = New SqlParametermySQLParm.ParameterName = "@parm" & (i + 1).ToStringSelect Case PLCParms(i).PLCDataTypeCase 0Console.WriteLine("NIL")Case 1PLCParms(i).PLCParmBool = CType((_SQLCLientData(256 + (i* 52) + 3) = 1), Boolean)mySQLParm.SqlDbType = SqlDbType.BitmySQLParm.Value = PLCParms(i).PLCParmBoolConsole.WriteLine(PLCParms(i).PLCParmBool.ToString )"G?ran Andersson" <gu***@guffa.comwrote in messagenews:ex*************@TK2MSFTNGP02.phx.gbl...Scott Townsend wrote:>So I need to talk to a devices that expects all of the bits and bytes Isent it to be in specific places (not yet 100% defined).I wanted to create a structure/class with all of the data in it and thenconvert that to a byte array, pass it to the device, then get a reply andthen convert that to a structure.I''m having issues with making sure what I''ve coded is correct. Cantfigure out how to define an array in structure that is a Fixed length.Could someone tell me if I''m way off in what I''m trying to do?8<>>DataReceived does not seem to be what I expected, though converting itback to a struct seemed to work.The Size is always 12 too?Thanks, Scott<-Trying to make data being stored in a specific way in memory is a bittricky, especially when you try to put objects like strings as inline datain a structure.I would just use a MemoryStream and a BinaryWriter to write the data theway it needed to be.--G?ran Andersson_____ http://www.guffa.com 这篇关于结构/类到字节数组再返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-16 03:05