C RS232 通讯。如何比较CPU时间?

标签 c time serial-port cpu

第一次发帖,所以可能会有比必要的更多的信息,但我想彻底:

我们的 C 练习之一是创建发送器和接收器程序,通过 RS232 串行通信与零调制解调器交换数据。我们使用了虚拟端口程序(如果你想测试的话,我使用了eltima软件的虚拟串行端口的试用版)。我们被要求做 4 个版本:

1)使用由以前的学生创建的预定库,该库具有发送者和接收者等预制功能 2)使用inportb和outportb函数 3)使用操作系统中断int86并通过REGS union 给出寄存器值 4)使用内联汇编

编译器:DevCPP(Bloodshed)。

一切正常,但现在我们需要根据发送和接收字符所花费的 CPU 时间来比较所有不同的版本。它特别指出我们必须找到以下内容:

平均值、标准差、最小值、最大值和 99.5%

类里面没有解释任何内容,所以我在这里有点迷失......我猜这些是经过多次正态分布试验后的统计数字?但即便如此,我如何实际测量 CPU 周期呢?我会继续搜索,但同时我会在这里发布,因为截止日期是 3 天:D。

int86版本的代码示例:

#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;
}

最佳答案

这里需要一些猜测,因为这个问题有点不确定。

您列出了四种不同的发送/接收字符的方法。我怀疑您的讲师正在寻找的是从您调用给定方法(或输入内联汇编代码)到您从该方法返回(保留内联代码)的时间。您需要捕获通话前和通话后的时间,找出它们的区别。

CPU 时间就不那么模糊了。 Clock() 方法是最直接的方法,但这可能不是讲师想要的。

最后是统计数据,这很简单。进行一系列运行,并运行一些时间统计

关于C RS232 通讯。如何比较CPU时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10449533/

相关文章:

c++ - C++ 中的 ReadFile、COM 和 NULL 字符

c++ - 有没有办法改变变量在内存中的存储方式(位大小)?

c++ - basic_serial_port 和 async_read_until

c - 不是一代核心

c - 我不明白 fseek 参数的作用

javascript - 使用 moment().utcOffset() 更新时间偏移

Java比较一天中自定义开始结束时间的两次

c - 哈希函数确定

未知字段的代码复用

python - 日期范围 : is there a more pythonic way? 中的所有星期二和星期三