Thread: Using RS232 25pin i need a program to detect magnitude and frequency of lightning

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    6

    Question Using RS232 25pin i need a program to detect magnitude and frequency of lightning

    This is a PROGRAM TO DETECT THE LOCATION AND THE FREQUENCY OF LIGHTNING in a given area. But what i want to find out is a PROGRAM TO DETECT THE MAGNITUDE AND THE FREQUENCY OF LIGHTNING. All the measurement are done
    using components and ICs' (hardware) and is then interfaced to the computer using an interface card with RS232 25 pin. This card is used to connect the computer with the hardware and is slot into the IBM in the main circuit board of the computer. This card is then connected with the RS232 25 pin. But the program that i have is to detect the loca tion of the lightning but what i re quire is a program to detect its magnitude nd frequency. I'll be very grateful if someone here could help me with his problem i'm facing. Thankin you in advance.

    Below is the program ti detect the location and frequency of lightning:



    PROGRAM TO DETECT LOCATION AND FREQUENCY OF LIGHTNING

    Code:
    //------define file usage------//
    
    #include<dos.h>
    #include<math.h>
    #include<time.h>
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<graphic.h>
    
    
    //------define constants and input ports------//
    
    #define porta 0x300		  //add porta 8255	
    #define portb 0x301		  //add portb 8255
    #define portc 0x302		  //add portc 8255
    #define control 0x303		  //add control port 8255
    #define maxdistance 5100.0
    #define vlight 330.0
    #define X 320
    #define Y 240
    
    int main(void)
    {
    clrscr();
    unsigned int dataA, dataB, dataC;
    float D1, D2, J2, D3, J3, yp, yo, zo, xp, ya;
    float rad1, rad, costeta, cospsi, sinpsi, dig1, angleteta, angle;
    double t1, t2, t3, tm, xo, h, deltax, slope, a, b, c;
    int B, C, y1, digit, xaxis, Yaxis, Zaxis, xpaxis, ypaxis;
    char buffer[20];
    
    //------define input and control port------//
    
    outportb(control,0x9b);		//control port
    dataA=inportb(0x300);		//input port bit a
    dataB=inportb(0x301);		//input port bit b
    dataC=inportb(0x302);		//input port bit c
    
    //------calculations for psi angle------//
    
    for(dig1=0 ; dig1<=90 ; dig1++)
    {
    rad1=dig1*3.1412/180.0;
    
    //------calculations for distance D1,D2,D3 from input data------//
    
    tm=maxdistance/vlight;		//max time,tm
    t1=(dataA/99)*tm;		//time for x count,tn
    t2=(dataB/99)*tm;
    t3=(dataC/99)*tm;
    
    D1=t1*vlight;			//distance of x count,Dn
    D2=t2*vlight;
    D3=t3*vlight;	
    
    //------display of value of distance and input data------//
    
    clrscr();
    textcolor(BROWN);
    textbackground(CYAN);
    
    printf("\n\t******************************************");
    printf("\n\t\tDISTANCE AND LOCATION OF LIGHTNING");
    printf("\n\t******************************************");
    printf("\n\n\tData from portA[heksadecimal] = %x",dataA);
    printf("\n\tData from portB[heksadecimal] = %x",dataB);
    printf("\n\tData from portC[heksadecimal] = %x",dataC);
    printf("\n\n\tDistance of lightning from detector A = %.1f",D1);
    printf("\n\tDistance of lightning from detector B = %.1f",D2);
    printf("\n\tDistance of lightning from detector C = %.1f",D3);
    
    //------calculation of angle towards 2nd distance, J2------//
    
    xp=1000;					//distance between detector
    costeta=((D1*D1)+(xp*xp)-(D2*D2))/(2*xp*D1);	//[3.3]
    rad=acos(costeta);				//[3.4]
    angleteta=rad*180.0/3.1412;		//angle in degree
    
    h=D1*sin(rad);				//[3.5]
    xo=D1*cos(rad);				//[3.6]
    J2=sqrt(h*h+(xp-xo)*(xp-xo));		//[3.7]
    
    //------calculation of 3rd distance comparison, J3------//
    
    cospsi=cos(rad1);
    sinpsi=sin(rad1);
    yo=h*cospsi;				//[3.9]
    zo=h*sinpsi;				//[3.10]
    yp=sqrt(xp*xp-xp/2*xp/2);			//[3.8]
    J3=sqrt(h*h+yp*yp-h*yp*cospsi);		//[3.13]
    
    
    //------display of xo,yo,zo, distance of B and C from calculations and teta and psi angle------//
    
    
    printf("\n\n\tDistance of detector B form calculations = %.2f",J2);
    printf("\n\tDistance of detector C form calculations = %.2f",J3);
    printf("\n\n\tcoordinate of xo = %.2f",xo);
    printf("\n\tcoordinate of yo = %.2f",yo);
    printf("\n\tcoordinate of zo = %.2f\n",zo);
    printf("\n\tangle from detector A = %.2f",angleteta);
    printf("\n\tangle at xplane = %.2f\n",dig1);
    
    }
    
    stop;
    getch();
    
    //------request auto detection------//
    
    int gdriver=DETECT,EGA,gmode=EGAHI,errorcode;
    
    //------start of graphic------//
    
    initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
    
    //------xaxis------//
    
    line(X,Y,X+250,Y);
    moveto(X+260,Y);
    outtext("X");
    
    //------zaxis------//
    
    line(X,Y,X,Y-250);
    moveto(X-20,Y-240);
    outtext("Z");
    
    //------yaxis and its slope------//
    
    a=(Y-350);
    c=(X-100);
    slope=a/c;
    line(X,Y,100,350);
    moveto(100-10,350-10);
    outtext("Y");
    
    //------scale graph of 3 dimension------//
    
    setcolor(GREEN0;
    rectangle(40,130,200,200);
    moveto(50,140);
    outtext("scale");
    moveto(50,160);
    outtext(xaxis=5:102");
    moveto(50,170);
    outtext(yaxis=41:850");
    moveto(50,180);
    outtext(xaxis=5:102");
    Xaxis=(xo/5100*250);
    Yaxis=(xo/5100*250);
    Zaxis=(xo/5100*246);
    xpaxis=(xp/5100*250);
    ypaxis=(yp/5100*246);
    
    //------actual coordinate------//
    
    moveto(44,240);
    sprintf(buffer,"(%d,%d,%d)"Xaxis,Yaxis,Zaxis);
    outtext(buffer);
    moveto(44,220);
    outtext("coordinate");
    
    setcolor(RED);
    
    //------xo location------//
    
    line(X,Y,X+Xaxis,Y);
    
    //------zo location------//
    
    line(X,Y,X,Y-Zaxis);
    
    //------yo location------//
    //slope=(Y-y1)/(X-(X+Yaxis))
    
    y1=Y-slope*Yaxis;
    line(X,Y,X-Yaxis,y1);
    
    //------plot location------//
    
    setcolor(YELLOW);
    B=(X+Xaxis)-((Y-y1)/slope);
    
    //------distance detector A------//
    
    line(X,Y,B,Y-Zaxis);
    
    //------distance detector B------//
    
    line(X+xpaxis,Y,B,Y-Zaxis);
    
    //------distance detector C------//
    
    C=Y-slope*ypaxis;
    line(B,Y-Zaxis,X+xpaxis/2-ypaxis,C);
    
    //------graph title------//
    
    setcolor(GREEN);
    moveto(120,420);
    outtext("Display 3D to detect location of lightning");
    
    //------clenup------//
    
    getch();
    closegraph();
    cleardevice();
    return();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I'll be very grateful if someone here could help me with his problem i'm facing
    Apart from badly formatted code which doesn't even compile, what is your actual question?

    I suggest you throw away all the graphics stuff until you're confident of receiving data from your hardware.
    In the meantime, the easy thing to do is just print the data out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    6

    Unhappy Help needed for C++ programming to detect magnitude of lightning

    i need a C++ program to detect the magnitude of the lightning and how often it stikes on a surge arrestor. I'm using hardware to detect them and a Analog to Digital Converter to convert the analog signals to digital form to be read by the INTERFACE Card, RS 232 25 pin. The hardware is suppose to measure the magnitude up to 5 volts only and the estimated minimum and maximum value of the actual magnitude is between 1K Volt to 100K Volt. When a lightning strikes the hardware is suppose to send the analog signal to the converter and then send to the Cpu. Here the program is suppose to detect what is the actual magnitude of the lightning and how often does it strike say maybe in 1 month duration. That means i need a program to get the actual scaling of the magnitude of lightning and the result has to be plotted in a table form and preferably converted into graph. This all should be shown in the Monitor. This is my final year project and i need some help in this.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    How on earth, though, is anyone supposed to answer these questions when nobody knows anything about the specifics of the project?

    I mean, saying something uses and ADC and RS232 is practically meaningless. If you were asking "how do I send this string over RS232", or "how do I receive data on RS232" you might have a chance of getting an answer, but apart from whomever built your hardware, who is going to know anything about it? How should I, or anyone else know, what commands to send to get your data back?

    Also, are you saying your ADC is supposed to be measuring a signal that has a 100 KV dynamic range????? There's not an ADC in existence that can measure that high of voltage directly.

    Is this program written for your particular hardware? It doesn't sound like it. When you get to custom hardware, you can't reuse someone else's program, you know, you need to start from scratch unless they happened for some reason to use the identical hardware as you.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The main problem is the organization of the program. If the code isn't well-organized, you won't be able to produce good results. Separate the user-interface, serial IO, and calculations. Use files to store data. Use main() to propagate errors, and to otherwise drive the program. Place your calculations in functions and test them using different sets of test data.

    Imagine if this were a large-scale project. Would you have approached it like this? A good program adapts easily to changing requirements, different volumes of data, and variations of granularity.

    Anyone can write code. Good software requires careful planning.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well since you totally failed to listen last time, and totally reposted your assignment with no further effort, there's not much more to do.

    Code:
    // this is what the detector provides
    struct {
        unsigned int A, B, C;
    } strike;
    
    strike read_detector ( void ) {
        strike result;
        // wait for lightning event here?
        // or is some bit of A,B,C meant to indicate valid data?
        outportb(control,0x9b);		//control port
        result.A=inportb(0x300);	//input port bit a
        result.B=inportb(0x301);	//input port bit b
        result.C=inportb(0x302);	//input port bit c
        return result;
    }
    
    int main ( ) {
        strike info;
        while ( 1 ) {
          info = read_detector();
          cout << info.A << " " << info.B << " " << info.c << endl;
        }
        return 0;
    }
    OK, until this works, you're pretty much wasting your time with all the pretty stuff.

    Using the strike struct as a basic interface, you can SEPARATELY work on the following with constant (but accurate) data samples.

    Code:
    struct {
        unsigned int A, B, C;
    } strike;
    
    void plot ( strike info ) {
        // now do all your graphic stuff
    }
    void file ( strike info ) {
        // read/write a file
    }
    void analyse ( strike info ) {
        // do some data analysis
    }
    
    int main ( ) {
        strike info[10] = {
            { 1, 2, 3 },  // pick 10 sets of numbers representing actual data which you might expect
        };
        for ( int i = 0 ; i < 10 ; i++ ) {
            plot( info[i] );
        }
        return 0;
    }
    THEN AND ONLY THEN do you join the two parts together into a single program
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed