Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

nturpin77

Member Since 06 Jan 2011
Offline Last Active Aug 07 2012 11:35 AM
-----

Topics I've Started

N+ Serial Read Frequency

11 May 2011 - 12:45 PM

Good Day all,

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++;
                        }
                        
       
                    
                }

Elapsed time

06 April 2011 - 02:09 PM

Is there an easy way to get an elapsed time? Ive read too many different code snippets and now have myself crosseyed. Im trying to just use DateTime.Now at the begining of the code and then subract it from DateTime.Now at the end but its not working too well for me atm. Thanks N

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.