public WaveguideMessage(WGMessageType type, WGStatus _status, byte[] payload, string payloadStr) { payloadBytes = payload; messageType = type; payloadString = payloadStr; status = _status; }
public static bool ParseMessage(byte[] data, out WaveguideMessage message) { bool success = true; message = null; if (data.Length < 4) { success = false; } else { try { WGMessageType messageType = (WGMessageType)BitConverter.ToInt16(data, 0); short payloadSize = BitConverter.ToInt16(data, 2); WGStatus status = WGStatus.UNKNOWN; string payloadStr = ""; byte[] payload = null; switch (messageType) { case WGMessageType.STATUS: status = (WGStatus)data[4]; payload = new byte[payloadSize - 1]; Buffer.BlockCopy(data, 5, payload, 0, payloadSize - 1); payloadStr = Encoding.ASCII.GetString(payload); break; case WGMessageType.GET_STATUS: break; case WGMessageType.CONFIG_EXPERIMENT: payload = new byte[payloadSize]; Buffer.BlockCopy(data, 4, payload, 0, payloadSize); break; case WGMessageType.START_EXPERIMENT: break; case WGMessageType.STOP_EXPERIMENT: break; } message = new WaveguideMessage(messageType, status, payload, payloadStr); } catch (Exception) { success = false; } } return(success); }
public static bool Build_Status_Message(WGStatus status, string statusMsg, out byte[] message) { bool success = true; message = null; if (success) { try { byte[] payloadBytes = Encoding.ASCII.GetBytes(statusMsg); short messageType = (short)WGMessageType.STATUS; byte[] messageTypeBytes = BitConverter.GetBytes(messageType); short payloadSize = (short)(payloadBytes.Length + 1); byte[] payloadSizeBytes = BitConverter.GetBytes(payloadSize); byte statusByte = (byte)status; message = new byte[4 + payloadSize]; Buffer.BlockCopy(messageTypeBytes, 0, message, 0, 2); Buffer.BlockCopy(payloadSizeBytes, 0, message, 2, 2); message[4] = statusByte; Buffer.BlockCopy(payloadBytes, 0, message, 5, payloadBytes.Length); } catch (Exception) { success = false; message = null; } } return(success); }
public StatusChangeEventArgs(WGStatus Status) { _status = Status; }