Main Site Documentation

GHI E-Book Loop-Back Test on COBRA under .NET MF 4.1


#1

I’m unable to get the GHI E-Book Loop-Back Test to work on a COBRA under .NET MF 4.1 .
I had to comment-out the ReadTimout line because it was causing an Invalid-Operation-Execption Error at run-time. The progam gets down to the comport read statement and then hangs. Thank you for any help you can give this Newbie.


using System.Threading;
using System.IO.Ports;
using System.Text;
using Microsoft.SPOT;

namespace MFConsoleApplication1
{                                 //  ComPort Loop-Back Test From GHI Beginners E-Book
    public class Program          //  Problems Running on FEZ COBRA under .NET MF 4.1
    {
        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");
            UART.Open();
          //  UART.ReadTimeout = 0; // ** This line causes Run-Time Error - InvalidOperationExecption **
                                    // ** Therefore, I've commented-out the ReadTimeout line          **
            while (true)
            {
                // flush all data
                UART.Flush();
                // send some data
                UART.Write(tx_data, 0, tx_data.Length);
                // wait to make sure data is transmitted
                Thread.Sleep(100);
                // read the data
                Debug.Print("About to attempt read"); // ** This Line Prints with above ReadTimeout line commented-out **
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                Debug.Print("Read Complete"); // *** Program Hangs Before This Line ***
                if (read_count != 3)
                {
                    // we sent 3 so we should have 3 back
                    Debug.Print("Wrong size: " + read_count.ToString());
                }
                else
                {
                    // 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);
            }
        }
    }
}


#2

This is a sore topic somewhere else on this forum…

It’s bad practice to WAIT for a serial port to receive characters the cobra willl sit there all day

I believe this code expects you to bridge Txd and Rxd on com1 then send and receive on the same port!!! There is no error checking what so ever… If the ReadTimeout is creating a run time error, there is some thing wrong… Try a value instead of 0.

Just check the ebook for obvious mistakes

Cheers Ian


#3

you wrote three characters and the ou are doing a read for ten characters.


#4

Ian,
Thanks for giving my Loop-Back issue some thought.
Yes, I have bridged the TX & RX pins of COM1.
I’ve tried the ReadTimeout = 500, and that still produces
the InvalidOperationExecption run-time error.
I don’t know what else to try?
Thanks,
Carl


#5

Mike,
You appear to be on the right track.
The SerialPort.Read method appears to work differently than contemplated in the E-Book Loop-Back example.
I’ll play more with it when I have more time.
Thanks,
Carl


#6

However, I still don’t know why the ReadTimeout property causes a InvalidOperationExecption run-time error. This my explain why the Read method appears to operate differently than in the Loop-Back example.
Carl


#7

I wrote the code but didn’t test it :-[ I guess I need to give it a try and update the book.


#8

Was having the same problem myself, changing the rx byte array to 3 instead of 10 fixed it for me.

No run-time error for me though.


#9

Now with book on wiki, anyone can fix this