亲爱的我对C语言中的串口编程有疑问,请尽快帮助我 [英] Dear all I have a doubt in serial port programming in c ,please help me as fast as possible

查看:74
本文介绍了亲爱的我对C语言中的串口编程有疑问,请尽快帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ANSI风格的c代码来为嵌入式单元的GUI创建串行数据前端.
该代码是根据标准ASCI C代码开发的,用于串行端口编程.
/*********** CODE **********************/
#include< dos.h>
#include< stdio.h>
#include< conio.h>

#define PORT1 0x3F8

/*定义串行端口基地址*/
/* COM1 0x3F8 */
/* COM2 0x2F8 */
/* COM3 0x3E8 */
/* COM4 0x2E8 */

void main(void)
{
int c;
int ch;
outportb(PORT1 + 1,0); /*关闭中断-端口1 */

/*端口1-通讯设置*/

outportb(PORT1 + 3,0x80); /*将DLAB设置为*/
outportb(PORT1 + 0,0x03); /*设置波特率-除数锁存器低字节*/
/*默认0x03 = 38,400 BPS */
/* 0x01 = 115,200 BPS */
/* 0x02 = 57,600 BPS */
/* 0x06 = 19,200 BPS */
/* 0x0C = 9,600 BPS */
/* 0x18 = 4,800 BPS */
/* 0x30 = 2,400 BPS */
outportb(PORT1 +1,0x00); /*设置波特率-除数锁存器高字节*/
outportb(PORT1 + 3,0x03); /* 8位,无奇偶校验,1个停止位*/
outportb(PORT1 + 2,0xC7); /* FIFO控制寄存器*/
outportb(PORT1 + 4,0x0B); /*打开DTR,RTS和OUT2 */

printf("\ nSample Comm's Program.按ESC退出\ n");

做{c = inportb(PORT1 + 5); /*检查char是否为*/
/* 已收到. */
if(c& 1){ch = inportb(PORT1); /*如果是,则获取Char */
printf(%c",ch);}/*将字符打印到屏幕上*/

如果(kbhit()){ch = getch(); /*如果按下键,则获取Char */
outportb(PORT1,ch);}/*将字符发送到串行端口*/

} while(ch!= 27); /*按下ESC(ASC 27)时退出*/
}
在所有标准教科书中(例如:YING BAI的窗口串行端口编程),都明确提到该代码不会在Win NT/2000/XP OS平台上运行.但是我可以在Win XP OS中运行c代码,但它的问题是,它会暂停一段时间,直到检测到鼠标移动或键盘被击中).我关闭了屏幕保护程序和省电模式以进行显示.相同的代码与MSDOS完美配合/Win 95/98 OS平台(标准)没有任何问题.您能对此问题进行解释和补救吗(如果有)?问候Arun.A.D

I am using ANSI style c code for creating a serial data front end with GUI for my embedded unit.
The code is developed from the standard ASCI C code for serial port programming.
/***********CODE**********************/
#include <dos.h>
#include <stdio.h>
#include <conio.h>

#define PORT1 0x3F8

/* Defines Serial Ports Base Address */
/* COM1 0x3F8 */
/* COM2 0x2F8 */
/* COM3 0x3E8 */
/* COM4 0x2E8 */

void main(void)
{
int c;
int ch;
outportb(PORT1 + 1 , 0); /* Turn off interrupts - Port1 */

/* PORT 1 - Communication Settings */

outportb(PORT1 + 3 , 0x80); /* SET DLAB ON */
outportb(PORT1 + 0 , 0x03); /* Set Baud rate - Divisor Latch Low Byte */
/* Default 0x03 = 38,400 BPS */
/* 0x01 = 115,200 BPS */
/* 0x02 = 57,600 BPS */
/* 0x06 = 19,200 BPS */
/* 0x0C = 9,600 BPS */
/* 0x18 = 4,800 BPS */
/* 0x30 = 2,400 BPS */
outportb(PORT1 + 1 , 0x00); /* Set Baud rate - Divisor Latch High Byte */
outportb(PORT1 + 3 , 0x03); /* 8 Bits, No Parity, 1 Stop Bit */
outportb(PORT1 + 2 , 0xC7); /* FIFO Control Register */
outportb(PORT1 + 4 , 0x0B); /* Turn on DTR, RTS, and OUT2 */

printf("\nSample Comm''s Program. Press ESC to quit \n");

do { c = inportb(PORT1 + 5); /* Check to see if char has been */
/* received. */
if (c & 1) {ch = inportb(PORT1); /* If so, then get Char */
printf("%c",ch);} /* Print Char to Screen */

if (kbhit()){ch = getch(); /* If key pressed, get Char */
outportb(PORT1, ch);} /* Send Char to Serial Port */

} while (ch !=27); /* Quit when ESC (ASC 27) is pressed */
}
In all standard text books(Eg:The window serial port programming By YING BAI) it is clearly mentioned that the code will not run in Win NT/2000/XP OS platforms.But I could run the c code in the Win XP OS, and it is working!!!But the problem is that after some time it pauses, until a mouse movement or key board hit is detected).I turned off the screen saver and power saving mode for display.The same code works perfectly with MSDOS /Win 95/98 OS platforms (that is standard)with out any problem.Can you give the explanation and remedy(if available)for this problem? With warm regards Arun.A.D

推荐答案



关闭屏幕保护程序是否可以解决?

据我所知,XP,2000、2003和……正在保护对其硬件的直接访问.我使用Microsoft库访问com端口.但是在C语言中,我不知道您是否可以使用它们.
Hi,

Does It solve when you turn off screen saver?

As I know, XP, 2000, 2003 and ... are protecting direct access to their hardware. I use Microsoft libraries to access to the com ports. but in C I don''t know if you can use them.


这篇关于亲爱的我对C语言中的串口编程有疑问,请尽快帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆