コード例 #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
        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);
            }
        }
コード例 #4
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;
        }