写入.NET中的内存,并在Labview中获取进程数据

本文关键字:Labview 获取 取进程 并在 数据 NET 内存 写入 | 更新日期: 2023-09-27 18:35:33

我正在使用 Kinect 面部跟踪基础知识 WPF 示例从对象收集数据。我可以访问文本文件等中的数据,但我想将数据写入C#进程中的托管内存,并让Labview的.NET程序从同一位置获取数据。

到目前为止,这就是我得到的:

                    this.facePoints3D = frame.Get3DShape();
                   // using (MemoryStream stream = new MemoryStream())
                   // {
                        //var sw = new StreamWriter(stream);
                        int n = 121;
                        foreach (Vector3DF[] vector in facePoints3D.GetSlices(n))
                        {
                            //convert from float to byte array before we pass on to memory
                            var bytearray = new byte[vector.Length * this.facePoints3D.Count];
                            Buffer.BlockCopy(vector, 0, bytearray, 0, bytearray.Length);
                           //Initialize unmanaged memory to hold array.
                            int size = Marshal.SizeOf(bytearray[0]) * bytearray.Length;
                            IntPtr pnt = Marshal.AllocHGlobal(size);
                            try
                            {
                                //copy the array to unmanaged memory.
                                Marshal.Copy(bytearray, 0, pnt, bytearray.Length);
                                // Copy the unmanaged array back to another managed array.
                                byte[] bytearray2 = new byte[bytearray.Length];
                                Marshal.Copy(pnt, bytearray2, 0, bytearray.Length);
                                //Console.WriteLine("The array was coppied to unmanaged memory and back.");
                            }
                            finally
                            {
                                // Free the unmanaged memory.
                                Marshal.FreeHGlobal(pnt);
                            }

}

到目前为止,我已经将Labview程序正确配置为

进程 A (c#): 内存流缓冲区 -> 元帅分配 ->封送副本 ->封送释放 -> IntPtr ToInt64过程 B(Labview):IntPtr 值 -> 马歇尔分配 HGlobal -> 元帅副本 -> 目标

现在,labview端运行良好,但它似乎没有从内存位置获取值。

请指教?

写入.NET中的内存,并在Labview中获取进程数据

这两个过程是独立的吗?(2个独立的前任)。 如果是这样,由于进程隔离(一个进程看不到另一个进程的内存),您将无法通过直接分配共享内存。

假设您的答案为"是"(2 个单独的进程),请考虑使用命名管道进行跨进程通信(或使用 WCF 将其包装)

与命名管道的进程间通信:http://msdn.microsoft.com/en-us/library/bb546085(v=vs.110).aspx

WCF 教程:基本进程间通信:http://tech.pro/tutorial/855/wcf-tutorial-basic-interprocess-communication

您还可以使用内存映射文件:http://www.abhisheksur.com/2012/02/inter-process-communication-using.html

编辑

添加了使用二进制序列化程序而不是执行结构的手动内存副本的示例。 您可以将生成的字节数组写入内存映射文件。 此解决方案确实要求您将 [Serializable] 属性应用于 Vector3DF 结构,并假定读取内存的代码具有与 Vector3DF 相同的类型定义。

(注意:在注释中的代码中,看起来好像您正在使用 Vector3DF 结构数组数组,因此这就是我对序列化代码进行建模的方式。 根据需要进行调整


        public byte[] SerializeVectors(Vector3DF[][] vectors)
        {
            var formatter = new BinaryFormatter();
            // note: if you are using a stream to write to the memory mapped file, 
            // you could pass it in instead of using this memory stream as an intermediary
            using (var stream = new MemoryStream())
            {
                formatter.Serialize(stream, vectors);
                return stream.ToArray();
            }
        }
        public Vector3DF[][] DeserializeVectors(byte[] vectorBuffer)
        {
            var formatter = new BinaryFormatter();
            using (var stream = new MemoryStream(vectorBuffer, false))
            {
                return (Vector3DF[][])formatter.Deserialize(stream);
            }
        }

下面是一个包含工作代码和单元测试的 Gist 的链接,以便您可以使用它: https://gist.github.com/jsmarsch/d0dcade8c656b94f5c1c