【发布时间】:2012-05-04 13:12:28
【问题描述】:
第一次发帖,所以可能会比必要的信息更多,但我想彻底:
我们在 C 语言中的一个练习是创建发送器和接收器程序,这些程序将通过 RS232 串行通信与空调制解调器交换数据。我们使用了一个虚拟端口程序(如果你想测试,我使用了 eltima 软件的 Virtual Serial Port 试用版)。我们需要做 4 个版本:
1) 使用由以前的学生创建的预定库,该库具有发送者和接收者等预制功能 2) 使用 inportb 和 outportb 函数 3) 使用 OS 中断 int86 并通过 REGS 联合给出寄存器值 4) 使用内联汇编
编译器:DevCPP(流血)。
一切正常,但现在我们需要根据发送和接收字符所花费的 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 时间' - 从什么时间到哪里?您可以说,在 I/O 映射中往返于 UART 的“输入”和“输出”指令就足够了。
-
我在 Windows 7 上,正如我所说,我正在运行一个模拟器。它创建端口对,我们处理这些端口对,但它模拟 16550 或 8250 UART。我猜在使用 in 和 out 指令时,它取决于指令的解析速度?例如,内联汇编应该是最快的,因为您直接向寄存器赋值并且它们已准备好发送,而使用预制库必须通过函数然后找到寄存器等。
标签: c time serial-port cpu