private void Update() { try { //We should not attempt to read data, if the port is not open. if (serialPort.IsOpen) { if (serialPort.BytesToRead > 1) { //Read, process and save the data from the serial port buffer try { ReadBytes(); ConvertResistanceToConductance(); //OBS: This is the place where data is parsed to wherever it is needed. } catch (Exception e) { Debug.LogWarning("Error when reading bytes or writing to document: " + e); } //Send data to Processing try { Byte[] sendBytes = Encoding.ASCII.GetBytes(currentPressureValue.ToString()); UDPTX_resistance.Send(sendBytes, sendBytes.Length); sendBytes = Encoding.ASCII.GetBytes(string.Format("{0}", (int)fsrConductance)); UDPTX_weight.Send(sendBytes, sendBytes.Length); } catch (Exception e) { Debug.LogWarning("Connection failed: " + e); } } } } catch (Exception e) { Debug.LogWarning("There's no port: " + e); } }
private void OnApplicationQuit() { if (serialPort != null) { serialPort.Close(); } UDPTX_resistance.Close(); UDPTX_weight.Close(); }