This code example demonstrates some advanced operations using the serial (RS232) port on a GHI Electronics FEZ board. In this case the code is expecting that certain data is sent into the board. If the data sent it will respond with “Perfect Size” otherwise “Wrong Size…”.
This code is written in C# and assumes the development workstation has all required software installed.
using System;
using System.Text;
using System.IO.Ports;
using System.Threading;
using Microsoft.SPOT;
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");
UART.ReadTimeout = 0;
UART.Open();
while (true)
{
UART.Flush();
UART.Write(tx_data, 0, tx_data.Length);
Thread.Sleep(100);
read_count = UART.Read(rx_data, 0, rx_data.Length);
if (read_count != 3)
{
Debug.Print("Wrong size: " + read_count.ToString());
}
else
{
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);
}
}
}
}
For more information on specific namespaces please refer to the following resources: