public void sendCommand(CloudMessage message) { try { if (!this.Serial.Port.IsOpen) { this.Serial.connectionChange(null, null); } string msg = serializeMessage(message); if (this.Serial.Port.IsOpen) { this.Serial.Port.Write(msg); } else { throw new Exception("Unable to connect to device."); } } catch (Exception ex) { if (ex.Message == "Unable to connect to device.") { throw ex; } else { throw new Exception("Undefined serial port, please select a valid COM port first."); } } }
private String serializeMessage(CloudMessage message) { MemoryStream stream = new MemoryStream(); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(CloudMessage)); serializer.WriteObject(stream, message); stream.Position = 0; return(new StreamReader(stream).ReadToEnd()); }