My question of the day is, is there a limit to the frequency that serial read can do. I have an IMU pack that outputs at 20Hz but when I read/log the data to the SD card we get about 1/3 of that. Obviously there is a limit to how fast the N+ can read from the serial UART but I wouldnt have thought it was 20Hz or less. Here is my code that reads/writes. We would like to read as much data as possible from the IMU, we can change the frequency up to 100Hz but 20Hz is as low as it can go.
public static void Main() { SerialPort port1 = new SerialPort("COM1", 57600, Parity.None, 8, StopBits.One); port1.Open(); SerialPort Port2 = new SerialPort("COM2", 115200, Parity.None, 8, StopBits.One); Port2.Open(); OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); FileStream fs = new FileStream("\\SD\\CTAP.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 512); StreamWriter sw = new StreamWriter(fs); //int j = 0; double currentTime = 0; double lastTime = 0; //ArrayList accz = new ArrayList(); Stopwatch timing = Stopwatch.StartNew(); while (true) { int GPStoRead = port1.BytesToRead; //start reading the stream if (GPStoRead > 0) { //blink the LED to show we got data led.Write(true); //Thread.Sleep(100); led.Write(false); // get the waiting data byte[] GPSbuffer = new byte[GPStoRead]; port1.Read(GPSbuffer, 0, GPSbuffer.Length); port1.Write(GPSbuffer, 0, GPSbuffer.Length); if (GPSbuffer[0] != 36) continue; String GPSMessage = new String(System.Text.Encoding.UTF8.GetChars(GPSbuffer)); Debug.Print(GPSMessage); sw.Write(GPSMessage); } int CHRtoRead = Port2.BytesToRead; if (CHRtoRead > 37) { int i = 0; //blink the LED to show we got data led.Write(true); //Thread.Sleep(100); led.Write(false); // get the waiting data byte[] CHRbuffer = new byte[CHRtoRead]; byte[] CHRoutput = new byte[20]; //serialPort2.Flush(); Port2.Read(CHRbuffer, 0, CHRbuffer.Length); if (CHRbuffer[i] == 's') { //Print final result i = i + 7; YawVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16); String ShowYaw = YawVal.ToString(); YawCalc = YawVal * 0.0109863; String ShowYCalc = YawCalc.ToString("F2"); PitchVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16); String ShowPitch = PitchVal.ToString(); PitchCalc = PitchVal * 0.0109863; String ShowPCalc = PitchCalc.ToString("F2"); RollVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16); String ShowRoll = RollVal.ToString(); RollCalc = RollVal * 0.0109863; String ShowRCalc = RollCalc.ToString("F2"); string CTAP = "$CTAP,R," + ShowRCalc + ",P," + ShowPCalc + ",Y," + ShowYCalc + "\r\n"; sw.Write(CTAP); CHRoutput = Encoding.UTF8.GetBytes(CTAP); Port2.Write(CHRoutput, 0, CHRoutput.Length); YRateVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16); String ShowYRate = YRateVal.ToString(); YRateCalc = YRateVal * 0.0137329; String ShowYRateCalc = YRateCalc.ToString("F2"); PRateVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16); String ShowPRate = PRateVal.ToString(); PRateCalc = PRateVal * 0.0137329; String ShowPRateCalc = PRateCalc.ToString("F2"); RRateVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16); String ShowRRate = RRateVal.ToString(); RRateCalc = RRateVal * 0.0137329; String ShowRRateCalc = RRateCalc.ToString("F2"); xMagVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16); String ShowxMag = xMagVal.ToString(); xMagCalc = xMagVal * 0.061035; String ShowxMagCalc = xMagCalc.ToString("F2"); yMagVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16); String ShowyMag = yMagVal.ToString(); yMagCalc = yMagVal * 0.061035; String ShowyMagCalc = yMagCalc.ToString("F2"); zMagVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16); String ShowzMag = zMagVal.ToString(); zMagCalc = zMagVal * 0.061035; String ShowzMagCalc = zMagCalc.ToString("F2"); xGyroVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16); String ShowxGyro = xGyroVal.ToString(); xGyroCalc = xGyroVal * 0.01812; String ShowxGyroCalc = xGyroCalc.ToString("F2"); yGyroVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16); String ShowyGyro = yGyroVal.ToString(); yGyroCalc = yGyroVal * 0.01812; String ShowyGyroCalc = yGyroCalc.ToString("F2"); zGyroVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16); String ShowzGyro = zGyroVal.ToString(); zGyroCalc = zGyroVal * 0.01812; String ShowzGyroCalc = zGyroCalc.ToString("F2"); xAccVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16); String ShowxAcc = xAccVal.ToString(); xAccCalc = xAccVal * 0.106812; String ShowxAccCalc = xAccCalc.ToString("F2"); yAccVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16); String ShowyAcc = yAccVal.ToString(); yAccCalc = yAccVal * 0.106812; String ShowyAccCalc = yAccCalc.ToString("F2"); currentTime = timing.ElapsedMilliseconds; zAccVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16); String ShowzAcc = zAccVal.ToString(); zAccCalc = zAccVal * 0.106812; //accz.Add(zAccCalc); String ShowzAccCalc = zAccCalc.ToString("F2"); string CTSens = "$CTSens,Rates,Y," + ShowYRateCalc + ",P," + ShowPRateCalc + ",R," + ShowRRateCalc + ",Mags,X," + ShowxMagCalc + ",Y," + ShowyMagCalc + ",Z," + ShowzMagCalc + ",Gyros,X," + ShowyGyroCalc + ",Y," + ShowxGyroCalc + ",Z," + ShowzGyroCalc + ",Accels,X," + ShowxAccCalc + ",Y," + ShowyAccCalc + ",Z," + ShowzAccCalc + "\r\n"; sw.Write(CTSens); } else { i++; } }