Hi everyone
I’m trying to use the SerialDataReceivedEventHandler to receive data at 56700 Bps. Data are sent by a 9dof Razor IMU.
I instantiate the serial port, open it and then subscribe to the event. Data are received properly untill reaching the empty while(true) loop.
Here’s the code:
using System.Threading;
using System.IO.Ports;
using System.IO;
using System.Text;
using Microsoft.SPOT;
namespace EMX_Application1
{
public class Program
{
static SerialPort UART = new SerialPort("COM2", 57600);
static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] buffer = new byte[UART.BytesToRead];
UART.Read(buffer, 0, buffer.Length);
UART.DiscardInBuffer();
UART.Flush();
string output_imu = bytesToString(buffer);
Debug.Print("IMU:" + output_imu);
output_imu = null;
buffer = null;
}
public static string bytesToString(byte[] bytes)
{
string s = "";
for (int i = 0; i < bytes.Length; ++i)
s += (char)bytes[i];
return s;
}
static void send_cmd(char byte_snd, int time_wait)
{
byte[] buffer = new byte[1];
buffer[0] = (byte)byte_snd;
UART.Write(buffer, 0, 1);
buffer = null;
Thread.Sleep(time_wait);
}
public static void Main()
{
UART.ReadTimeout = 10;
UART.Open();
UART.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
send_cmd('4', 200);
while (true)
{
Debug.Print("sleep for 10ms");
Thread.Sleep(10);
}
}
}
}
And the debug screen shows:
[quote]‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managé) : ‘C:\Program Files\Microsoft .NET Micro Framework\v4.1\Assemblies\le\mscorlib.dll’ chargé, symboles chargés.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managé) : ‘C:\Program Files\Microsoft .NET Micro Framework\v4.1\Assemblies\le\Microsoft.SPOT.Native.dll’ chargé, symboles chargés.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managé) : ‘C:\Program Files\Microsoft .NET Micro Framework\v4.1\Assemblies\le\Microsoft.SPOT.Hardware.dll’ chargé, symboles chargés.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managé) : ‘C:\Program Files\Microsoft .NET Micro Framework\v4.1\Assemblies\le\Microsoft.SPOT.Hardware.SerialPort.dll’ chargé, symboles chargés.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managé) : ‘C:\Documents and Settings\Jeremie\Bureau\EMXIMU\EMX Application1\EMX Application1\bin\Debug\le\EMX Application1.exe’ chargé, symboles chargés.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managé) : ‘C:\Program Files\Microsoft .NET Micro Framework\v4.1\Assemblies\le\Microsoft.SPOT.Net.dll’ chargé, symboles chargés.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managé) : ‘C:\Program Files\Microsoft .NET Micro Framework\v4.1\Assemblies\le\System.dll’ chargé, symboles chargés.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managé) : ‘C:\Program Files\Microsoft .NET Micro Framework\v4.1\Assemblies\le\Microsoft.SPOT.IO.dll’ chargé, symboles chargés.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managé) : ‘C:\Program Files\Microsoft .NET Micro Framework\v4.1\Assemblies\le\System.IO.dll’ chargé, symboles chargés.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managé) : ‘C:\Program Files\GHI Electronics\GHI NETMF v4.1 SDK\Assemblies\le\GHIElectronics.NETMF.Hardware.EMX.dll’ chargé
Le thread ‘’ (0x2) s’est arrêté avec le code 0 (0x0).
IMU:
9DOF IMU Firmware v20
==========================
[1]Accelerometer: ADXL345
[2]Magnetometer: HMC5843
[3]Gyroscope: ITG-3200
[4]Raw Output
[5]Change Baud Rate: 57600bps
[Ctrl+z]Toggle Autorun
[?]Help
$23,4,240,-11,1,80,-85,-21,-24#
IMU:$24,5,239,-12,5,76,53,-161,11#
$22,3,241,-12,4,81,-272,-53,18#
$24,5,241,-14,2,77,-1,-25,162#
IMU:$24,4,237,-11,2,80,-138,130,-260#
sleep for 10ms
IMU:$
IMU:23,4,
IMU:240,-15,2,
IMU:77,-43,-224,71#
sleep for 10ms
sleep for 10ms
sleep for 10ms
IMU:$
IMU:24,3,239,
IMU:-11,0,77,-79,-6,348#
IMU:
sleep for 10ms
sleep for 10ms
sleep for 10ms
sleep for 10ms
sleep for 10ms
sleep for 10ms
sleep for 10ms
sleep for 10ms
sleep for 10ms
sleep for 10ms
sleep for 10ms
sleep for 10ms
sleep for 10ms
sleep for 10ms
sleep for 10ms
sleep for 10ms
sleep for 10ms
sleep for 10ms
[/quote]
It seems that the loop does not allow the events to be managed properly… Am I wrong or misunderstand anything?