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.
Photo

N+ Serial Read Frequency


  • Please log in to reply
5 replies to this topic

#1 nturpin77

nturpin77

    Advanced Member

  • Members
  • PipPipPip
  • 42 posts
  • LocationNewfoundland, Canada

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


#2 Michel Trahan

Michel Trahan

    Advanced Member

  • Members
  • PipPipPip
  • 155 posts

Posted 11 May 2011 - 02:50 PM

My first thought would be : writting to an SD card is SLOW ...
Started with C in 1985, moved to Vb3 ... to vb6 and stopped. Now started with .Net and learning C# and VB.net and wishing VB.net was on MF !

#3 Tecchie

Tecchie

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationGermany

Posted 11 May 2011 - 05:26 PM

    port1.Write(GPSbuffer, 0, GPSbuffer.Length);
    if (GPSbuffer[0] != 36)
        continue;


Those two lines seem a bit strange. First line: Why do you try to write the data back to the GPS?
Second line: are you sure that you want to skip all of the remaining part of the while-loop (including the reading from the IMU)?

#4 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 11 May 2011 - 06:18 PM

Have you thought about using the Stopwatch class to put some timings in place? You could at least narrow it down to data collection or data formatting for writing to the SD card.

Regards,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#5 nturpin77

nturpin77

    Advanced Member

  • Members
  • PipPipPip
  • 42 posts
  • LocationNewfoundland, Canada

Posted 11 May 2011 - 06:24 PM

Good Afternoon and thanks for the info, some things to think about. @Nevyn - I have the Stopwatch class in right now for a different application but I will look at using it for data collection/formatting. Makes sense. @Tecchie - The Write line is there to output over a serial RS232 chip so I can read the data on a laptop or other computer real time, or near enough. As for the other line I found that without looking for the $ the code would always crash, I guess the better question for this line of code is why is it crashing without this line. Thanks again I will post results soon.......

#6 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 11 May 2011 - 09:38 PM

The SD card access is surprisingly fast (although writing may be slow--depending on the SD card). You should be able to read/write to the serial port at 115200 or faster. We're currently working on a product using the Netduino firmware which reads/writes to two serial ports pretty quickly. That said your limitation may be the amount of processing done on the code or the SD card speed, etc. Sounds like a fun performance tuning project :) Chris




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

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.