me again, :
after much playing i have finally got the code to work however im not to happy with the solution i have come up with. it seems within the datareceived event if i slept the event for a small time then the code would work, i discovered this by accident to be honest, if i placed a break point at the start of the event which i was doing for testing and looking at byte counts in and out i discovered the code worked however without a breakpoint the event only ran the once and never retriggers. now i
m aware that by not having a time i will get partial message returned because the event get triggered multiple times, however if i try to implement any of the previous codes or try to and while loops inside the datareceived event things seem to go wrong. like i say i`m not to happy with my implementation but i dont see a way round it at the moment.
using System;
using System.Threading;
using System.IO.Ports;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
namespace FEZ_Domino_Application2
{
public class Program
{
static SerialPort uart = new SerialPort("COM1");
static int Count = 0;
public static void Main()
{
uart.Open();
if (uart.IsOpen)
{
uart.DataReceived += new SerialDataReceivedEventHandler(Test_DataReceived);
}
byte[] TX_Data = Encoding.UTF8.GetBytes("$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68"); // switched data out for testing // 0123456789
while (true)
{
uart.Write(TX_Data, 0, TX_Data.Length);
Thread.Sleep(1000);
}
}
static void Test_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(100);
if (uart.BytesToRead > 0)
{
Count += 1;
Debug.Print("Bytes to read before uart read: " + uart.BytesToRead.ToString());
byte[] ByteBuffer = new byte[256];
uart.Read(ByteBuffer, 0, uart.BytesToRead);
string RX_Data = new string(System.Text.Encoding.UTF8.GetChars(ByteBuffer));
Debug.Print(Count + ": Data: " + RX_Data);
Debug.Print("Bytes to read after uart read: " + uart.BytesToRead.ToString());
Debug.Print("************************************");
}
}
}
}
Bytes to read before uart read: 66
1: Data: $GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68
Bytes to read after uart read: 0
Bytes to read before uart read: 66
2: Data: $GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68
Bytes to read after uart read: 0
Bytes to read before uart read: 66
3: Data: $GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68
Bytes to read after uart read: 0
Bytes to read before uart read: 66
4: Data: $GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68
Bytes to read after uart read: 0
Bytes to read before uart read: 66
5: Data: $GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68
Bytes to read after uart read: 0