Пример #1
0
        private bool _checkDevice()
        {
            SerialState checkState = new SerialState();

            byte[] msg = { 0xAD, 0x05, 0x0 };
            _serialCommPort.Write(msg, 0, msg.Length);
            Thread.Sleep(500);

            WUSerialPacketResponse response = new WUSerialPacketResponse();

            response.DataIndex = 0;
            int data = 0;

            while (_serialCommPort.BytesToRead > 0)
            {
                data = _serialCommPort.ReadByte();

                switch (checkState.state)
                {
                case SerialState.SerialReadStates.MagicNumber:
                    if ((byte)data == 0xAD)
                    {
                        checkState.state = SerialState.SerialReadStates.Command;
                    }
                    break;

                case SerialState.SerialReadStates.Command:
                    response.Command = (byte)data;
                    checkState.state = SerialState.SerialReadStates.Length;
                    break;

                case SerialState.SerialReadStates.Length:
                    response.Length  = (byte)data;
                    response.Data    = new char[response.Length];
                    checkState.state = SerialState.SerialReadStates.Data;
                    break;

                case SerialState.SerialReadStates.Data:
                    response.Data[response.DataIndex] = (char)data;
                    if (response.Length == response.DataIndex + 1)
                    {
                        if (response.Data[0] == 0xFA && response.Data[1] == 0xBA)
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        response.DataIndex++;
                    }
                    break;

                default:
                    break;
                }
            }

            return(false);
        }
Пример #2
0
        public void UartHandler()
        {
            SerialState            checkState = new SerialState();
            WUSerialPacketResponse response   = new WUSerialPacketResponse();
            int data = 0;

            response.DataIndex = 0;
            while (this._isWorking)
            {
                if (this._serialCommPort.IsOpen == true)
                {
                    if (_serialCommPort.BytesToRead > 0)
                    {
                        data = this._serialCommPort.ReadByte();
                        switch (checkState.state)
                        {
                        case SerialState.SerialReadStates.MagicNumber:
                            if ((byte)data == 0xAD)
                            {
                                checkState.state = SerialState.SerialReadStates.Command;
                            }
                            break;

                        case SerialState.SerialReadStates.Command:
                            response.Command = (byte)data;
                            checkState.state = SerialState.SerialReadStates.Length;
                            break;

                        case SerialState.SerialReadStates.Length:
                            response.Length  = (byte)data;
                            response.Data    = new char[response.Length];
                            checkState.state = SerialState.SerialReadStates.Data;
                            break;

                        case SerialState.SerialReadStates.Data:
                            response.Data[response.DataIndex] = (char)data;
                            if (response.Length == response.DataIndex + 1)
                            {
                                response.DataIndex = 0;
                                Debug.Print("# [UART] { command: " + response.Command + ", data: " + response.Data.ToString());
                                this._dataArrivedHandler(response);
                            }
                            else
                            {
                                response.DataIndex++;
                            }
                            break;

                        default:
                            break;
                        }
                    }
                }

                Thread.Sleep(10);
            }
        }
Пример #3
0
 private void UARTDataArrived(WUSerialPacketResponse data)
 {
     switch (data.Command)
     {
     case 0x9:     // UART_REQUEST_CHECK_DEVICE_ID
         CloudRequest req = new CloudRequest();
         req.UARTRequest = data;
         this._cloud.AddRequest(req);
         // TODO
         // 1. HTTP request to server with device ID.
         // 2. Add request to list of requests, delete it when response will be sent to the device.
         break;
     }
 }
Пример #4
0
        public void UartHandler()
        {
            SerialState             checkState  = new SerialState();
            WUSerialPacketResponse  response    = new WUSerialPacketResponse();
            int                     data        = 0;

            response.DataIndex = 0;
            while (this._isWorking)
            {
                if (this._serialCommPort.IsOpen == true)
                {
                    if (_serialCommPort.BytesToRead > 0)
                    {
                        data = this._serialCommPort.ReadByte();
                        switch (checkState.state)
                        {
                            case SerialState.SerialReadStates.MagicNumber:
                                if ((byte)data == 0xAD)
                                {
                                    checkState.state = SerialState.SerialReadStates.Command;
                                }
                                break;
                            case SerialState.SerialReadStates.Command:
                                response.Command = (byte)data;
                                checkState.state = SerialState.SerialReadStates.Length;
                                break;
                            case SerialState.SerialReadStates.Length:
                                response.Length = (byte)data;
                                response.Data = new char[response.Length];
                                checkState.state = SerialState.SerialReadStates.Data;
                                break;
                            case SerialState.SerialReadStates.Data:
                                response.Data[response.DataIndex] = (char)data;
                                if (response.Length == response.DataIndex + 1)
                                {
                                    response.DataIndex = 0;
                                    Debug.Print("# [UART] { command: " + response.Command + ", data: " + response.Data.ToString());
                                    this._dataArrivedHandler(response);
                                }
                                else
                                {
                                    response.DataIndex++;
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }

                Thread.Sleep(10);
            }
        }
Пример #5
0
        private bool _checkDevice()
        {
            SerialState checkState = new SerialState();
            byte[] msg = { 0xAD, 0x05, 0x0 };
            _serialCommPort.Write(msg, 0, msg.Length);
            Thread.Sleep(500);

            WUSerialPacketResponse response = new WUSerialPacketResponse();
            response.DataIndex = 0;
            int data = 0;
            while (_serialCommPort.BytesToRead > 0)
            {
                data = _serialCommPort.ReadByte();

                switch (checkState.state)
                {
                    case SerialState.SerialReadStates.MagicNumber:
                        if ((byte)data == 0xAD)
                        {
                            checkState.state = SerialState.SerialReadStates.Command;
                        }
                        break;
                    case SerialState.SerialReadStates.Command:
                        response.Command = (byte)data;
                        checkState.state = SerialState.SerialReadStates.Length;
                        break;
                    case SerialState.SerialReadStates.Length:
                        response.Length = (byte)data;
                        response.Data = new char[response.Length];
                        checkState.state = SerialState.SerialReadStates.Data;
                        break;
                    case SerialState.SerialReadStates.Data:
                        response.Data[response.DataIndex] = (char)data;
                        if (response.Length == response.DataIndex + 1)
                        {
                            if (response.Data[0] == 0xFA && response.Data[1] == 0xBA)
                            {
                                return true;
                            }
                        }
                        else
                        {
                            response.DataIndex++;
                        }
                        break;
                    default:
                        break;
                }
            }

            return false;
        }
Пример #6
0
 private void UARTDataArrived(WUSerialPacketResponse data)
 {
     switch (data.Command)
     {
         case 0x9: // UART_REQUEST_CHECK_DEVICE_ID
             CloudRequest req = new CloudRequest();
             req.UARTRequest = data;
             this._cloud.AddRequest(req);
             // TODO
             // 1. HTTP request to server with device ID.
             // 2. Add request to list of requests, delete it when response will be sent to the device.
             break;
     }
 }