Hi All, I am creating an application that takes a sample every 10 ms in C#. Well I am using the Windows forms timer and its not accurate at all. Does anyone have a timer they can suggest that is more accurate? Please let me know of the timer and how to program it. Thanks, Laura
laura1316
Posts
-
Help Timer Not Accurate -
Exporting to Excel using C# and Visual Studio 2005Hi All, I have created a project that exports data to Excel. I take some samples and display it using excel. I have 4 columns: time, X, Y, Z data. The X,Y,Z data is integer data and exports perfectly. The time column doesn't work. I create the array and then I try to export it, but for some reason with double it changes all the array to the first time value .1s and if I have it as a string instead it changes all the values in the array to the final read value. Does anyone know how to display the time. I am taking a sample every 100ms so I am trying to do an elapsed timer. Please help. Below is my export to excel function:
private void button2_Click(object sender, EventArgs e) { timerTakeData.Enabled = false; radioButton10.Enabled = true; radioButton100.Enabled = true; radioButton20.Enabled = true; radioButton200.Enabled = true; radioButton50.Enabled = true; button1.Enabled = true; labelTime.Text = "0.00"; Excel.Workbooks ExcelBooks; Excel.Sheets ExcelSheets; Excel._Worksheet ExcelSheet; Excel.Range range; timerTakeData.Enabled = false; radioButton10.Enabled = true; radioButton100.Enabled = true; radioButton20.Enabled = true; radioButton200.Enabled = true; radioButton50.Enabled = true; button1.Enabled = true; labelTime.Text = "0.00"; Excel.Workbooks ExcelBooks; Excel.Sheets ExcelSheets; Excel._Worksheet ExcelSheet; Excel.Range range; try { // Instantiate Excel and start a new workbook. ExcelApp = new Excel.Application(); ExcelBooks = ExcelApp.Workbooks; ExcelBook = ExcelBooks.Add(Missing.Value); ExcelSheets = ExcelBook.Worksheets; ExcelSheet = (Excel._Worksheet)ExcelSheets.get_Item(1); ExcelSheet.Cells[1, 1] = "Time"; ExcelSheet.Cells[1, 2] = "X A/D"; ExcelSheet.Cells[1, 3] = "Y A/D"; ExcelSheet.Cells[1, 4] = "Z A/D"; ExcelSheet.get_Range("A1", "D1").Font.Bold = true; ExcelSheet.get_Range("A1", "D1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter; range = ExcelSheet.get_Range("A2", Missing.Value); range = range.get_Resi
-
Simple math dividingHi all, I am trying to do a simple math division function. I am trying to divide 10/100. This is the line of code: double test = 10/100; I always get 0. I want to be able to get .1, what am I doing wrong? Please help!
-
Deployment Visual Studio 2005 ErrorHi All, I have deployed my project and people are having trouble because the link for the InstallUrl is one that redirects you. http://go.microsoft.com/fwlink/?LinkId=9832 Under the launch conditions in the Microsoft .NET Framework it has a red exclimation point on it. How do I fix this? I programmed in Visual Studio 2005. Should I Allow Later Versions?
-
2 Equations 2 Unknows - How to program in C#?Sorry the equations are actually: 495 = 66t + c 951 = 190t + c Thanks! Please Help! :laugh: Laura
-
2 Equations 2 Unknows - How to program in C#?Hi All: I have the following algebraic equations I want to code in C#. I am not sure how to do this. Please help! 495 = 66t + c 951 = 190 + c How do I write this in code? Thanks, Laura
-
How to call the paint event handlerThanks so much that worked!:-D
-
How to call the paint event handlerHi All, I am tring to call a paint event with a timer so that the points change depending on the location. I am going to have a for loop that incrementally changes the X location. So I am not sure quite how to do this. This is my paint code:
private void PictureBox1_Paint(object sender, PaintEventArgs e) { Filtered_Tilt f = new Filtered_Tilt(); Graphics g = e.Graphics; Size ballsize = new Size(30, 30); Rectangle r = new Rectangle(f.front, ballsize); Pen p = new Pen(Color.Blue, 4); front.X = f.x; front.Y = f.y; g.DrawEllipse(p, r); }
I have a timer function that updates the X I then need to call the paint event to refresh the ellipse on the form. How do I do this? x is a global int variable. front is a global point. ***** GLobal Variable***** public int x, y; public Point front;private void timer1_Tick(object sender, EventArgs e) { for (int i = 0; i < pictureBox1.Width; i++) { x = i; } // How to call the paint event handler to pass it the new x value? }
Thanks, Laura -
Paint Event - ErrorOops sorry, here is the code with the draw ellipse function call. That did fiz 2 errors but I still have 5 more errors. Please help! Error 10 An object reference is required for the nonstatic field, method, or property 'Triax_ReVamp.Filtered_Tilt.front' Error 11 An object reference is required for the nonstatic field, method, or property 'Triax_ReVamp.Filtered_Tilt.front' Error 12 'System.Drawing.Point.x' is inaccessible due to its protection level Error 13 An object reference is required for the nonstatic field, method, or property 'Triax_ReVamp.Filtered_Tilt.front' Error 14 'System.Drawing.Point.y' is inaccessible due to its protection level
Graphics g = e.Graphics; Size ballsize = new Size(30, 30); Rectangle r = new Rectangle(front, ballsize); Pen p = new Pen(Color.Blue, 4); front.x = 176; front.y = 717; g.DrawEllipse(p, r);
Thanks, Laura -
Paint Event - ErrorHi All, I am trying to create a paint event for my project. See below for my code. I am getting a whole bunch of errors. I am new to GDI+, so I am not sure what I am doing wrong. I am writing in C# and using Visual Studio 2005. I have declared public Point front; in my global variables. I added the reference to System.Drawing. What am I doing wrong? Please see below for errors and code. Error 10 An object reference is required for the nonstatic field, method, or property 'Triax_ReVamp.Filtered_Tilt.front' Error 11 An object reference is required for the nonstatic field, method, or property 'Triax_ReVamp.Filtered_Tilt.front' Error 12 'System.Drawing.Point.x' is inaccessible due to its protection level Error 13 An object reference is required for the nonstatic field, method, or property 'Triax_ReVamp.Filtered_Tilt.front' Error 14 'System.Drawing.Point.y' is inaccessible due to its protection level Error 15 The best overloaded method match for 'System.Drawing.Graphics.DrawEllipse(System.Drawing.Pen, System.Drawing.RectangleF)' has some invalid arguments Error 16 Argument '1': cannot convert from 'System.Drawing.Brush' to 'System.Drawing.Pen'
private static void f1_paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; Size ballsize = new Size(30, 30); Rectangle r = new Rectangle(front, ballsize); front.x = 176; front.y = 717; }
Thanks, Laura -
Can I have to PaintEventHandlers in my programHi All, I am trying to draw a rectangle and then draw a circle within the rectangle. At this point I have the rectangle drawing but the circle. The circle within the rectangle is going to move based on motion of the mouse. I am going to try to take the input of the mouse and make those my X and Y coordinates. If anyone has any experience with either have 2 paint event handlers or with having to update the circle depending on location, it would be great! Any sample code and guidance would be greatly appreciated! See below for event handlers. I am developing use C# and Visual Studio 2005.
//In constructor, after intialize component call first Paint Event Handler f1_paint. private static void f1_paint(object sender, PaintEventArgs e) { Graphics dc = e.Graphics; Pen p = new Pen(Color.White, 3); dc.DrawRectangle(p, 478, 10, 487, 352); p.Dispose(); } private static void f2_paint(object sender, PaintEventArgs e) { Graphics dc = e.Graphics; Pen o = new Pen(Color.Orange, 5); dc.DrawEllipse(o, 717, 176, 5, 5); o.Dispose(); } //In timer function will take in mouse input and update circle so need to call f2_paint. this.Paint += new PaintEventHandler(f2_paint);
Thanks, Laura -
Implement C++ in C# projectsHi all, I want to see if there is anyway to implement a C++ project into a C# project. I have a .cpp class that I want to use. Does anyone have any experience and can give me some sort of direction on how to do this. I am using Visual Studio 2005 and implementing in C#. Thanks! Laura
-
How to work with Double Buffering C#Hi All: I am trying to work with the built in double buffering in C# Visual Studio 2005. I want to have no flicker. I have a timer event that throws an event every 100 ms when the timer event occurs it draws a square at the given location. This occurs very quickly. Behind the square is a four quadrant background. I am trying to do the double buffering so that the background is not redrawn everytime or so that it is smoother. When I set the doublebuffer function to true it flickers faster so I am obviously not doing something right. Please help! Please view my Paint function:
public void XYZ_PaintData(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Pen penRed = new Pen(Color.Red, 2); Pen penGreen = new Pen(Color.Green, 2); Pen penBlue = new Pen(Color.Blue, 2); Pen penWhite = new Pen(Color.White, 1); Pen penBack = new Pen(Color.Gray, 1); //Graphics g = this.CreateGraphics(); //g.Clear(this.BackColor); this.DoubleBuffered = true; /*Pen penRed = new Pen(Color.Red, 2); Pen penGreen = new Pen(Color.Green, 2); Pen penBlue = new Pen(Color.Blue, 2); Pen penWhite = new Pen(Color.White, 1); Pen penBack = new Pen(Color.Gray, 1);*/ penBack.DashStyle = DashStyle.Solid; // Background g.DrawLine(penBack, 375, 105, 375, 600); g.DrawLine(penBack, 50, 325, 650, 325); g.DrawRectangle(penBack, x0XYZ, y0XYZ, x1XYZ, y1XYZ); AccelReadings = true; //Line that is drawn when timer event occurs g.DrawRectangle(Pens.Black, xMXYZ, yMXYZ, 2, 2); }
Laura -
How to draw a ontop of a background in C#Hi All: I am trying to draw small points ontop of an origin background. I am trying to plot points in a 4 coordinate background. So I want the background to be stationary and the points to change given the incoming values. How can I do this? Right now my background is redrawing everytime I draw the small square so it looks awful! :( Please help! Laura
-
Trying to draw using System.Drawing.Drawing2DCould you give me a coded example of drawing a 1cm square or a point whichever is easier on a form at location (15,25)? Thanks so much!:-D
-
Trying to draw using System.Drawing.Drawing2DThanks so much! That worked. Now how can I dry a small square and display it in the form at certain locations?
-
Trying to draw using System.Drawing.Drawing2DHi all I am new to C# and real new to drawing with it. I have been looking online and have been unsuccessful in finding tutorials or examples in what I am trying to do. I am trying to draw a very small square about 1cm in size. I want to draw it at a certain location on the form. Does anyone know how to do this or can help point me in the right direction with sample code. I have a timer implemented and when the timer interrupt occurs I want to draw the small square in a differnt location in the form. Right now its not drawing anything. I call my paint function in the timer event function by doing the following: this.Paint += new PaintEventHandler(XYZ_Paint); But nothing gets displayed to the form, not even a simple line or anything. Why is this? Please help. I am using C# and Visual Studio 2005. Thanks, Laura:sigh:
-
Help with my class using Serial Port Class1. The following is the error that I am getting: Error 4 'Triax_ReVamp.SCI_Communication.SciGetCal(int, int, int, int, int, int)': not all code paths return a value I want it to be a boolean function. 2. I know that the serial communication is working, I have it working with a C++ program that we wrote previously which is very similair to this one and it works perfect! 3. I do have it set up properly: baud rate, stop parity, etc. I actually have this same code in my RawData.cs and it works. The reason I am trying to write a SCICommunication class is because I have multiple forms that will be calling the calibration function so I thought it would be better to have an outside serial class that has the function that I can call from all my forms. 4. I am using Visual Studio 2005 C# I have attatched my code. I am trying to make it a boolean function, so that if it is successful in getting the calibration data it returns true, if it is not then it returns false. Thanks so much for your help! I appreciate it!:-D
public static bool SciGetCal(int X0g, int X1g, int Y0g, int Y1g, int Z0g, int Z1g) { int readCal; int[] readXYZCalBytes = new int [9]; COM_Port.SCIPort.Write("K"); int readCalBytes = COM_Port.SCIPort.BytesToRead; //return false; if (readCalBytes == 9) { for (int j = 0; j < readCalBytes; j++) { readCal = COM_Port.SCIPort.ReadByte(); readXYZCalBytes[j] = readCal; } if (readXYZCalBytes[0] == 88 && readXYZCalBytes[3] == 89 && readXYZCalBytes[6] == 90) { //0g Calibrated X Value X0g = readXYZCalBytes[1]; //1g Calibrated X Value X1g = readXYZCalBytes[2]; //0g Calibrated X Value Y0g = readXYZCalBytes[4]; //1g Calibrated X Value Y1g = readXYZCalBytes[5]; //0g Calibrated X Value Z0g = readXYZCalBytes[7]; //1g Calibrated X Value Z1g = readXYZCalBytes[8]; return true; } else { X0g = Y0g = Z0g = 128; X1g = Y1g = Z1g = 189; return false;
-
Help with my class using Serial Port ClassMy goal in this project is to read in accelerometer data from a semiconductor board. The board responds to a protocol. If I write to the board a "K" it responds with 9 bytes of calibration data. The calibration data is as follows x, X0g, X1g, y, Y0g, Y1g, z, Z0g, Z1g. I am trying to create a class that has a built in function that writes a K to get the 9 calibration bytes. So that is why I wrote the class SciCommunications. I want to then call the function from a different class in order to get the calibration values in the RawData Class and other form classes. I am not getting an error but I am also not sure how to get the 9 calibration bytes in my RawData.cs. In my Raw Data I call it by doing ? = SCI_Communication.SciGetCal(X0g, X1g, Y0g, Y1g, Z0g, Z1g); Is this how I can get the 9 calibration bytes? What should I set the question mark to be, int or array? Hope this makes more sense!;P Laura
-
Help with my class using Serial Port ClassHi: I am somewhat new to C# and I am trying to create a class that my forms use. The SCICommunication class contains a method called SCIgetCal. See below for code. My goal is to get the calibration data (X0g, X1g, Y0g, Y1g, Z0g, Z1g) from this function and work with it in my form class RawData.cs. I call it in my RawData.cs by the following: SCI_Communication.SciGetCal(X0g, X1g, Y0g, Y1g, Z0g, Z1g); How can I do this? What am I doing wrong? Do I need to add any headers or anything to RawData.cs to use this function? Please help! :sigh:
// SciGetCal function in SCI_Communication Class using System; using System.Collections.Generic; using System.Text; using System.IO.Ports; using System.IO; using System.Threading; namespace Triax_ReVamp { public partial class SCI_Communication { public static int SciGetCal(int X0g, int X1g, int Y0g, int Y1g, int Z0g, int Z1g) { int readCal; int[] readXYZCalBytes = new int [9]; COM_Port.SCIPort.Write("K"); int readCalBytes = COM_Port.SCIPort.BytesToRead; //return false; if (readCalBytes == 9) { for (int j = 0; j < readCalBytes; j++) { readCal = COM_Port.SCIPort.ReadByte(); readXYZCalBytes[j] = readCal; return 1; } if (readXYZCalBytes[0] == 88 && readXYZCalBytes[3] == 89 && readXYZCalBytes[6] == 90) { //0g Calibrated X Value X0g = readXYZCalBytes[1]; //1g Calibrated X Value X1g = readXYZCalBytes[2]; //0g Calibrated X Value Y0g = readXYZCalBytes[4]; //1g Calibrated X Value Y1g = readXYZCalBytes[5]; //0g Calibrated X Value Z0g = readXYZCalBytes[7]; //1g Calibrated X Value Z1g = readXYZCalBytes[8]; return 1; } else { X0g = Y0g = Z0g = 128; X1g = Y1g = Z1g = 189; return 0; } } } } } }
Laura