private void SerialDataHandler(SerialData.SerialHeader Header, byte[] messageBuffer, UInt32 NumBytes) { switch (Header.Type) { case '@': Console.WriteLine("Got bytes read header: " + Header.Type + " - " + Header.NumBytes); Int32 bytes = System.BitConverter.ToInt32(messageBuffer, 0); Console.WriteLine("\tBytes: " + bytes); break; case '!': Console.WriteLine("Got test 1 header: " + Header.Type + " - " + Header.NumBytes); //Int32 message = System.BitConverter.ToInt32(messageBuffer, 0); float message = System.BitConverter.ToSingle(messageBuffer, 0); Console.WriteLine("\tMessage: " + message); break; case '#': Console.WriteLine("Got test 2 message header: " + Header.Type + " - " + Header.NumBytes); //Int32 message = System.BitConverter.ToInt32(messageBuffer, 0); Int32 randomVariable = System.BitConverter.ToInt32(messageBuffer, 0); Console.WriteLine("\tMessage: " + randomVariable); break; case '$': Console.WriteLine("Got test 3 message header: " + Header.Type + " - " + Header.NumBytes); GCHandle messageHandle = GCHandle.Alloc(messageBuffer, GCHandleType.Pinned); TestStruct Struct = (TestStruct)Marshal.PtrToStructure(messageHandle.AddrOfPinnedObject(), typeof(TestStruct)); messageHandle.Free(); Console.WriteLine("\tChar: " + Struct.Char); Console.WriteLine("\tInteger: " + Struct.Integer); Console.WriteLine("\tFloat: " + Struct.Float); break; default: Console.WriteLine("Unknown Header type: " + Header.Type); Console.WriteLine("\t" + (byte)Header.Type); Console.WriteLine("\t" + messageBuffer.Length); Console.Write("\t"); foreach (byte b in messageBuffer) { Console.Write(b + ", "); } Console.WriteLine(); break; } ; }
private void dataReceivedFunction(object sender, SerialDataReceivedEventArgs e) { if (serialPort.BytesToRead >= (Marshal.SizeOf(typeof(SerialHeader)) + this.MESSAGE_SIZE)) { byte[] headerBuffer = new byte[Marshal.SizeOf(typeof(SerialHeader))]; serialPort.Read(headerBuffer, 0, Marshal.SizeOf(typeof(SerialHeader))); GCHandle handle = GCHandle.Alloc(headerBuffer, GCHandleType.Pinned); SerialHeader header = (SerialHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(SerialHeader)); handle.Free(); byte[] messageBuffer = new byte[this.MESSAGE_SIZE]; serialPort.Read(messageBuffer, 0, this.MESSAGE_SIZE); this.dataReceivedHandler(header, messageBuffer, (UInt32)this.MESSAGE_SIZE); } //this.dataReceivedHandler(buffer, bytesRead); }
public bool Write(SerialHeader Header, object message, byte NumBytes) { int writeBufferSize = Marshal.SizeOf(typeof(SerialHeader)) + this.MESSAGE_SIZE; Header.NumBytes = NumBytes; try { byte[] writeBuffer = new byte[writeBufferSize]; //Add Header to witeBuffer IntPtr headerPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Header)); Marshal.StructureToPtr(Header, headerPtr, false); Marshal.Copy(headerPtr, writeBuffer, 0, Marshal.SizeOf(Header)); //Add message to writeBuffer //BinaryFormatter bf = new BinaryFormatter(); //using (var ms = new MemoryStream()) //{ // bf.Serialize(ms, message); // Array.Copy(ms.ToArray(), 0, writeBuffer, Marshal.SizeOf(typeof(SerialHeader)), this.MESSAGE_SIZE); //} IntPtr messagePtr = Marshal.AllocHGlobal(Marshal.SizeOf(message)); Marshal.StructureToPtr(message, messagePtr, false); Marshal.Copy(messagePtr, writeBuffer, Marshal.SizeOf(typeof(SerialHeader)), Marshal.SizeOf(message)); //Write writeBuffer to Serial Port serialPort.Write(writeBuffer, 0, writeBufferSize); } catch { return(false); } return(true); }