I simply test the UART on FEZ Cerbuino Bee, connect tx to rx.
Write string “test” to SerialPort.
Read data use UART_DataReceived event.
But only first time I get the string “test”, then get wrong.
Debug.output
test
t
e
st
t
e
st
t
e
st
t
e
st
t
e
st
public partial class Program
{
// This method is run when the mainboard is powered up or reset.
SerialPort UART;
void ProgramStarted()
{
GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
// Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
Debug.Print("Program Started");
UART = new SerialPort("COM3", 19200);
UART.DataReceived += new SerialDataReceivedEventHandler(UART_DataReceived);
UART.ReadTimeout = 0;
UART.Open();
timer.Start();
}
void UART_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int btr = UART.BytesToRead;
if (btr == 0) return;
byte[] bytes = new byte[btr];
UART.Read(bytes, 0, btr);
string Text = new string(System.Text.Encoding.UTF8.GetChars(bytes));
Debug.Print(Text);
}
void timer_Tick(GT.Timer timer)
{
byte[] tx_data;
tx_data = Encoding.UTF8.GetBytes("test");
UART.Flush();
UART.Write(tx_data, 0, tx_data.Length);
// wait to make sure data is transmitted
Thread.Sleep(100);
}
}