Give this a whirl… it’s overkill and takes some code from one of my projects, but it should prove that you have comms working on a port.
using System;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF;
namespace FEZ_Panda_II_Application1
{
public class Program
{
static SerialBuffer serialBuffer = new SerialBuffer(256);
static SerialPort com1;
static int delay_1interval = 1000;
static int delay_2interval = delay_1interval * 2;
static int myserialtimeout = 2000;
public static void Main()
{
// Blink board LED
bool ledState = false;
string command;
string response;
com1 = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
com1.ReadTimeout = myserialtimeout;
com1.Open();
OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, ledState);
while (true)
{
// Sleep for 5000 milliseconds
Thread.Sleep(5000);
// toggle LED state
ledState = !ledState;
led.Write(ledState);
command = "Hello PC, are you awake??\r\n";
Debug.Print("about to send " + command);
response = SendReceive(com1, command);
Debug.Print("Got back " + response);
}
}
private static string SendReceive(SerialPort port, string request)
{
port.Write(Encoding.UTF8.GetBytes(request), 0, request.Length);
string line = ReadLine(port, 13, 300); // \r is line seperator.
// if line starts with ", then keep reading lines until line that ends with
return line;
}
public static string ReadLine(SerialPort port, int delim = 10, int maxBufSize = 300)
{
//CR 13 LF 10 = "\r\n"
if (maxBufSize <= 0) throw new ArgumentOutOfRangeException("maxBufSize");
byte[] buf = new byte[maxBufSize];
int curPos = 0;
while (curPos < maxBufSize)
{
int read = port.Read(buf, curPos, 1);
if (read == 0) break;
if (buf[curPos] == delim) //this won't work for multiline responses since this flushes anything left unread
{
port.Flush();
break;
}
curPos += read;
}
// curPos will be zero if first read is zero or first read is delim byte.
if (curPos == 0) return null;
if (curPos == maxBufSize)
throw new InvalidOperationException("Line size > maxBufSize.");
if (buf[curPos] != delim)
{
port.Flush();
return null; // if no delim found, assume comms failure. drop the current reading and move on
}
buf[curPos] = 0;
char[] ca = Encoding.UTF8.GetChars(buf);
return new string(ca);
}
}
public class SerialBuffer
{
private System.Text.Decoder decoder = System.Text.UTF8Encoding.UTF8.GetDecoder();
private byte[] buffer;
private int startIndex = 0;
private int endIndex = 0;
private char[] charBuffer;
public SerialBuffer(int initialSize)
{
buffer = new byte[initialSize];
charBuffer = new char[256];
}
public void LoadSerial(SerialPort port)
{
int bytesToRead = port.BytesToRead;
//Debug.Print("to read:" + bytesToRead);
if (bytesToRead > 0)
{
if (buffer.Length < endIndex + bytesToRead) // do we have enough buffer to hold this read?
{
// if not, look and see if we have enough free space at the front
if (buffer.Length - DataSize >= bytesToRead)
{
ShiftBuffer();
}
else
{
// not enough room, we'll have to make a bigger buffer
ExpandBuffer(DataSize + bytesToRead);
}
}
//Debug.Print("serial buffer load " + bytesToRead + " bytes read, " + DataSize + " buffer bytes before read");
port.Read(buffer, endIndex, bytesToRead);
endIndex += bytesToRead;
//Debug.Print("serial buffer loaded " + bytesToRead + " bytes read, " + DataSize + " buffer bytes after read");
}
else
{
Debug.Print("serial read empty");
}
}
private void ShiftBuffer()
{
// move the data to the left, reclaiming space from the data already read out
Array.Copy(buffer, startIndex, buffer, 0, DataSize);
endIndex = DataSize;
startIndex = 0;
}
private void ExpandBuffer(int newSize)
{
byte[] newBuffer = new byte[newSize];
Array.Copy(buffer, startIndex, newBuffer, 0, DataSize);
buffer = newBuffer;
endIndex = DataSize;
startIndex = 0;
}
public byte[] Buffer
{
get
{
return buffer;
}
}
public int DataSize
{
get
{
return endIndex - startIndex;
}
}
public string ReadLine()
{
lock (buffer)
{
int lineEndPos = Array.IndexOf(buffer, '\r', startIndex, DataSize);
// HACK: not looking for \r, just assuming that they'll come together
if (lineEndPos > 0)
{
int lineLength = lineEndPos - startIndex;
if (charBuffer.Length < lineLength) // do we have enough space in our char buffer?
{
charBuffer = new char[lineLength];
}
int bytesUsed, charsUsed;
bool completed;
decoder.Convert(buffer, startIndex, lineLength, charBuffer, 0, lineLength, true, out bytesUsed, out charsUsed, out completed);
string line = new string(charBuffer, 0, lineLength);
startIndex = lineEndPos + 1;
Debug.Print("found string length " + lineLength + "; new buffer = " + startIndex + " to " + endIndex);
return line;
}
else
{
return null;
}
}
}
public bool GotEOL(char terminator) //
{
if (startIndex != endIndex)
{
if (startIndex + 1 != endIndex)
{
return true;
}
else
{
return true;
}
}
else
{
return false;
}
}
public bool GotEOL(byte terminator) //
{
if (startIndex != endIndex)
{
if (buffer[endIndex - 1] == terminator)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public bool GotEOL(byte[] terminator) //
{
if (terminator.Length > 2) throw new IndexOutOfRangeException("terminator must be 2 or less bytes");
if (startIndex != endIndex)
{
if (endIndex - startIndex > terminator.Length)
{
return false;
}
else
{
if (buffer[endIndex - 1] == 0x0D && buffer[endIndex - 2] == 0x22)
{
return true;
}
else
{
return false;
}
}
}
else
{
return false;
}
}
public void DumpBuf()
{
string hexline = "0123456789ABCDEF";
if (startIndex != endIndex)
{
lock (buffer)
{
int lineLength = endIndex - startIndex;
for (int i = 0; i < lineLength; i++)
{
byte j = buffer[i + startIndex];
Debug.Print("char=" + i + " Hex=" + hexline[(j & 0xF0) >> 4] + hexline[j & 0x0F]);
}
startIndex = endIndex;
}
}
}
}
}