Ok So I’m new to this forum and FEZ in general, but am familiar with C# and .NET
So I had a couple of question I couldn’t seem to find in the forum or on the web site all regarding the serial port.
Question 1: What is the buffer size of the UARTs on the panda?
Question 2: So I’m writing some event driven software to parses MIDI data coming in on COM1. It seems to work fine except when a lot of data rushes into the port at once and also at other time sort of randomly.
using System;
using System.Threading;
using System.IO.Ports;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
namespace FEZ_Panda_MIDI
{
public class Program
{
public static void Main()
{
Program MyProgram = new Program();
MyProgram.begin("COM1", 0);
while (true)
{
Thread.Sleep(1000);
}
}
public void begin(string port, byte Channel)
{
SerialPort UART;
UART = new SerialPort(port, 31250);
UART.DataReceived += new SerialDataReceivedEventHandler(DataReceviedHandler);
UART.ReadTimeout = 0;
UART.Open();
}
private static void DataReceviedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort UART = (SerialPort)sender;
if (UART.BytesToRead > 128)
{
UART.Flush();
}
while (UART.BytesToRead > 0)
{
byte[] mData = new byte[UART.BytesToRead];
byte highNibble = 0;
byte lowNibble = 0;
string type;
int channel = 0;
int data1 = 0;
int data2 = 0;
UART.Read(mData, 0, UART.BytesToRead); //Read all the data in the buffer
//loop through all data from serial buffer
for (int i = 0; i < mData.Length; i++)
{
//check if the current byte we are checking is a statis byte
if (mData[i] >= 128)
{
highNibble = (byte)(mData[i] >> 4); //get high nibble (message type)
lowNibble = (byte)(mData[i] & 15); //get low nibble (MIDI channel);
channel = lowNibble;
switch (highNibble)
{
case 8: //Note off event
type = "NoteOff";
data1 = mData[i + 1];
data2 = mData[i + 2];
Debug.Print("Channel " + channel + " " + type + " Data1 = " + data1 + " Data2 = " + data2 + "\n");
break;
case 9: //Note on event
type = "NoteOn";
data1 = mData[i + 1];
data2 = mData[i + 2];
Debug.Print("Channel " + channel + " " + type + " Data1 = " + data1 + " Data2 = " + data2 + "\n");
break;
case 14: //pitch bend event
type = "PitchBend";
data1 = mData[i + 1];
data2 = mData[i + 2];
Debug.Print("Channel " + channel + " " + type + " Data1 = " + data1 + " Data2 = " + data2 + "\n");
break;
}
}
}
}
}
}
}
This is the error I get:
#### Exception System.ArgumentOutOfRangeException - CLR_E_OUT_OF_RANGE (3) ####
#### Message:
#### System.IO.Ports.SerialPort::Read [IP: 0000] ####
#### FEZ_Panda_MIDI.Program::DataReceviedHandler [IP: 0044] ####
#### System.IO.Ports.SerialPort::DataEventHandler [IP: 0012] ####
A first chance exception of type ‘System.ArgumentOutOfRangeException’ occurred in Microsoft.SPOT.Hardware.SerialPort.dll
An unhandled exception of type ‘System.ArgumentOutOfRangeException’ occurred in Microsoft.SPOT.Hardware.SerialPort.dll
It is coming from the line “UART.Read(mData, 0, UART.BytesToRead);”
Something went out of bonds and I’m not sure exactly what. It would help if I knew a bit more about what the error is implying.
[EDIT]
Also I occupationally seem to be getting this in the debug window as well:
GC: 2msec 13428 bytes used, 50952 bytes available
Type 0F (STRING ): 240 bytes
Type 11 (CLASS ): 960 bytes
Type 12 (VALUETYPE ): 108 bytes
Type 13 (SZARRAY ): 408 bytes
Type 15 (FREEBLOCK ): 50952 bytes
Type 16 (CACHEDBLOCK ): 120 bytes
Type 17 (ASSEMBLY ): 8040 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 19 (REFLECTION ): 24 bytes
Type 1B (DELEGATE_HEAD ): 144 bytes
Type 1D (OBJECT_TO_EVENT ): 72 bytes
Type 1E (BINARY_BLOB_HEAD ): 252 bytes
Type 1F (THREAD ): 768 bytes
Type 20 (SUBTHREAD ): 96 bytes
Type 21 (STACK_FRAME ): 1332 bytes
Type 27 (FINALIZER_HEAD ): 96 bytes
Type 31 (IO_PORT ): 108 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 540 bytes
I’m not sure if it’s an error or not some explanation of this would be great.
Any help would be great!
Eric