/// <summary>
        /// Initialize serialport.
        /// </summary>
        public void InitializeSerialPort()
        {
            //Read config and initialize serialport.
            config     = SerialPortConfigurer.ReadConfig();
            serialPort = new SerialPort(config.portName, config.baudRate, config.parity, config.dataBits, config.stopBits)
            {
                ReadBufferSize  = config.readBufferSize,
                ReadTimeout     = config.readTimeout,
                WriteBufferSize = config.writeBufferSize,
                WriteTimeout    = config.writeTimeout
            };

            //Initialize read and write thread.
            readThread = new Thread(ReadBytesFromBuffer)
            {
                IsBackground = true
            };
            writeThread = new Thread(WriteBytesToBuffer)
            {
                IsBackground = true
            };

            //Initialize bytes array.
            readBytes  = new byte[config.readCount];
            writeBytes = new byte[config.writeCount];
        }
        /// <summary>
        /// Write SerialPortConfig to config file.
        /// </summary>
        /// <param name="config">Config of serialport.</param>
        public static void WriteConfig(SerialPortConfig config)
        {
            try
            {
#if UNITY_5_3_OR_NEWER
                var configJson = JsonUtility.ToJson(config);
#else
                var configJson = JsonMapper.ToJson(config);
#endif
                File.WriteAllText(ConfigPath, configJson);
#if UNITY_EDITOR
                AssetDatabase.Refresh();
#endif
            }
            catch (Exception e)
            {
                LogUtility.LogError(e.Message);
            }
        }
        /// <summary>
        /// Read SerialPortConfig from config file.
        /// </summary>
        /// <param name="config">Config of serialport.</param>
        /// <param name="error">Error message.</param>
        /// <returns>Read config succeed.</returns>
        public static bool ReadConfig(out SerialPortConfig config, out string error)
        {
            config = new SerialPortConfig();
            try
            {
                var json = File.ReadAllText(ConfigPath);
#if UNITY_5_3_OR_NEWER
                config = JsonUtility.FromJson <SerialPortConfig>(json);
#else
                config = JsonMapper.ToObject <SerialPortConfig>(json);
#endif
                error = string.Empty;
                Debug.Log("Read config succeed.");
                return(true);
            }
            catch (Exception e)
            {
                error = e.Message;
                Debug.LogError(error);
                return(false);
            }
        }
        /// <summary>
        /// Write SerialPortConfig to config file.
        /// </summary>
        /// <param name="config">Config of serialport.</param>
        /// <param name="error">Error message.</param>
        /// <returns>Write config succeed..</returns>
        public static bool WriteConfig(SerialPortConfig config, out string error)
        {
            try
            {
#if UNITY_5_3_OR_NEWER
                var configJson = JsonUtility.ToJson(config);
#else
                var configJson = JsonMapper.ToJson(config);
#endif
                File.WriteAllText(ConfigPath, configJson);
#if UNITY_EDITOR
                AssetDatabase.Refresh();
#endif
                error = string.Empty;
                Debug.Log("Write config succeed.");
                return(true);
            }
            catch (Exception e)
            {
                error = e.Message;
                Debug.LogError(error);
                return(false);
            }
        }
 private void Start()
 {
     config = SerialPortConfigurer.ReadConfig();
 }