jungervin / EsPy

Micropython IDE for ESP8266
71 stars 25 forks source link

Com-Port for Terminal often does not "open" correctly #5

Open ThomasAtBaum opened 6 years ago

ThomasAtBaum commented 6 years ago

Too often I can not connect with the terminal to my ESP board. Extiting and restarting the application does not help. Sometimes the Connect and Disconnect buttons in the toolbar are both active but don't do anything. After fiddeling around I have a workaround to get the serial communication working.. Believe it or not, deleting the "DockPanel.config" in the Installation directory and restarting EsPy.exe does the trick! I would really apprechiate, if you would take a look into your com-port handeling code. best regards Thomas

jungervin commented 6 years ago

Did you get an exception when you trying to open the port?

If not, then the port is open (on the PC), and it is more likely that the device is busy, for example, because it is in a cycle without time.sleep()

I suggest you try this code and after the ESP reset you will find out the device and it will be communicating. (I hope :) )

import time print ("Waiting for CTRL + I ...") for i in range (5): print ("% d sec"% (5-i)) time.sleep (1) print ("Started ...")

--Your program goes here

ThomasAtBaum commented 6 years ago

It' different... I took a look at the C# code and I think I found the bug... The loop: foreach (DockContent dc in this.dockPanel1.Documents) { if (dc is IPort) { (dc as IPort).Port = xxx } } fails to include the TerminalForm in case this form is docked at the right (any?) side of the window. The TerminalForm ist removed from the Documents collection and so the Port in the Form is not set in this case!

I added code as follows: public PySerial Port { get { return this.FPort; } private set { if (this.FPort != null) { // if(this.Port.IsOpen) this.FPort.Close();

                this.FPort.PortOpen -= Port_PortOpen;
                this.FPort.PortClose -= Port_PortClose;
                this.FPort.DataReceived -= Port_DataReceived;
                this.FPort.ErrorReceived -= Port_ErrorReceived;
                this.FPort.PortBusy -= Port_PortBusy;
                this.FPort.PortFree -= Port_PortFree;

                foreach (DockContent dc in this.dockPanel1.Documents)
                {
                    if (dc is IPort)
                    {
                        (dc as IPort).Port = null;
                    }
                }
                if (this.TerminalForm is IPort)
                {
                    (this.TerminalForm).Port = null;
                }
                this.FPort = null;
            }

            this.FPort = value;
            if (this.FPort != null)
            {
                if (this.FPort is SerialPort)
                {
                    this.FPort.PortName = this.CurretPortName;
                    this.FPort.PortOpen += Port_PortOpen;
                    this.FPort.PortClose += Port_PortClose;
                    this.FPort.DataReceived += Port_DataReceived;
                    this.FPort.ErrorReceived += Port_ErrorReceived;
                    this.FPort.PortBusy += Port_PortBusy;
                    this.FPort.PortFree += Port_PortFree;
                }
                else
                {
                    //Websocket
                }

                foreach (DockContent dc in this.dockPanel1.Documents)
                {
                     if (dc is IPort)
                    {
                        (dc as IPort).Port = this.Port;
                    }
                }
                if (this.TerminalForm is IPort)
                {
                    (this.TerminalForm).Port = this.Port;
                }
            }
        }
    }

in Mainform.cs For me now all is working as desired. Greetings Thomas

jungervin commented 6 years ago

Yes, you are right. Because I removed the terminal from the documents. But I have not uploaded the new version yet.

in TerminalForm() this.DockAreas = //DockAreas.Document | DockAreas.DockBottom | DockAreas.DockLeft | DockAreas.DockRight | DockAreas.DockTop;

Thank You!

ThomasAtBaum commented 6 years ago

OK Anyway it seems to me having a reference to the port in every document is overkill. Consider to call the eventhandlers directly from the ONE/MAIN Port-Object. This will remove lots of code and improve clearness without major changes in your code. Thomas