Exemple #1
0
        public override void Write(byte[] data, bool getCompleted = false)
        {
            if (data == null)
            {
                return;
            }
            if (data.Length == 0)
            {
                return;
            }

#if UNITY_STANDALONE
            try
            {
                _serialPort.Write(data, 0, data.Length);
                if (getCompleted)
                {
                    OnWriteCompleted.Invoke();
                }
            }
            catch (Exception)
            {
                ErrorClose();
                OnErrorClosed.Invoke();
            }
#endif
        }
Exemple #2
0
        public override byte[] Read()
        {
#if UNITY_STANDALONE
            List <byte> bytes = new List <byte>();

            while (true)
            {
                try
                {
                    bytes.Add((byte)_serialPort.ReadByte());
                }
                catch (TimeoutException)
                {
                    break;
                }
                catch (Exception)
                {
                    ErrorClose();
                    OnErrorClosed.Invoke();
                    return(null);
                }
            }

            if (bytes.Count == 0)
            {
                return(null);
            }
            else
            {
                return(bytes.ToArray());
            }
#else
            return(null);
#endif
        }
        public override byte[] Read()
        {
            List <byte> bytes = new List <byte>();

            try
            {
                if (_socket.Available > 0)
                {
                    byte[] rcvData = new byte[256];
                    int    count   = _socket.Receive(rcvData);
                    for (int i = 0; i < count; i++)
                    {
                        bytes.Add(rcvData[i]);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.Log("Read: " + e);
                ErrorClose();
                OnErrorClosed.Invoke();
                return(null);
            }

            if (bytes.Count == 0)
            {
                return(null);
            }
            else
            {
                return(bytes.ToArray());
            }
        }
Exemple #4
0
        // Update is called once per frame
        void Update()
        {
            if (_threadOnOpen)
            {
                OnOpen.Invoke();
                _threadOnOpen = false;
            }
            if (_threadOnOpenFailed)
            {
                ErrorClose();
                OnOpenFailed.Invoke();
                _threadOnOpenFailed = false;
            }
            if (_threadOnErrorClosed)
            {
                ErrorClose();
                OnErrorClosed.Invoke();
                _threadOnErrorClosed = false;
            }
            if (_threadOnStartSearch)
            {
                OnStartSearch.Invoke();
                _threadOnStartSearch = false;
            }
            if (_threadOnStopSearch)
            {
                OnStopSearch.Invoke();
                _threadOnStopSearch = false;
            }
            if (_threadOnFoundDevice)
            {
                OnFoundDevice.Invoke(new CommDevice(foundDevices[foundDevices.Count - 1]));
                _threadOnFoundDevice = false;
            }
            if (_threadOnChangedDevice)
            {
                OnChangedDevice.Invoke(new CommDevice(device));
                _threadOnChangedDevice = false;
            }
            if (_threadOnWriteCompleted)
            {
                OnWriteCompleted.Invoke();
                _threadOnWriteCompleted = false;
            }

            if (_searchTimeout > 0f)
            {
                _searchTimeout -= Time.deltaTime;
                if (_searchTimeout <= 0f)
                {
                    StopSearch();
                }
            }
        }
 void LateUpdate()
 {
     if (IsOpen && _txBuffer.Count > 0)
     {
         try
         {
             _socket.Send(_txBuffer.ToArray());
             _txBuffer.Clear();
             if (_getCompleted)
             {
                 OnWriteCompleted.Invoke();
             }
         }
         catch (Exception e)
         {
             Debug.Log("Write: " + e);
             ErrorClose();
             OnErrorClosed.Invoke();
         }
     }
 }
Exemple #6
0
 private void AndroidMessageErrorClose(string message)
 {
     Debug.Log(message);
     ErrorClose();
     OnErrorClosed.Invoke();
 }
Exemple #7
0
 protected override void ErrorClose()
 {
     //    Debug.LogError("Error Close");
     OnErrorClosed.Invoke();
     Close();
 }