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);
}
}
}
}