简体   繁体   中英

C RS232 comm. How to compare CPU time?

First time posting so there's probably gonna be more info than necessary but I wanna be thorough:

One of our exercises in C was to create sender and receiver programs that would exchange data via RS232 serial communication with null modem. We used a virtual port program (I used the trial version of Virtual Serial Port by eltima software if you want to test). We were required to do 4 versions:

1) Using a predetermined library created by a previous student that had sender and reveiver etc. premade functions 2) Using the inportb and outportb functions 3) Using OS interrupt int86 and giving register values through the REGS union 4) Using inline assembly

Compiler: DevCPP (Bloodshed).

All worked, but now we are required to compare all the different versions based on the CPU time that is spent to send and receive a character. It specifically says that we have to find the following:

average, standard deviation, min, max and 99,5 %

Nothing was explained in class so I'm a little lost here...I'm guessing those are statistical numbers after many trials of the normal distribution? But even then how do I actually measure CPU cycles on this? I'll keep searching but I'm posting here in the mean time 'cause the deadline is in 3 days :D.

Code sample of the int86 version:

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>

#define RS232_INIT_FUNCTION 0
#define RS232_SEND_FUNCTION 1
#define RS232_GET_FUNCTION 2
#define RS232_STATUS_FUNCTION 3
#define DATA_READY 0x01

#define PARAM 0xEF
#define COM1 0
#define COM2 1


void rs232init (int port, unsigned init_code)
{
     union REGS inregs;
     inregs.x.dx=port;
     inregs.h.ah=RS232_INIT_FUNCTION;
     inregs.h.al=init_code;
     int86(0x14,&inregs,&inregs);
}

unsigned char rs232transmit (int port, char ch)
{
     union REGS inregs;
     inregs.x.dx=port;
     inregs.h.ah=RS232_SEND_FUNCTION;
     inregs.h.al=ch;
     int86(0x14,&inregs,&inregs);
     return (inregs.h.ah);
}

unsigned char rs232status(int port){
     union REGS inregs;
     inregs.x.dx=port;
     inregs.h.ah=RS232_STATUS_FUNCTION;
     int86(0x14, &inregs, &inregs);
     return (inregs.h.ah);  //Because we want the second byte of ax
     }

unsigned char rs232receive(int port)
{
    int x,a;
    union REGS inregs;
    while(!(rs232status(port) & DATA_READY))
    {
        if(kbhit()){
            getch();
            exit(1); 
            }
        };
    inregs.x.dx=port;
    inregs.h.ah=RS232_GET_FUNCTION;
    int86(0x14,&inregs,&inregs);
    if(inregs.h.ah & 0x80)
    {
        printf("ERROR");
        return -1;
    }
    return (inregs.h.al);
}

int main(){
    unsigned char ch;
    int d,e,i;

    do{
        puts("What would you like to do?");
        puts("1.Send data");
        puts("2.Receive data");
        puts("0.Exit");
        scanf("%d",&i);
        getchar();

        if(i==1){
           rs232init(COM1, PARAM);

           puts("Which char would you like to send?");
           scanf("%c",&ch);
           getchar();
           while(!rs232status(COM1));
           d=rs232transmit(COM1,ch);
           if(d & 0x80) puts("ERROR");   //Checks the bit 7 of ah for error
        }
        else if(i==2){
           rs232init(COM1,PARAM);
           puts("Receiving character...");
           ch=rs232receive(COM1);
           printf("%c\n",ch);
        }
    }while(i != 0);

    system("pause");
    return 0;
}

There is some guesswork required here because the question is a little undefined.

You've listed four different methods for sending/receiving a character. What I suspect your lecturer is looking for is the time from when you call the method given (or enter your inline assembly code) to the time when you return from the method (leave inline code). You will need to grab a time just before the call and just after the call and find their difference.

Less ambiguous is cpu time. The clock() method is the most straightforward way to do this, however this may not be what the lecturer is looking for.

Finally are the statistics, which is straightforward. Do a bunch of runs, and run some statistics on the times

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM