static void test1() { //write smm_header head = new smm_header(); head.command = 1; head.length = Marshal.SizeOf(typeof(Person)); ShareMemory sm = new ShareMemory(); var bytesBuffer = Struct_Transform.StructToBytes(head); Console.WriteLine("waitting for write smm_header..."); sm.Write(bytesBuffer, 0, bytesBuffer.Length); Person pp = new Person(); pp.age = 13; pp.sex = 1; bytesBuffer = Struct_Transform.StructToBytes(pp); Console.WriteLine("waitting for write Person..."); sm.Write(bytesBuffer, Marshal.SizeOf(typeof(smm_header)), bytesBuffer.Length); // read Console.WriteLine("waitting for read..."); Thread.Sleep(3000); var bytes = new byte[Marshal.SizeOf(typeof(smm_header))]; sm.Read(out bytes, 0, Marshal.SizeOf(typeof(smm_header))); smm_header head2 = Struct_Transform.BytesToStruct <smm_header>(bytes); Console.WriteLine($"command = {head2.command}, length = {head2.length}"); }
static void test_write() { //write Console.WriteLine("begin to write..."); var points = new mypoint[] { new mypoint() { x = 1, y = 1 }, new mypoint() { x = 2, y = 2 }, new mypoint() { x = 3, y = 3 }, }; List <mypoint> Tmplist2 = new List <mypoint>(); Tmplist2.AddRange(points); Person2 pp2 = new Person2() { name = "李四 abc1", address = "add2", age = 10, Tmplist1 = { 11, 20, 35 }, Tmplist2 = Tmplist2 }; // json序列化 string jsonStr = JsonConvert.SerializeObject(pp2); var StrBuffer = System.Text.Encoding.UTF8.GetBytes(jsonStr); smm_header head = new smm_header(); head.command = 1; head.length = StrBuffer.Length; // write head ShareMemory sm = new ShareMemory(); var bytesBuffer = Struct_Transform.StructToBytes(head); Console.WriteLine("waitting for write smm_header..."); sm.Write(bytesBuffer, 0, bytesBuffer.Length); // write data Console.WriteLine("waitting for write data..."); sm.Write(StrBuffer, Marshal.SizeOf(typeof(smm_header)), StrBuffer.Length); Console.WriteLine("write data finish."); }