/// <summary> /// Read a G-code channel /// </summary> /// <param name="from">Origin</param> /// <param name="channel">Channel that has acquired the lock</param> /// <returns>Number of bytes read</returns> public static int ReadCodeChannel(ReadOnlySpan <byte> from, out CodeChannel channel) { CodeChannelHeader header = MemoryMarshal.Cast <byte, CodeChannelHeader>(from)[0]; channel = header.Channel; return(Marshal.SizeOf(header)); }
/// <summary> /// Read a G-code channel /// </summary> /// <param name="from">Origin</param> /// <param name="channel">Channel that has acquired the lock</param> /// <returns>Number of bytes read</returns> public static int ReadCodeChannel(ReadOnlySpan <byte> from, out CodeChannel channel) { CodeChannelHeader header = MemoryMarshal.Read <CodeChannelHeader>(from); channel = header.Channel; return(Marshal.SizeOf <CodeChannelHeader>()); }
/// <summary> /// Write a G-code channel /// </summary> /// <param name="to">Destination</param> /// <param name="channel">Channel for the lock request</param> /// <returns>Number of bytes written</returns> public static int WriteCodeChannel(Span <byte> to, CodeChannel channel) { CodeChannelHeader header = new CodeChannelHeader { Channel = channel }; MemoryMarshal.Write(to, ref header); return(Marshal.SizeOf(header)); }
/// <summary> /// Write a request to evaluate an expression /// </summary> /// <param name="to">Destination</param> /// <param name="channel">Where to evaluate the expression</param> /// <param name="expression">Expression to evaluate</param> /// <returns>Number of bytes written</returns> public static int WriteEvaluateExpression(Span <byte> to, CodeChannel channel, string expression) { Span <byte> unicodeExpression = Encoding.UTF8.GetBytes(expression); // Write header CodeChannelHeader header = new CodeChannelHeader { Channel = channel }; MemoryMarshal.Write(to, ref header); int bytesWritten = Marshal.SizeOf(header); // Write expression unicodeExpression.CopyTo(to.Slice(bytesWritten)); bytesWritten += unicodeExpression.Length; return(AddPadding(to, bytesWritten)); }