求WINDOWS下C语言读写串口的程序

haoxihuan 2011-01-07 01:15:52
如题,要求:
1.不要TC下面的C程序,要求能在visual studio 6.0下运行的C语言程序。(TC下程序我从网上找到过)
2.不要linux下的C程序
...全文
1088 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
haoxihuan 2011-01-10
  • 打赏
  • 举报
回复
楼上的read_string和send_string都是我写的。目的是改变每循环一次 才接收一个字符的问题。
haoxihuan 2011-01-10
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <io.h>
#include <sys\stat.h>

#define LENGTH 1024

void InitCOM(); //initialize serial port
void OpenPort(); //open serial port
void ClosePort(); //close serial port,release resource

void interrupt far asyncint(); //new ISR
void interrupt(*asyncoldvect)();//interrupt vector: to reserve interrupt scene

unsigned char Buffer[LENGTH];

int buffin=0;
int buffout=0;

unsigned char rbuf[RWMAXBUFLEN] = {0};

void OpenPort() //open COM1
{
unsigned char ucTemp;
InitCOM(); //initialize serial port

asyncoldvect=getvect(0x0c); //COM1 produce hardware interrupt IQR4,corresponding interrupt vector 0CH
disable(); //close interrupt
inportb(0x3f8);
inportb(0x3fe);
inportb(0x3fb);
inportb(0x3fa);
outportb(0x3fc,0x08|0x0b);
outportb(0x3f9,0x01);
ucTemp=inportb(0x21)&0xef; //open IRQ1
outportb(0x21,ucTemp);
setvect(0x0c,asyncint);
enable(); //open interrupt
}

void interrupt far asyncint() //ISR,receiving data from COM1
{
//unsigned char ch;
Buffer[buffin++] = inportb(0x3f8); //put chars to buffer
if (buffin>= LENGTH)
buffin=0; //pointer reset
outportb(0x20,0x20); //send EOI to ICR
}

void ClosePort(void) //close COM1
{
disable();
outportb(0x3f9,0x00); //close interrupt
outportb(0x3fc,0x00);
outportb(0x21,inportb(0x21) & 0x10);
enable();
setvect(0x0c,asyncoldvect);
}

void InitCOM() //initialize serial port
{

outportb(0x3fb,0x80); //DLAB=1,which means to set baud rate
outportb(0x3f8,0x0C); //set baud rate as 9600bps
outportb(0x3f9,0x00);

outportb(0x3fb,0x03); //set 8 databits,1 stopbit and no parity
outportb(0x3fc,0x08|0x0b);
outportb(0x3f9,0x01);
}

unsigned char read_char(void) //read received data from buffer
{
unsigned unch;
if(buffout != buffin)
{
unch = Buffer[buffout];
buffout++;
if(buffout >= LENGTH)
buffout=0; //pointer reset
return(unch);
}
else
return(0xff);
}

unsigned char* read_string(void)
{
if(buffout<=buffin)
{
unsigned char * temp =(unsigned char *) malloc(buffin-buffout+1);
memcpy(temp, &Buffer[buffout],buffin-buffout+1);
buffout= buffin;
if(buffout>=LENGTH)
buffout=0; //pointer reset
return temp;
}
else if(buffout > buffin)
{
unsigned char *temp =(unsigned char *)malloc(LENGTH-buffout+buffin+2);
memcpy(temp,&Buffer[buffout],LENGTH-buffout+1);
strncat(temp,Buffer,buffin+1);
buffout = buffin;
if(buffout>=LENGTH)
buffout=0; //pointer reset
return temp;
}
else
return (0xff);
}

void send_char(unsigned char unch)
{
while ( ((inp( 0x3f8 + 5)) & 0x40 ) == 0); //get D6 and check it to avoid lost data to be sent
outportb(0x3f8 , unch);
}
/*
void send_string(int nStrlen,char *ChBuf)
{
int k=0;
do {
send_char(*(ChBuf + k));
k++;
} while ((k < nStrlen));
}
*/
void send_string(char * chBuf)
{
char *temp = chBuf;
do{
send_char(*temp++);
}while(*temp!='\0');
}

void main() //main func as follow
{
unsigned char unChar;
short bExit_Flag = 0;

int i =0;
OpenPort(); //open the serial port

fprintf(stdout, "\n\nReady to Receive and Send DATA\n"
"press [ESC] to quit...\n\n");
do {

if ( kbhit() ) //press any key and check it
{
unChar = getch();
switch(unChar)
{
case 0x1B: //the ASCII value of ESC is 27
bExit_Flag = 1; //Exit program
break;
default:
break;
}


if(!bExit_Flag)
send_char(unChar); //send the char

}

unChar = read_char(); //get the received char,可添加接收处理函数部分
if (unChar != 0xff)
{
fprintf(stdout,"%c",unChar);
}

} while ( !bExit_Flag );

ClosePort(); //close serial port
}

haoxihuan 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 subfate 的回复:]
看来只有windows API适合楼主的要求了,可以看看龚建伟写的书,里面介绍几种方法:
1、使用第3方串口类;
2、控件;
3、API
百度一下“龚建伟”。
[/Quote]

龚建伟的书我看过。那本书唯一的一个C语言串口读写程序是运行在TC下的。其他的都是C++程序。我需要的是运行在visual studio的C程序。
李迟 2011-01-07
  • 打赏
  • 举报
回复
看来只有windows API适合楼主的要求了,可以看看龚建伟写的书,里面介绍几种方法:
1、使用第3方串口类;
2、控件;
3、API
百度一下“龚建伟”。
haoxihuan 2011-01-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 linxren 的回复:]
不都一样?
[/Quote]
不要C++的, 不要用微软的控件。
haoxihuan 2011-01-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 linxren 的回复:]
不都一样?
[/Quote]
不一样,TC下有些串口操作函数VC6.0不支持,就算include头文件也不能编译通过。
linxren 2011-01-07
  • 打赏
  • 举报
回复
不都一样?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧