Click here to Skip to main content
15,881,559 members
Articles / Programming Languages / C#
Article

Graphical Analysis of Process Control Program

Rate me:
Please Sign up or sign in to vote.
3.50/5 (3 votes)
11 Feb 2007CPOL2 min read 27.2K   371   10   2
This mathematical program uses explicit conversion and graphical display
Sample image

Introduction

Mathematical programs can be made more interesting when they use graphics to display their results. I use the graphics in C# to draw a representative diagram of the calculation. This calculation is used for Process Control. An instrumentation transmitter is calibrated to output 4 - 20 Milliamps when measuring a typical column of water. Let's say this column of water is a 0" - 120" span of H2O. The calculation will tell us what delta Milliamps should be output for the given percentage process span, or what delta process span is needed to give us the output delta Milliamps. This will allow us to calibrate our instrument. We generally check calibration at 0 %, 25 %, 50 %, 75 %, and 100 % levels of the process span.

Explicit Conversion

A numerical value must be a string value to be displayed. When that value is used in a calculation, it must be Converted Explicitly. I use a double variable type as it allows decimal places that will be produced by division.

C#
delta1 = Double.Parse(txtDelta1.Text);
delta3 = span2 * ( delta1/ span1);

Data Validation

Interestingly enough, those same lines of code will take care of most of our Data Validation for us. If Double.Parse throws an exception, the code in our catch block will run.

C#
catch(Exception w)
{
    MessageBox.Show(w.ToString(), "Important", MessageBoxButtons.OK,
        MessageBoxIcon.Stop);
}

The next part of our Data Validation is to ensure that our delta falls within the limits of our span. Otherwise, division by zero, or even memory overflow could occur. Actually, at one time, people considered both of these errors to be one and the same thing.

C#
if ( double.Parse( txtDelta1.Text ) >= 0 )
{
    if ( double.Parse( txtDelta1.Text ) <= span1 )
    {
        blue = false;
        delta1 = Double.Parse(txtDelta1.Text);
        delta3 = span2 * ( delta1/ span1);
        span4 = Double.Parse(txtSpan2a.Text);
        txtDelta2.Text = delta3.ToString();
        Answere2 = delta3 + span4;
        lblAnswere2.Text = txtDelta2.Text.ToString()  + 
            " + " +   txtSpan2a.Text.ToString() + 
            " = " + Answere2.ToString();
        lblAnswere2Unit.Visible = true;
    }
    else
    {
        MessageBox.Show("Delta must be within Span limits",
            "Important", MessageBoxButtons.OK, MessageBoxIcon.Stop);
    }
}
else
{
    MessageBox.Show("Delta must be within Span limits",
        "Important", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}

Graphics

Graphics can be manipulated by calculation as well. Here, we convert explicitly to Int32 type variable. We do this because this conversion process will round a double type variable to an integer value.

C#
// Graphics
Graphics graph = this.CreateGraphics();
Pen pen = new Pen(Color.Red);
                    
//Red Line
if( blue == true)
{
    //Red Line
    int line2 = Convert.ToInt32(delta2/ span2 * 200) + 350;
    graph.DrawLine( pen, line2, 435, line2, 615);
}
else
{
    int line = Convert.ToInt32(delta1/ span1 * 200) + 350;
    graph.DrawLine( pen, line, 435, line, 615);
}

Conclusion

There is a lot left to do with this program, but I hope this helps you through "conversion hell". Feel free to develop software as you desire. This code is your code.

History

  • 11th February, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
I started programming in High School writing Fortran IV on punch cards using a keypunch machine. When I moved to Las Vegas I studied IBM AS-400, and RPG. This was when Windows was really taking over, so I switched to VB-6, then VB.Net, and C#.Net.

I have really enjoyed the .Net Platform, and it still amazes me every time I am inspired to the point of writing a new program.

Thanks All ! for giving me the time and patience.

Comments and Discussions

 
Generalerror input string not in correct format Pin
winheart22-Apr-07 21:19
winheart22-Apr-07 21:19 
if i enter a values like this 0.00...., its giveing me error, how to avoid this error. please help me D'Oh! | :doh:
Question???? Pin
V. Thieme11-Feb-07 21:59
V. Thieme11-Feb-07 21:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.