[Solved] UART loopback stops at "UART.Read()" on Fez Panda

I’m running the UART loopback program provided in the e-Book on my Fez Panda.

However whenever it encounters the UART.Read() line it does not progress further in the code (yes I have a wire shorting Tx and Rx). I found this by running Debug.Print statements. Any idea how I can debug this? I checked that the references to Microsoft.SPOT.Hardware.SerialPort was added for System.IO.Ports, and I’m also able to run the LED blinking example program in the same project space.

Edit: updated topic to say solved

I keep forgetting about fixing that example. I will look into a fix tomorrow

It was throwing an exception. We updated the exmaple…

Thanks for investigating this! I’ll be able to try it out again the middle of next week. Have a nice holiday

When I run the program, at the line UART.ReadTimeout = 0; I get the error window popup:

“An unhandled exception of type ‘System.InvalidOperationException’ occurred in Microsoft.SPOT.Hardware.SerialPort.dll”.

It has the options continue and break. If I click continue I have the following in my Debug window:

Rebooting...

Found debugger!

Create TS.

 Loading start at 4e13c, end 56314

Attaching file.

Assembly: mscorlib (4.1.2821.0) (3880 RAM - 33236 ROM - 19134 METADATA)


   AssemblyRef    =        0 bytes (       0 elements)

   TypeRef        =        0 bytes (       0 elements)

   FieldRef       =        0 bytes (       0 elements)

   MethodRef      =        0 bytes (       0 elements)

   TypeDef        =     1112 bytes (     139 elements)

   FieldDef       =      272 bytes (     135 elements)

   MethodDef      =     1572 bytes (     786 elements)



   Attributes      =        0 bytes (       0 elements)

   TypeSpec        =       16 bytes (       4 elements)

   Resources       =      232 bytes (      29 elements)

   Resources Files =       16 bytes (       2 elements)

   Resources Data  =      437 bytes

   Strings         =     1551 bytes

   Signatures      =     2126 bytes

   ByteCode        =    11702 bytes




Loading Deployment Assemblies.

Attaching deployed file.

Assembly: Microsoft.SPOT.Hardware.SerialPort (4.1.2821.0) (508 RAM - 3440 ROM - 1527 METADATA)


   AssemblyRef    =        8 bytes (       2 elements)

   TypeRef        =       96 bytes (      24 elements)

   FieldRef       =        0 bytes (       0 elements)

   MethodRef      =       80 bytes (      20 elements)

   TypeDef        =       16 bytes (       2 elements)

   FieldDef       =       32 bytes (      16 elements)

   MethodDef      =       92 bytes (      46 elements)



   Attributes      =        0 bytes (       0 elements)

   TypeSpec        =        0 bytes (       0 elements)

   Resources       =        0 bytes (       0 elements)

   Resources Files =        0 bytes (       0 elements)

   Resources Data  =        0 bytes

   Strings         =      667 bytes

   Signatures      =      239 bytes

   ByteCode        =     1118 bytes




Attaching deployed file.

Assembly: Scratchpad (1.0.0.0) (328 RAM - 1344 ROM - 566 METADATA)


   AssemblyRef    =       12 bytes (       3 elements)

   TypeRef        =       56 bytes (      14 elements)

   FieldRef       =        0 bytes (       0 elements)

   MethodRef      =       76 bytes (      19 elements)

   TypeDef        =       24 bytes (       3 elements)

   FieldDef       =        4 bytes (       2 elements)

   MethodDef      =       12 bytes (       5 elements)



   Attributes      =        0 bytes (       0 elements)

   TypeSpec        =        0 bytes (       0 elements)

   Resources       =       16 bytes (       2 elements)

   Resources Files =        8 bytes (       1 elements)

   Resources Data  =       13 bytes

   Strings         =      274 bytes

   Signatures      =       98 bytes

   ByteCode        =      317 bytes




Attaching deployed file.

Assembly: Microsoft.SPOT.Hardware (4.1.2821.0) (1752 RAM - 11440 ROM - 7371 METADATA)


   AssemblyRef    =        8 bytes (       2 elements)

   TypeRef        =      124 bytes (      31 elements)

   FieldRef       =       24 bytes (       6 elements)

   MethodRef      =      120 bytes (      30 elements)

   TypeDef        =      496 bytes (      62 elements)

   FieldDef       =      176 bytes (      88 elements)

   MethodDef      =      444 bytes (     222 elements)



   Attributes      =        0 bytes (       0 elements)

   TypeSpec        =        0 bytes (       0 elements)

   Resources       =        0 bytes (       0 elements)

   Resources Files =        0 bytes (       0 elements)

   Resources Data  =        0 bytes

   Strings         =     1329 bytes

   Signatures      =     1067 bytes

   ByteCode        =     2611 bytes




Attaching deployed file.

Assembly: FEZPanda_GHIElectronics.NETMF.FEZ (4.1.3.0) (380 RAM - 1332 ROM - 729 METADATA)


   AssemblyRef    =       12 bytes (       3 elements)

   TypeRef        =       68 bytes (      17 elements)

   FieldRef       =        0 bytes (       0 elements)

   MethodRef      =       40 bytes (      10 elements)

   TypeDef        =       88 bytes (      11 elements)

   FieldDef       =       16 bytes (       8 elements)

   MethodDef      =       12 bytes (       5 elements)



   Attributes      =        0 bytes (       0 elements)

   TypeSpec        =        0 bytes (       0 elements)

   Resources       =       16 bytes (       2 elements)

   Resources Files =        8 bytes (       1 elements)

   Resources Data  =       13 bytes

   Strings         =      270 bytes

   Signatures      =       69 bytes

   ByteCode        =      146 bytes




Attaching deployed file.

Assembly: Microsoft.SPOT.Native (4.1.2821.0) (1144 RAM - 6516 ROM - 4479 METADATA)


   AssemblyRef    =        4 bytes (       1 elements)

   TypeRef        =       80 bytes (      20 elements)

   FieldRef       =        0 bytes (       0 elements)

   MethodRef      =       60 bytes (      15 elements)

   TypeDef        =      336 bytes (      42 elements)

   FieldDef       =      192 bytes (      95 elements)

   MethodDef      =      224 bytes (     111 elements)



   Attributes      =       48 bytes (       6 elements)

   TypeSpec        =        0 bytes (       0 elements)

   Resources       =       72 bytes (       9 elements)

   Resources Files =        8 bytes (       1 elements)

   Resources Data  =      747 bytes

   Strings         =      648 bytes

   Signatures      =      595 bytes

   ByteCode        =      418 bytes




Resolving.


Total: (6804 RAM - 57308 ROM - 33806 METADATA)


   AssemblyRef    =       44 bytes (      11 elements)

   TypeRef        =      424 bytes (     106 elements)

   FieldRef       =       24 bytes (       6 elements)

   MethodRef      =      376 bytes (      94 elements)

   TypeDef        =     2072 bytes (     259 elements)

   FieldDef       =      692 bytes (     344 elements)

   MethodDef      =     2356 bytes (    1175 elements)



   DebuggingInfo  =     1188 bytes



   Attributes      =       48 bytes (       6 elements)

   TypeSpec        =       16 bytes (       4 elements)

   Resources Files =      120 bytes (       5 elements)

   Resources       =      336 bytes (      42 elements)

   Resources Data  =     1210 bytes

   Strings         =     4739 bytes

   Signatures      =     4194 bytes

   ByteCode        =    16312 bytes




The debugging target runtime is loading the application assemblies and starting execution.
Ready.

'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.1\Assemblies\le\mscorlib.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.1\Assemblies\le\Microsoft.SPOT.Native.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.1\Assemblies\le\Microsoft.SPOT.Hardware.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.1\Assemblies\le\Microsoft.SPOT.Hardware.SerialPort.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Documents and Settings\p.nanda\my documents\visual studio 2010\Projects\Scratchpad\Scratchpad\bin\Debug\le\Scratchpad.exe', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\GHI Electronics\GHI NETMF v4.1 SDK\Assemblies\le\FEZPanda_GHIElectronics.NETMF.FEZ.dll'
The thread '<No Name>' (0x2) has exited with code 0 (0x0).
COM port opened
    #### Exception System.InvalidOperationException - 0x00000000 (1) ####
    #### Message: 
    #### System.IO.Ports.SerialPort::set_ReadTimeout [IP: 0009] ####
    #### MFConsoleApplication1.Program::Main [IP: 0035] ####
A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.SPOT.Hardware.SerialPort.dll
An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.SPOT.Hardware.SerialPort.dll

 Uncaught exception 
The thread '<No Name>' (0x1) has exited with code 0 (0x0).

This is my full program:

using System.Threading;
using System.IO.Ports;
using System.Text;
using Microsoft.SPOT;
using GHIElectronics.NETMF.FEZ;

namespace MFConsoleApplication1
{
  public class Program
  {
    public static void Main()
    {
      SerialPort UART = new SerialPort( "COM1", 115200 );
      int read_count = 0;
      byte[] tx_data;
      byte[] rx_data = new byte[ 10 ];
      tx_data = Encoding.UTF8.GetBytes( "FEZ" );
      string rx_string = "";
      UART.Open();
      Debug.Print( "COM port opened" );
      UART.ReadTimeout = 0;
      while ( true )
      {
        // flush all data
        UART.Flush();
        // send some data
        Debug.Print( "Writing 'FEZ'" );
        UART.Write( tx_data, 0, tx_data.Length );
        // wait to make sure data is transmitted
        Thread.Sleep( 100 );
        // read the data
        Debug.Print( "Reading 'FEZ'" );
        read_count = UART.Read( rx_data, 0, rx_data.Length );
        Debug.Print( "Read data" );
        rx_string = rx_data + "\r\n";
        Debug.Print( rx_string.ToString() );
        
        if ( read_count != 3 )
        {
          // we sent 3 so we should have 3 back
          Debug.Print( "Wrong size: " + read_count.ToString() );
        }
        else
        {
          Debug.Print( rx_data.ToString() );
          // the count is correct so check the values
          // I am doing this the easy way so the code is more clear
          if ( tx_data[ 0 ] == rx_data[ 0 ] )
          {
            if ( tx_data[ 1 ] == rx_data[ 1 ] )
            {
              if ( tx_data[ 2 ] == rx_data[ 2 ] )
              {
                Debug.Print( "Perfect data!" );
              }
            }
          }
        }
        
        Thread.Sleep( 100 );
      }
    }
  }
}

Set timeout before you open the port.

Wrong:


      ...
      UART.Open();
      Debug.Print( "COM port opened" );
      UART.ReadTimeout = 0;
      ...

Correct:


      ...
      UART.ReadTimeout = 0;
      UART.Open();
      Debug.Print( "COM port opened" );      
      ...

That was it.
Thank you!


      SerialPort UART = new SerialPort( "COM1", 115200 );
      UART.ReadTimeout = 0;
      int read_count = 0;
      byte[] tx_data;
      byte[] rx_data = new byte[ 10 ];
      tx_data = Encoding.UTF8.GetBytes( "FEZ" );
      string rx_string = "";
      UART.Open();
      Debug.Print( "COM port opened" );

You are welcome! :wink: