




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、一个c语言的串口通信程序范例标签: c语言串口通信通信程序it分类: 技术笔记 最近接触一个项目,用HL-C1C激光位移传感器+易控组态软件完成生产线高度跳变检测,好久没有接触c c#,一些资料,找来做个记录,也许大家用的着#include <stdio.h>#include <dos.h>#include <conio.h>#include <string.h>#define COM232 0x2f8#define COMINT 0x0b#define MaxBufLen &
2、#160; 500#define Port8259 0x20#define EofInt 0x20static int comportaddr;static char intvectnum;static unsigned char maskb;static unsigned char BufferMaxBufLen;static int CharsInBuf,CircIn,CircOut;static void (i
3、nterrupt far *OldAsyncInt)();static void interrupt far AsyncInt(void);void Init_COM(int ComPortAddr, unsigned char IntVectNum, int Baud, unsigned char Data, unsigned char Stop, unsigned char Parity) unsigned char High,Low; int f; comportaddr=ComPort
4、Addr; intvectnum=IntVectNum; CharsInBuf=0;CircIn=0;CircOut=0; f=(Baud/100); f=1152/f; High=f/256; Low=f-High*256; outp(ComPortAddr+3,0x80); outp(ComPortAddr,Low); outp(ComPortAddr+1,High); Data=(Data-5)|(Stop-1)*4); if(Parity=2) Data=Data
5、|0x18; else if(Parity=1) Data=Data|0x8; outp(ComPortAddr+3,Data); outp(ComPortAddr+4,0x0a); outp(ComPortAddr+1,0x01); disable(); OldAsyncInt=getvect( IntVectNum ); setvect( IntVectNum, AsyncInt ); enable(); maskb=inp(Port8259+1);
6、160;if(IntVectNum=0x0c)outp(Port8259+1,maskb&0xef); else outp(Port8259+1,maskb&0xf7); static void interrupt far AsyncInt(void) disable(); if(CharsInBuf<MaxBufLen) BufferCircIn=inp(compo
7、rtaddr); if(CircIn<MaxBufLen-1) CircIn+; else CircIn=0; if(CircIn=CircOut) CircOut+; else CharsInBuf+; enable(); outp(Port8259,EofInt);void Restore(void) setvect(intvectnum,OldAsyncInt); outp(Port8259+1,maskb); int GetCharInBuf(unsigned char *Char)
8、0;int Flag; Flag=-1; if(CharsInBuf>0) (*Char)=BufferCircOut; if(CircOut<MaxBufLen-1)CircOut+; else CircOut=0; CharsInBuf-; Flag=0; return Flag;int SendChar(unsigned char Char) if(inp(comportadd
9、r+5)&0x20)=0) return -1; outp(comportaddr,Char); return 0;main() int i,c; unsigned char InChar; Init_COM(COM232,COMINT,1200,8,1,0); while(1) if(kbhit() if(InChar=getch()=27)break; else while(Se
10、ndChar(InChar); if(GetCharInBuf(&InChar)=0) printf("%c",InChar); Restore(); 接收程序:#include <dos.h>#include <fstream.h>#include <conio.h>#include <stdio.h>#include <stdlib.h>#
11、include <math.h>#define RXD 0 /接收#define TXD 0 /发送#define LSB 0 /波特率调节低8位#define MSB 1 /波特率调节高8位#define IER 1 / 中断起用寄存器#define IIR 2 /中断标识寄存器#define LCR 3 /线路控制寄存器#define MCR 4 /调制解调器控制寄存器#define LSR 5 /线路状态寄存器#define MSR 6 /调制解调器状态寄存器#define IERV 1#define OUT2 0x08#define ERTS 2#define EDTR 1
12、#define EMPTY 0X20#define READY 0X30#define ICREG 0X20#define IMASKREG 0X21#define EOI 0X20#define WAITCOUNT 5000#define BUFFLEN 2048 /用于存储字符的数组的界#define ALTE 0X12#define ALTQ 0X10#define SER_BAUD_1200 96 #define SER_BAUD_2400 48#define SER_BAUD_9600 0x0C#define SER_BAUD_19200 6#define SER_STO
13、P_1 0 /*/ 1 stop bit per character*/#define SER_STOP_2 4 /*/ 2 stop bits per character*/#define SER_BITS_5 0 /*/ send 5 bit characters*/#define SER_BITS_6 1 /*/ send 6 bit characters*/#define SER_BITS_7 2 /*/ send 7 bit characters*/#define SER_BITS_8 3 /*/ send 8 bit characters*/#define SER_PARITY_N
14、ONE 0 /*/ no parity*/#define SER_PARITY_ODD 8 /*/ odd parity*/#define SER_PARITY_EVEN 24 /*/ even parity*/int port;int ComNum;unsigned portaddr;unsigned portf;unsigned int baudtable=0x180,0xc0,0x60,0x30,0x18,0x0c,0x06;unsigned char paritytable=0x08,0x18,0x00,0x28,0x38;unsigned char bufferBUFFLEN;/re
15、cv bufint buffin=0;int buffout=0;int incount=0;int outcount=0;void interrupt(*vect_com)(.);void putb(unsigned char ch)/write a char to the recvbuf 将中断得到的数据写到缓冲区int temp;temp=buffin;if(+buffin=BUFFLEN) buffin=0;if(buffin!=buffout) bufferbuffin=ch; / printf("bufferin%d=%c",bufferbuffin); / g
16、etch(); else buffin=temp;unsigned char getb()/read a char from the recvbufif(buffout!=buffin) if(+buffout=BUFFLEN) buffout=0; /printf("bufferout%d=%c",buffout,bufferbuffout); return(bufferbuffout); else return(0xff);/*unsigned char sender( unsigned char ch)outportb(portaddr2+TXD,ch);printf
17、("nsender outportdata=%cn",ch);outcount+;return(1);*/void interrupt receiver(.)unsigned char ch;ch=inportb(portaddr+RXD);putb(ch);incount+; /记录接收了多少个数据。outportb(ICREG,EOI);void SerInit(int baud,int configuration)disable();outportb(portaddr+LCR,0x80);outportb(portaddr+LSB,baud);outportb(por
18、taddr+MSB,0x00);outportb(portaddr+LCR,configuration);enable();void SerOpen(void)vect_com=getvect(portf+8);disable();inportb(portaddr+RXD);inportb(portaddr+MSR);inportb(portaddr+LSR);inportb(portaddr+IIR);outportb(portaddr+IER,IERV);outportb(portaddr+MCR,OUT2|ERTS|EDTR);outportb(IMASKREG,inportb(IMAS
19、KREG)&(1<<portf);setvect(portf+8,receiver);enable();void SerClose(void)disable();outportb(portaddr+IER,0);outportb(portaddr+MCR,0);outportb(IMASKREG,inportb(IMASKREG)|(1<<portf);enable();setvect(portf+8,vect_com);void Getportaddr(int port) switch (port) case 0: portaddr=0x3F8; break;
20、 case 1: portaddr=0x2F8; break; case 2: portaddr=0x3E8; break; case 3: portaddr=0x2E8; break; portf=(port=0)?4:3;void Int()unsigned char key,key2;port=ComNum-1;if(peek(0x40,port*2)=0) printf("have no special com .n"); exit(1);else printf("The used port is :COM%dn",ComNum);Getport
21、addr(port); / 得到串口地址SerInit(SER_BAUD_9600,SER_PARITY_EVEN| SER_BITS_8 | SER_STOP_1); /初始化串口,设置波特率等SerOpen();do if(kbhit() key2=getch(); if(key2=27) break; ; key=getb(); if(key!=0xff) printf("%xt",key); FILE *fp; fp=fopen("C:Receivedata.dat","ab"); /可选择接收数据的存放文件路径和文件名 if
22、(fp=NULL) printf("File open error!"); / fputc(key,fp); fwrite(&key,sizeof(unsigned char),1,fp); fclose(fp); while (key2!=27);SerClose();/ printf("%d char has been receivedn",incount);/ printf("%d char has been sendedn",outcount);/ printf("nsum=%d n",sum);v
23、oid main()/com1 receiveprintf("Please input the receive COMnum:(14)n"); cin>>ComNum;printf("the current com set is:9600,even, data bits 8, stop 1,");Int();printf("nnOVERn");exit(0);发送文件程序#include <dos.h>#include <bios.h>#include <stdio.h>#include
24、 <math.h>#include <conio.h>#include <graphics.h>#ifdef _cplusplus #define _CPPARGS .#else #define _CPPARGS#endif#define SER_RBF 0 #define SER_THR 0 #define SER_IER 1 #define SER_IIR 2 #define SER_LCR 3 #define SER_MCR 4 #define SER_LSR 5 #d
25、efine SER_MSR 6 #define SER_DLL 0 #define SER_DLH 1 #define SER_BAUD_1200 96 #define SER_BAUD_2400 48#define SER_BAUD_9600 12#define SER_BAUD_19200 6#define SER_GP02 8 #define COM_1 0x3F8 #define COM_2 0x2F8 /*/ base port address of port 1*/#define COM_3 0x3E8#def
26、ine COM_4 0x2E8#define SER_STOP_1 0 /*/ 1 stop bit per character*/#define SER_STOP_2 4 /*/ 2 stop bits per character*/#define SER_BITS_5 0 /*/ send 5 bit characters*/#define SER_BITS_6 1 /*/ send 6 bit characters*/#define SER_BITS_7 2 /*/ send 7 bit characters*/#define SER_BITS_8 3 /*/ send 8 bit ch
27、aracters*/#define SER_PARITY_NONE 0 /*/ no parity*/#define SER_PARITY_ODD 8 /*/ odd parity*/#define SER_PARITY_EVEN 24 /*/ even parity*/#define SER_DIV_LATCH_ON 128 /*/ used to turn reg 0,1 into divisor latch*/#define PIC_IMR 0x21 /*/ pic's interrupt mask reg.*/#define PIC_ICR 0x20 /*/ pic's
28、 interupt control reg.*/#define INT_SER_PORT_0 0x0C /*/ port 0 interrupt com 1 & 3*/#define INT_SER_PORT_1 0x0B /*/ port 0 interrupt com 2 & 4*/#define SERIAL_BUFF_SIZE 128 /*/ current size of circulating receive buffer*/void interrupt far (*Old_Isr)(_CPPARGS); /*/ holds old com port interru
29、pt handler*/char ser_bufferSERIAL_BUFF_SIZE; /*/ the receive buffer*/int ser_end = -1,ser_start=-1; /*/ indexes into receive buffer*/int ser_ch, char_ready=0; /*/ current character and ready flag*/int old_int_mask; /*/ the old interrupt mask on the PIC*/int open_port; /*/ the currently open port*/in
30、t serial_lock = 0; /*/ serial ISR semaphore so the buffer*/ /*/ isn't altered will it is being written*/ /*/ to by the ISR*/*-写串口-*/ void interrupt far Serial_Isr(_CPPARGS)serial_lock = 1;ser_ch = inp(open_port + SER_RBF);if (+ser_end > SERIAL_BUFF_SIZE-1) ser_end = 0;ser_bufferser_end
31、= ser_ch;+char_ready;outp(PIC_ICR,0x20);serial_lock = 0; int Ready_Serial()return(char_ready); /*-读串口-*/int Serial_Read()int ch;while(serial_lock)if (ser_end != ser_start) if (+ser_start > SERIAL_BUFF_SIZE-1) ser_start = 0; ch = ser_bufferser_start; printf("%x",ch); if (char
32、_ready > 0) -char_ready; return(ch); else return(0); /*-写串口-*/Serial_Write(char ch)while(!(inp(open_port + SER_LSR) & 0x20)asm clioutp(open_port + SER_THR, ch);asm sti/*-初始化串口-*/Open_Serial(int port_base, int baud, int configuration)open_port = port_base;disable();outp(port_base
33、+ SER_LCR, SER_DIV_LATCH_ON);outp(port_base + SER_DLL, baud);outp(port_base + SER_DLH, 0);outp(port_base + SER_LCR, configuration);outp(port_base + SER_MCR, SER_GP02);outp(port_base + SER_IER, 1);if (port_base = COM_1 | port_base=COM_3) Old_Isr = _dos_getvect(INT_SER_PORT_0); _dos_setvect(INT_SER_PO
34、RT_0, Serial_Isr); printf("nOpening Communications Channel Com Port #1/3.n"); else Old_Isr = _dos_getvect(INT_SER_PORT_1); _dos_setvect(INT_SER_PORT_1, Serial_Isr); printf("nOpening Communications Channel Com Port #2/4.n"); old_int_mask = inp(PIC_IMR);outp(PIC_IMR, (port_base=COM
35、_1) ? (old_int_mask & 0xEF) : (old_int_mask & 0xF7 );enable();/*-关闭串口-*/Close_Serial(int port_base)outp(port_base + SER_MCR, 0);outp(port_base + SER_IER, 0);outp(PIC_IMR, old_int_mask );if (port_base = COM_1) _dos_setvect(INT_SER_PORT_0, Old_Isr); printf("nClosing Communications Channel
36、 Com Port #1.n"); else _dos_setvect(INT_SER_PORT_1, Old_Isr); printf("nClosing Communications Channel Com Port #2.n"); /*-发送应用-*/void main(int argc,char *argv)char ch,press;int done=0;FILE *fp;argc=2;/argv1="c:comfile.c"if(argc<2) printf("nUsage:display filename.wav!");/ exit(1);if(fp=fopen(argv1,"r+b")=NULL) printf("cannot open the filen");/ exit(0);fs
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年中国一次性棉褥市场调查研究报告
- 2025年中国PVC后盖胶输送带市场调查研究报告
- 四年级下册数学教案-1.3 小数的意义(三)小数乘法的简便运算-北师大版
- 2025-2031年中国隔热铝型材行业发展概况及行业投资潜力预测报告
- 2025-2031年中国酱类制品行业市场全景监测及投资战略咨询报告
- 2025-2031年中国负载型催化剂行业市场需求预测及投资战略规划报告
- 2025-2031年中国第三方环境检测行业市场发展监测及投资策略研究报告
- 2025-2031年中国特色原料药行业发展前景预测及投资方向研究报告
- 第四单元信息窗1《圆柱和圆锥的认识》(教案)-五年级数学下册青岛版(五四学制)
- 零碳排放商业中心行业跨境出海战略研究报告
- 介入手术术中安全护理措施
- 高中语文整本书阅读教学研究
- 2024年苏州农业职业技术学院高职单招语文历年参考题库含答案解析
- 投资银行学第4版- 课件汇 马晓军 第1-4章 投资银行概述-上市公司再融资
- 2025年月度工作日历含农历节假日电子表格版
- 中国近现代史纲要心得体会
- 竣工结算审计服务投标方案(2024修订版)(技术方案)
- 2025年中考语文复习课件 模块三 语言综合运用
- 《年产2000吨果汁型茶饮料的生产工艺设计》4600字(论文)
- 中建落地式脚手架施工方案
- 基因检测销售培训
评论
0/150
提交评论