/// <summary> /// Used for sending an object of class JsonCommandMessage over serial to the chosen COM port /// </summary> /// <param name="msg">Object of the JsonCommandMessage format</param> public void SendObjViaSerial(JsonCommandMessage msg) { string temp = ClassToJson(msg); if (!this.IsOpen) { form.ShowDialog(); } try { this.WriteLine(temp); } catch (Exception e) { MessageBox.Show(e.Message, "Error When writing to " + this.PortName); } }
/// <summary> /// Constructor for working with a cbo filled with the available serial ports and a button for connecting. /// </summary> /// <param name="cbo">Combobox for choosing Comport</param> /// <param name="button">Button for connecting and disconnecting</param> /// <param name="baudRate">Integer denoting the baudrate of serial comms</param> public Communication(ComboBox cbo, Button button, int baudRate) { BaudRate = baudRate; ReadTimeout = 2000; WriteTimeout = 2000; dataReady = false; ConnectButton = button; //Assigns the connect button to the field DataReceived += new SerialDataReceivedEventHandler(DataRecieved); //Event handler for handling serial data recieved ConnectButton.Click += new EventHandler(BtnConnectClick); //Click event for the connect button cboParent = cbo; Cbo = cbo; Cbo.Items.AddRange(ports); if (Cbo.Items.Count > 0) { Cbo.SelectedIndex = 0; } form = new notConnectedForm(this); form.FormClosed += new FormClosedEventHandler(closedForm); cmdMsg = new JsonCommandMessage(); }
private string ClassToJson(JsonCommandMessage msg) { string json = JsonConvert.SerializeObject(msg); return(json); }