Enabling serial port turns screen off

I’m currently playing with a max232 on the extender.
The serial port works fine and i’m able to communicate with other equipements, but… as soon as I open the com port in a gadgeteer application, I get the following message in VS : [italic]Updating display configuration. DEVICE WILL NOW REBOOT. Visual Studio might lose connection, and debugging might need to be manually restarted[/italic], and the screen is disabled. I had to deploy an empty gadgeteer app to restore it.

When I do the same in an app based on the NetMF console application, the screen keeps working.

Here is the code I used in the gadgeteer app :


using System;
using System.IO.Ports;
using System.Text;
using Gadgeteer.Modules.GHIElectronics;
using Microsoft.SPOT;

namespace TestSerialPort
{
    public partial class Program
    {
        // Fez Spider sockets :
        //      Com1 : 11
        //      Com2 :  4
        //      Com3 :  8
        //      Com4 :  9

        private SerialPort comPort;

        void ProgramStarted()
        {
            comPort = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
            comPort.Handshake = Handshake.RequestToSend;
            comPort.DataReceived += ComPortDataReceived;
            comPort.ErrorReceived += ComPortErrorReceived;
            comPort.ReadTimeout = 200;

            try
            {
                Debug.Print("Opening " + comPort.PortName);
                comPort.Open();
                Debug.Print("open");
            }
            catch (Exception ex)
            {
                Debug.Print("Unable to open com port\n" + ex.ToString());
            }

           button.ButtonPressed += ButtonPressed;
        }

        void ButtonPressed(Button sender, Button.ButtonState state)
        {
            if (comPort.IsOpen)
            {
                button.TurnLEDOn();
                try
                {
                    var buf = Encoding.UTF8.GetBytes(DateTime.Now + "\r\n");
                    comPort.Write(buf, 0, buf.Length);
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.ToString());
                }
                button.TurnLEDOff();
            }
            else
                Debug.Print("Com port not open");
        }

        void ComPortErrorReceived(object sender, SerialErrorReceivedEventArgs e)
        {
            string eventTypeName;
            switch (e.EventType)
            {
                case SerialError.Frame: eventTypeName = "frame"; break;
                case SerialError.Overrun: eventTypeName = "overrun"; break;
                case SerialError.RXOver: eventTypeName = "RX overflow"; break;
                case SerialError.RXParity: eventTypeName = "RX parity error"; break;
                case SerialError.TXFull: eventTypeName = "TX buffer full"; break;
                default: eventTypeName = e.ToString(); break;
            }
            Debug.Print("Error received : " + eventTypeName);
        }

        void ComPortDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string eventTypeName;
            switch (e.EventType)
            {
                case SerialData.Chars: eventTypeName = "characters"; break;
                case SerialData.Eof: eventTypeName = "EOF"; break;
                default: eventTypeName = e.ToString(); break;
            }

            int available = comPort.BytesToRead;
            Debug.Print("Data received : " + eventTypeName + ", " + available + " bytes");

            var buf = new byte[available];
            comPort.Read(buf, 0, available);
            Debug.Print(new string(Encoding.UTF8.GetChars(buf)));
        }
    }
}

Some incompatibilities between standard assemblies and gadgeteer ?

What do you have in the designer? Post a snapshot please.

Just a button and the display. The extender is connected to socket 4.

DataReceived event is subscribed to before com port is opened.

Subscribe to events after the com port is opened.

Thanks, it works :slight_smile:
What I don’t understand is why it depends on the moment we subscribe to the event. It shouldn’t… and nothing special is mentioned in the reference documentation.
Moreover, in the app not using the gadgeteer layer, the problem is not present.
Anyway, thanks again for your help !

I think the order of subscribing might be fixed in MF 4.2.

This issue has been discussed many times in the forums. :smiley:

This is the type of information that might be put into a FAQ area.