Exemplo n.º 1
0
        public void Upload(string ComPort, byte[] data, int uploadBaudrate = 115200)
        {
            UploadProgress?.Invoke(this, 0);

            this.SerialPort = new SerialPort(ComPort);
            try
            {
                try
                {
                    this.SerialPort.Open();
                }
                catch (Exception ex)
                {
                    Message?.Invoke(this, "Unable to open Serial Port " + ComPort);
                    return;
                }
                NextionInfos model = null;
                try
                {
                    model = DetectNextion();
                }
                catch
                {
                }

                if (model != null)
                {
                    Message?.Invoke(this, "Nextion found " + model.Model);
                    SwitchToUploadMode(data, uploadBaudrate);
                    UploadData(data);
                }
                else
                {
                    Message?.Invoke(this, "Nextion not found");
                }
            }
            finally
            {
                try
                {
                    SerialPort.Close();
                }
                catch
                {
                }
            }
        }
Exemplo n.º 2
0
        public void Upload(string ComPort, byte[] data, int uploadBaudrate = 115200, bool reset = false)
        {
            UploadProgress?.Invoke(this, 0);

            this.SerialPort = new SerialPort(ComPort);
            UploadStatus res = UploadStatus.Ok;

            try
            {
                try
                {
                    this.SerialPort.Open();
                }
                catch (Exception ex)
                {
                    Message?.Invoke(this, "Unable to open Serial Port " + ComPort);
                    return;
                }
                NextionInfos model = null;
                try
                {
                    model = DetectNextion();
                }
                catch
                {
                }

                if (model != null)
                {
                    var firmwareVersion = int.Parse(model.FirmwareVersion);

                    Message?.Invoke(this, "Nextion found " + model.Model + " (Firmware version : " + model.FirmwareVersion + ")");

                    if (reset)
                    {
                        Message?.Invoke(this, "Resetting nextion");
                        SendInstruction(SerialPort, "rest");
                        Message?.Invoke(this, "Waiting 4 seconds ...");
                        for (int i = 0; i < 8; i++)
                        {
                            if (CancelRequested)
                            {
                                return;
                            }

                            Thread.Sleep(500);
                        }
                        SerialPort.ReadExisting();
                    }

                    if (firmwareVersion < 130 && uploadBaudrate > 115200)
                    {
                        Message?.Invoke(this, "Nextion firmware does not supports high speed upload, forcing 115200bauds");
                        SwitchToUploadMode(data, 115200);
                    }
                    else if (model.Model[6] == 'T' && uploadBaudrate > 115200)
                    {
                        Message?.Invoke(this, "Basic model does not supports high speed upload, forcing 115200bauds");
                        SwitchToUploadMode(data, 115200);
                    }
                    else
                    {
                        SwitchToUploadMode(data, uploadBaudrate);
                    }

                    res = UploadData(data);
                }
                else
                {
                    if (!CancelRequested)
                    {
                        Message?.Invoke(this, "Nextion not found");
                    }
                }
            }
            finally
            {
                try
                {
                    if (CancelRequested)
                    {
                        Message?.Invoke(this, "Upload canceled");
                    }

                    SerialPort.Close();
                    Message?.Invoke(this, "Resetting arduino micro if required");
                    ResetMicro(ComPort);

                    if (res == UploadStatus.Ok)
                    {
                        Message?.Invoke(this, "Upload finished successfully.");
                    }
                }
                catch
                {
                }
            }
        }