C语言串口通信范例

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
disable(); OldAsyncInt=getvect( IntVectNum ); setvect( IntVectNum, AsyncInt ); enable();
maskb=inp(Port8259+1); if(IntVectNum==0x0c)outp(Port8259+1,maskb&0xef); else outp(Port8259+1,maskb&0xf7); }
Data=(Data-5)|((Stop-1)*4); if(Parity==2) Data=Data|0x18; else if(Parity==1) Data=Data|0x8; outp(ComPortAddr+3,Data);
outp(ComPortAddr+4,0x0a);
outp(ComPortAddr+1,0x01);
outp(Port8259,EofInt); }
void Restore(void) {
setvect(intvectnum,OldAsyncInt); outp(Port8259+1,maskb); } int GetCharInBuf(unsigned char *Char) { int Flag;
一个 c 语言的串口通信程序范例
标签: 分类: 技术笔记
c 语言
串口通信
通信程序
it
最近接触一个项目,用 HL-C1C 激光位移传感器+易控组态软件完成生产线高度跳 变检测,好久没有接触 c c#,一些资料,找来做个记录,也许大家用的着
#include <stdio.h> #include <dos.h> #include <conio.h> #include <string.h>
static void interrupt far AsyncInt(void) {
disable();
if(CharsInBuf<MaxBufLen) Buffer[CircIn]=inp(comportaddr); if(CircIn<MaxBufLen-1) CircIn++; else CircIn=0; if(CircIn==CircOut) CircOut++; else CharsInBuf++; enable();
buffin=0; if(buffin!=buffout)
{buffer[buffin]=ch; // printf("bufferin[%d]=%c",buffer[buffin]); // getch(); } else buffin=temp; }; unsigned char getb()//read a char from the recvbuf { if(buffout!=buffin) { if(++buffout==BUFFLEN) buffout=0; //printf("bufferout[%d]=%c",buffout,buffer[buffout]); return(buffer[buffout]); } else return(0xff);
outportb(portaddr+MCR,0); outportb(IMASKREG,inportb(IMASKREG)|(1<<portf)); enable(); setvect(portf+8,vect_com); }; void Getportaddr(int port) {
ቤተ መጻሕፍቲ ባይዱ{ unsigned char High,Low; int f;
comportaddr=ComPortAddr; 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);
#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_NONE 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 buffer[BUFFLEN];//recv buf int 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)
if(kbhit()) {if((InChar=getch())==27)break; else while(SendChar(InChar)); }
if(GetCharInBuf(&InChar)==0)
printf("%c",InChar); } Restore(); }
接收程序: #include <dos.h> #include <fstream.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #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 #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_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*/
}; /*unsigned char sender( unsigned char ch) { outportb(portaddr2+TXD,ch); printf("\nsender outportdata=%c\n",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(portaddr+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(IMASKREG)&(~(1<<portf))); setvect(portf+8,receiver); enable(); }; void SerClose(void) { disable(); outportb(portaddr+IER,0);
static void (interrupt 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)
Flag=-1;
if(CharsInBuf>0) {
(*Char)=Buffer[CircOut]; if(CircOut<MaxBufLen-1)CircOut++; else CircOut=0; CharsInBuf--; Flag=0; } return Flag; }
int SendChar(unsigned char Char) {
if((inp(comportaddr+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) {
#define COM232 0x2f8 #define COMINT 0x0b
#define MaxBufLen #define Port8259 #define EofInt
500 0x20 0x20
static int comportaddr; static char intvectnum; static unsigned char maskb; static unsigned char Buffer[MaxBufLen]; static int CharsInBuf,CircIn,CircOut;
相关文档
最新文档