请问如何用c写串口通信的代码?

hillhero789 2004-04-04 02:09:57
我试过用bios 14h中断来做,但是总是不成功,说是溢出错误
我想是不是需要设置缓冲区呢?但是bios中断里边又没有要求

请大侠帮助一下小弟,感激"万分"(暂时只有100分相赠了.^_^)
...全文
156 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
rorot 2004-04-11
  • 打赏
  • 举报
回复
UP成星的代码是在tc环境下运行的。
w3guy 2004-04-11
  • 打赏
  • 举报
回复
用bios中断是通用性最差的一种。
ejiue 2004-04-11
  • 打赏
  • 举报
回复
使用dos.h,不能在非windows平台使用喽?
ejiue 2004-04-11
  • 打赏
  • 举报
回复
To bm1408(当UP成星时,我就......继续UP):

你的方法调用C库的函数,能不能在非Windows平台使用?
是如何实现的呢?
hillhero789 2004-04-04
  • 打赏
  • 举报
回复
感谢个位,但是能不能用bios中断来写呢?
bios来写的话可能通用性要好一点吧?
还有就是"bm1408(当UP成星时,我就......继续UP)"的程序能在什么环境下运行,不能在那些环境下运行呢?
w3guy 2004-04-04
  • 打赏
  • 举报
回复
window 下用:

打开端口

HANDLE hCom;
hCom = CreateFile( "\\\\.\\COM1", //COM1
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened w/exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);



if(hCom == INVALID_HANDLE_VALUE)
{
//error
}


写端口:

DWORD dwWritten;
char buf[1024]="date you want to send";
if(!WriteFile(hCom,buf,strlen(buf),&dwWritten,NULL)) {
// error
}
FlushFileBuffers(hCom);
printf("%d bytes written\n", dwWritten);



读端口:
DWORD dwRead;
buf[100];
if(!ReadFile(hCom,buf,100,&dwRead,NULL) ) {
//error
}



设置读写超时时间:

BOOL SetComTimeout(HANDLE hCom,DWORD millisec)
{

COMMTIMEOUTS timeout;
timeout.ReadIntervalTimeout = 2000;
timeout.ReadTotalTimeoutConstant = millisec;
timeout.ReadTotalTimeoutMultiplier =0;
timeout.WriteTotalTimeoutConstant = millisec;
timeout.WriteTotalTimeoutMultiplier = 0;
return SetCommTimeouts(hCom,&timeout);
}


w3guy 2004-04-04
  • 打赏
  • 举报
回复
bm1408 的程序使用截获中断实现,可能可以在DOS或98下跑,在NT,W2K,XP下就不行了。
bm1408 2004-04-04
  • 打赏
  • 举报
回复
这个程序在我机子里好长时间了,从来没有看过!
你看看吧!随便让他给我弄点分!
#include <dos.h>
#include <stdio.h>
#include <conio.h>

#define PORT1 0x2E8 /* Port Address Goes Here */
/* Defines Serial Ports Base Address */
/* COM1 0x3F8 */
/* COM2 0x2F8 */
/* COM3 0x3E8 */
/* COM4 0x2E8 */

#define INTVECT 0x0B /* Com Port's IRQ here */
/* (Must also change PIC setting) */

int bufferin = 0;
int bufferout = 0;
char ch;
char buffer[1025];
void interrupt (*oldport1isr)();

void interrupt PORT1INT() /* Interrupt Service Routine (ISR) for PORT1 */
{
int c;
do {
c = inportb(PORT1 + 5);
if (c & 1) {
buffer[bufferin] = inportb(PORT1);
bufferin++;
if (bufferin == 1024)
bufferin = 0;
}
}while (c & 1);

outportb(0x20,0x20);
}

void main(void)
{
int c;

outportb(PORT1 + 1 , 0); /* Turn off interrupts - Port1 */
oldport1isr = getvect(INTVECT); /* Save old Interrupt Vector for */
/* later recovery */
setvect(INTVECT, PORT1INT); /* Set Interrupt Vector Entry */
/* COM1 - 0x0C */
/* COM2 - 0x0B */
/* COM3 - 0x0C */
/* COM4 - 0x0B */
/* 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 = 56,700 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 */Interfacing the Serial / RS232 Port V5.0 http://www.senet.com.au/~cpeacock
outportb(PORT1 + 4 , 0x0B); /* Turn on DTR, RTS, and OUT2 */
outportb(0x21,(inportb(0x21) & 0xF7)); /* Set Programmable Interrupt */
/* Controller */
/* COM1 (IRQ4) - 0xEF */
/* COM2 (IRQ3) - 0xF7 */
/* COM3 (IRQ4) - 0xEF */
/* COM4 (IRQ3) - 0xF7 */
outportb(PORT1 + 1 , 0x01); /* Interrupt when data received */
printf("\nSample Comm's Program. Press ESC to quit \n");
do {
if (bufferin != bufferout){
ch = buffer[bufferout];
bufferout++;
if (bufferout == 1024) bufferout = 0;
printf("%c",ch);
}
if (kbhit()){
c = getch();
outportb(PORT1, c);
}
} while(c !=27);

outportb(PORT1 + 1 , 0); /* Turn off interrupts - Port1 */
outportb(0x21,(inportb(0x21) | 0x08)); /* MASK IRQ using PIC */
/* COM1 (IRQ4) - 0x10 */
/* COM2 (IRQ3) - 0x08 */
/* COM3 (IRQ4) - 0x10 */
/* COM4 (IRQ3) - 0x08 */
setvect(INTVECT, oldport1isr); /* Restore old interrupt vector */
}

69,369

社区成员

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

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