Debug and deploy

Hi,

I have got an working FEZ Mini while debugging. Breakpoints are reached consistenly and got no problem at all with debugging.

But when I deploy the program I can’t get it working.

Should I do something escpecially to create a standalone FEZ Mini without being connected to a PC? I thought deploy is the way to get the files on the FEZ Mini and then can unplug the USB and connect it to a battery that everything should work fine.

Please let me know!

Roel

Yes, as soon as you deploy successfully (deploy = placing on the chip) you can unplug the usb, connect it to a battery and the program runs.

Maybe there is an exception raised which causes the program to break?

you might want to post your code so we can have a look at it.

What do you mean when you say, “can’t get it working”? How are you powering the Mini? How do you know if it is working or not (what is it supposed to do?)

Code:


public class Program
    {
        private const byte sensorAddress = 72;
        TMP102Sensor temperatureSensor = new TMP102Sensor(sensorAddress);
        public SerialPort _port;
        OutputPort Di8 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di8, false);
        OutputPort Di9 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di9, false);
        double SetPoint = 21;

        public static void Main()
        {
            Program program = new Program();
            program.OpenSerial();

            while (true)
            {
            }
        }

        private void OpenSerial()
        {
            _port = new SerialPort("COM2");
            _port.BaudRate = 9600;
            _port.DataReceived += new SerialDataReceivedEventHandler(_port_DataReceived);

            if (!_port.IsOpen)
            {
                _port.Open();
            }
        }

        void _port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] result = new byte[_port.BytesToRead];
            SerialPort sp = (SerialPort)sender;
            sp.Read(result, 0, _port.BytesToRead);

            string r = new string(System.Text.UTF8Encoding.UTF8.GetChars(result));

            if (r == "T")
            {
                try
                {
                    float temperature = temperatureSensor.Temperature();

                    byte[] buffer = UTF8Encoding.UTF8.GetBytes(temperature.ToString());
                    sp.Write(buffer, 0, buffer.Length);

                    if (temperature < SetPoint)
                    {
                        Di9.Write(true);
                    }
                    else
                    {
                        Di9.Write(false);
                    }
                }
                catch
                {
                    byte[] buffer = UTF8Encoding.UTF8.GetBytes("ERROR\r");
                    sp.Write(buffer, 0, buffer.Length);
                }
            }
            else if (r.Substring(0, 1) == "S" && r.Length == 6)
            {
                string s = "";
                double d = 0;

                try
                {
                    s = r.Substring(1, 5);
                    d = Convert.ToDouble(s);
                    SetPoint = d;
                    byte[] buffer = UTF8Encoding.UTF8.GetBytes("OK\r");
                    sp.Write(buffer, 0, buffer.Length);
                }
                catch
                {
                    byte[] buffer = UTF8Encoding.UTF8.GetBytes("ERROR\r");
                    sp.Write(buffer, 0, buffer.Length);
                }
            }
            else
            {
                byte[] buffer = UTF8Encoding.UTF8.GetBytes("ERROR\r");
                sp.Write(buffer, 0, buffer.Length);
            }
        }
    }

  1. Registering for the DataReceived event of the serial port should be done after the port has been opened.

  2. In your main, doing [italic]while(true); [/italic]will slow down your program. You have put the main thread into a loop. Use [italic]Thread.Sleep(TimeOut.Infinite);[/italic]

  3. You do a compare [italic]if (r == “T”)[/italic] to see if a ‘T’ has been received. You should be doing the compare as [italic]if (t.Equals(“T”))[/italic]. What you did was comparing references not the content of the strings.

I suggest you single step in the debugger and follow the data flow.

In most cases, if the program works properly while connected to the debugger it will work standalone.

Thanks, now it is working and much faster!

Roel

        public static void Main()
        {
            Program program = new Program();
            program.OpenSerial();
 
            while (true)
            {
            }
        }

This looks very problematic to me. NETMF will call Main() when it starts. Your Main() is in the ‘Program’ class. Then you create a new instance of Program class (from inside the program class) but your Program class has no constructor (which IMHO is bad for a non-static class) and you’ll wind up defining all of this stuff (below) again:

        private const byte sensorAddress = 72;
        TMP102Sensor temperatureSensor = new TMP102Sensor(sensorAddress);
        public SerialPort _port;
        OutputPort Di8 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di8, false);
        OutputPort Di9 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di9, false);
        double SetPoint = 21;

I would do away with creating a new instance of the Program class and just make OpenSerial a static member.

        public static void Main()
        {
           OpenSerial();
 
           Thread.Sleep(TimeOut.Infinate);
        }
 
        static private void OpenSerial()
        {

One other consideration…

I believe that you are assuming that you will get the entire message from the serial port with a single read. This is not a good assumption to make. You could get the message one byte at a time from the serial port.

Hi,

This better, I guess!


namespace Embedded_Master_Temperature
{
    public class Program
    {
        private const byte sensorAddress = 72;

        public Program()
        {

        }

        public static void Main()
        {
            OpenSerial();

            Thread.Sleep(Timeout.Infinite);
        }

        private static void OpenSerial()
        {
            SerialPort _port = new SerialPort("COM2");
            _port.BaudRate = 9600;

            if (!_port.IsOpen)
            {
                _port.Open();
                _port.DataReceived += new SerialDataReceivedEventHandler(_port_DataReceived);
            }
        }

        static void _port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            TMP102Sensor temperatureSensor = new TMP102Sensor(72);
            OutputPort Di9 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di9, false);
            double SetPoint = 21;

            Thread.Sleep(500);

            SerialPort sp = (SerialPort)sender;
            byte[] result = new byte[sp.BytesToRead];
            sp.Read(result, 0, sp.BytesToRead);

            string r = new string(System.Text.UTF8Encoding.UTF8.GetChars(result));

            if (r == "T")
            {
                try
                {
                    float temperature = temperatureSensor.Temperature();

                    byte[] buffer = UTF8Encoding.UTF8.GetBytes(temperature.ToString() + "\n");
                    sp.Write(buffer, 0, buffer.Length);

                    if (temperature < SetPoint)
                    {
                        Di9.Write(true);
                    }
                    else
                    {
                        Di9.Write(false);
                    }
                }
                catch
                {
                    byte[] buffer = UTF8Encoding.UTF8.GetBytes("ERROR: Cannot get temperature right\n");
                    sp.Write(buffer, 0, buffer.Length);
                }
            }
            else if (r.Substring(0, 1) == "S" && r.Length == 6)
            {
                string s = "";
                double d = 0;

                try
                {
                    s = r.Substring(1, 5);
                    d = Convert.ToDouble(s);
                    SetPoint = d;
                    byte[] buffer = UTF8Encoding.UTF8.GetBytes("OK\n");
                    sp.Write(buffer, 0, buffer.Length);
                }
                catch
                {
                    byte[] buffer = UTF8Encoding.UTF8.GetBytes("ERROR: Cannot convert setpoint right\n");
                    sp.Write(buffer, 0, buffer.Length);
                }
            }
            else
            {
                byte[] buffer = UTF8Encoding.UTF8.GetBytes("ERROR: Not a recognized command\n");
                sp.Write(buffer, 0, buffer.Length);
            }
        }
    }

Roel

[quote]But then how should I make _port etc accessible for the rest of the program?
[/quote]

I don’t think there is any requirement that a delegate (call back function) is a member function.

Sorry for editing the post, thaught nobody has responded …

I suggest you make the output port and the temperature sensor to be static members of the Program class. Then you only have to initialize them once.

It is generally undesireable to spend a lot of time in an interrupt routine, such as the DataReceived handler routine. The 500 ms sleep is not good practice. I suspect this is present to allow all the serial data to arrive?

But, these are more advanced techniques. Kudos on getting the program working!

Yes, make them static and then you can access them from within every method. :wink:

Although from coding point of view. recommend structuring code this way . Just a thought.

namespace Embedded_Master_Temperature
{
  
  public class TesterClass
  {
  	public static void Main()
	{
	   SomeClass obj = new SomeClass();
	   obj.OpenSerial(); 
	   Thread.Sleep(Timeout.Infinite);
        }  
  }
   
  public class SomeClass
  {
        private const byte sensorAddress = 72;
 
 	//constructor
        public SomeClass() 
        {
 
        }        
 
 	//member method1
        public void OpenSerial()
        {
            //..
        }
 
 	//member method2
        public void _port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
         //....
        }
  }

}

I have a similar issue, Fez Mini. I run it in debug and it works fine.
I deploy a release version. Says it deployed fine, but when I power it down and power it back up all I get is the red led light up. I’m using the Com2 port to talk to it. The code should echo back an ack. It does so in Debug mode, dead in Release.

The development platform is win7 x86.

Any advise?

Your question is missing one or more of these details:[ul]
The name of the product.
The code you’re trying to run.
Details on your setup.[/ul]
(Generated by QuickReply)

Five days old… Resolved in another thread. :slight_smile:

Yeah, I read that one first and then saw your reply on the other thread.