阅读:1368回复:4
寻求dos下的串口通讯程序源码,应用层的。
多谢多谢
|
|
|
沙发#
发布于:2002-05-22 08:28
去这个网址看看.
http://roaringwind.best.163.com :P |
|
板凳#
发布于:2002-05-22 11:16
OPEN下COMX不就可以了么??好奇怪的问题啊~~好象很多书里面的附带这个代码的!
|
|
|
地板#
发布于:2002-05-22 20:38
/* CXXK-LOG SOFT
使用TC3.1编译 串口发送 LOG-U.C 2001.10.23 wyp */ #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <dos.h> #include <math.h> #include <graphics.h> #include <stdarg.h> #include <ctype.h> #include <string.h> #define ALT_X 301 #define max_buffer 1024 /* Circular buffer size */ #define COM1 0x3F8 /* I/O address of COM1 */ #define COM2 0x2F8 /* I/O address of COM2 */ #define EOI 0x20 /* End of interrupt */ /* Async registers: offset from base */ #define IER 1 /* Interrupt Enable Register */ #define IIR 2 /* Interrupt Identification Register */ #define LCR 3 /* Line Control Register */ #define MCR 4 /* Modem Control Register */ #define LSR 5 /* Line Status Register */ #define MSR 6 /* Modem Status Register */ int GetKey(void); void Quit(void); void process_gprmc(char *smc); FILE *fin,fin1; unsigned int rx_buff[max_buffer+1]; /*interrupt buffer */ unsigned int head=0; int port=0x3f8,vectport=0x0c; int Key=0; void (interrupt *old_serial)(void); void interrupt new_serial() { int status = 0; do{ status = inp(COM1+IIR); if(status == 4) rx_buff[head++] = inp(COM1); if (head == max_buffer) head = 0; }while(status == 4); disable(); outp(0x20,EOI); enable(); } void send_byte(unsigned char ch) { while((inp(COM1+5)&0x20)!=0x20); outp(COM1,ch); } void send_msg(unsigned char *msg) { int i; int len; len = strlen(msg); for(i=0;i<len;i++) send_byte(msg); } /*int get_byte() { unsigned char db; static int tail=0; if(tail==head) return -1; else{db=rx_buff[tail++]; if(tail==max_buffer)tail=0; return(db); } } */ void set_baud(int port,long int baud_rate) { union{ struct{ unsigned lobyte:8; unsigned hibyte:8; }byte; struct{ unsigned word; }whole; }bytes; outp(port+3,inp(port+3)|0x80);//锁定波特率发生器 bytes.whole.word=(unsigned)(1843200L/((long)baud_rate<<4)); outp(port+0,bytes.byte.lobyte);//波特率寄存器低字节 outp(port+1,bytes.byte.hibyte);//波特率寄存器高字节 outp(port+3,inp(port+3)&0x6b);//线路控制寄存器:8位数据,1位停止位,奇校验 } void baud(int port,long int baud_rate) { disable(); set_baud(port,baud_rate); outportb(port+3,0x03); outportb(port+4,0x0b); outportb(port+1,0x01); inportb(port); inportb(port+2); inportb(port+5); inportb(port+6); outportb(0x20,0x20); enable(); } int InitBaud(int port,long int baud_rate) { int tmp; disable(); tmp=inp(0x21); tmp|=0x10; outp(0x21,tmp); old_serial = getvect(0x0C); setvect(0x0C,new_serial); tmp&=0xEF; outportb(0x21,tmp); baud(port,baud_rate); return(1); } void Log_Data() { unsigned char message[15],buff[2]; int i,j,n,jj=0; message[0]=\'$\'; message[1]=\'$\'; message[2]=\'1\'; message[3]=\'1\'; message[4]=\'2\'; message[5]=\',\'; message[6]=\'2\'; message[7]=\'1\'; message[8]=\',\'; message[9]=\'5\'; message[10]=\'3\'; message[11]=\',\'; message[12]=0X00; message[13]=0X00; message[14]=0X00; do{ for(i=0;i<15;i++) { if(kbhit()) { Key=GetKey(); if(Key==ALT_X) { disable(); outp(COM1+IER,0x00); setvect(0x0C, old_serial); enable(); Quit(); } } message[10]=(unsigned char)message[10]+1; if(message[10]>\'9\')//秒 { message[10]=\'0\'; message[9]=(unsigned char)message[9]+1; if(message[9]>\'5\')//秒 { message[9]=\'0\'; message[7]=(unsigned char)message[7]+1; if(message[7]>\'9\')//分 { message[7]=\'0\'; message[6]=(unsigned char)message[6]+1; if(message[6]>\'5\')//分 { message[6]=\'0\'; message[4]=(unsigned char)message[4]+1; if(message[3] == \'2\') { if(message[4]>\'3\')//时 { message[4]=\'0\'; message[3]=(unsigned char)message[3]+1; if(message[3]>\'2\')//时 { message[3]=\'0\'; } } } else { if(message[4]>\'9\')//时 { message[4]=\'0\'; message[3]=(unsigned char)message[3]+1; if(message[3]>\'2\')//时 { message[3]=\'0\'; } } } } } } } for(j=2;j<12;j++) { if(message[j]!=\',\') jj=jj^message[j]; } message[12]=jj; for(n=0;n<13;n++) { send_byte(message[n]); printf(\"%02X\",message[n]); } printf(\"\\n\"); delay(900); } }while(1); /* outportb(COM1+IER,0x00); setvect(0x0C,old_serial); */ } main() { clrscr(); if(!InitBaud(COM1,4800)) goto lp; printf(\"\\n Init Passed !\\n\"); Log_Data(); lp:printf(\"\\n Init Not Passed!\\n\"); disable(); setvect( 0x0C, old_serial ); enable(); getch(); Quit(); } int GetKey(void) { int Key, Low,Hig; Key=bioskey(0); Low=Key&0x00ff; Hig=(Key&0xff00)>>8; return(Low==0 ? Hig+256 : Low); } void Quit(void) { //closegraph(); fcloseall(); exit(0); } |
|
地下室#
发布于:2002-05-22 20:39
/* CXXK-LOG SOFT
使用TC3.1编译 串口发送 LOG-U.C 2001.10.23 wyp */ #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <dos.h> #include <math.h> #include <graphics.h> #include <stdarg.h> #include <ctype.h> #include <string.h> #define ALT_X 301 #define max_buffer 1024 /* Circular buffer size */ #define COM1 0x3F8 /* I/O address of COM1 */ #define COM2 0x2F8 /* I/O address of COM2 */ #define EOI 0x20 /* End of interrupt */ /* Async registers: offset from base */ #define IER 1 /* Interrupt Enable Register */ #define IIR 2 /* Interrupt Identification Register */ #define LCR 3 /* Line Control Register */ #define MCR 4 /* Modem Control Register */ #define LSR 5 /* Line Status Register */ #define MSR 6 /* Modem Status Register */ int GetKey(void); void Quit(void); void process_gprmc(char *smc); FILE *fin,fin1; unsigned int rx_buff[max_buffer+1]; /*interrupt buffer */ unsigned int head=0; int port=0x3f8,vectport=0x0c; int Key=0; void (interrupt *old_serial)(void); void interrupt new_serial() { int status = 0; do{ status = inp(COM1+IIR); if(status == 4) rx_buff[head++] = inp(COM1); if (head == max_buffer) head = 0; }while(status == 4); disable(); outp(0x20,EOI); enable(); } void send_byte(unsigned char ch) { while((inp(COM1+5)&0x20)!=0x20); outp(COM1,ch); } void send_msg(unsigned char *msg) { int i; int len; len = strlen(msg); for(i=0;i<len;i++) send_byte(msg); } /*int get_byte() { unsigned char db; static int tail=0; if(tail==head) return -1; else{db=rx_buff[tail++]; if(tail==max_buffer)tail=0; return(db); } } */ void set_baud(int port,long int baud_rate) { union{ struct{ unsigned lobyte:8; unsigned hibyte:8; }byte; struct{ unsigned word; }whole; }bytes; outp(port+3,inp(port+3)|0x80);//锁定波特率发生器 bytes.whole.word=(unsigned)(1843200L/((long)baud_rate<<4)); outp(port+0,bytes.byte.lobyte);//波特率寄存器低字节 outp(port+1,bytes.byte.hibyte);//波特率寄存器高字节 outp(port+3,inp(port+3)&0x6b);//线路控制寄存器:8位数据,1位停止位,奇校验 } void baud(int port,long int baud_rate) { disable(); set_baud(port,baud_rate); outportb(port+3,0x03); outportb(port+4,0x0b); outportb(port+1,0x01); inportb(port); inportb(port+2); inportb(port+5); inportb(port+6); outportb(0x20,0x20); enable(); } int InitBaud(int port,long int baud_rate) { int tmp; disable(); tmp=inp(0x21); tmp|=0x10; outp(0x21,tmp); old_serial = getvect(0x0C); setvect(0x0C,new_serial); tmp&=0xEF; outportb(0x21,tmp); baud(port,baud_rate); return(1); } void Log_Data() { unsigned char message[15],buff[2]; int i,j,n,jj=0; message[0]=\'$\'; message[1]=\'$\'; message[2]=\'1\'; message[3]=\'1\'; message[4]=\'2\'; message[5]=\',\'; message[6]=\'2\'; message[7]=\'1\'; message[8]=\',\'; message[9]=\'5\'; message[10]=\'3\'; message[11]=\',\'; message[12]=0X00; message[13]=0X00; message[14]=0X00; do{ for(i=0;i<15;i++) { if(kbhit()) { Key=GetKey(); if(Key==ALT_X) { disable(); outp(COM1+IER,0x00); setvect(0x0C, old_serial); enable(); Quit(); } } message[10]=(unsigned char)message[10]+1; if(message[10]>\'9\')//秒 { message[10]=\'0\'; message[9]=(unsigned char)message[9]+1; if(message[9]>\'5\')//秒 { message[9]=\'0\'; message[7]=(unsigned char)message[7]+1; if(message[7]>\'9\')//分 { message[7]=\'0\'; message[6]=(unsigned char)message[6]+1; if(message[6]>\'5\')//分 { message[6]=\'0\'; message[4]=(unsigned char)message[4]+1; if(message[3] == \'2\') { if(message[4]>\'3\')//时 { message[4]=\'0\'; message[3]=(unsigned char)message[3]+1; if(message[3]>\'2\')//时 { message[3]=\'0\'; } } } else { if(message[4]>\'9\')//时 { message[4]=\'0\'; message[3]=(unsigned char)message[3]+1; if(message[3]>\'2\')//时 { message[3]=\'0\'; } } } } } } } for(j=2;j<12;j++) { if(message[j]!=\',\') jj=jj^message[j]; } message[12]=jj; for(n=0;n<13;n++) { send_byte(message[n]); printf(\"%02X\",message[n]); } printf(\"\\n\"); delay(900); } }while(1); /* outportb(COM1+IER,0x00); setvect(0x0C,old_serial); */ } main() { clrscr(); if(!InitBaud(COM1,4800)) goto lp; printf(\"\\n Init Passed !\\n\"); Log_Data(); lp:printf(\"\\n Init Not Passed!\\n\"); disable(); setvect( 0x0C, old_serial ); enable(); getch(); Quit(); } int GetKey(void) { int Key, Low,Hig; Key=bioskey(0); Low=Key&0x00ff; Hig=(Key&0xff00)>>8; return(Low==0 ? Hig+256 : Low); } void Quit(void) { //closegraph(); fcloseall(); exit(0); } :) |
|