C语言串口通信源代码.docx

上传人:b****1 文档编号:1587863 上传时间:2022-10-23 格式:DOCX 页数:12 大小:19.99KB
下载 相关 举报
C语言串口通信源代码.docx_第1页
第1页 / 共12页
C语言串口通信源代码.docx_第2页
第2页 / 共12页
C语言串口通信源代码.docx_第3页
第3页 / 共12页
C语言串口通信源代码.docx_第4页
第4页 / 共12页
C语言串口通信源代码.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

C语言串口通信源代码.docx

《C语言串口通信源代码.docx》由会员分享,可在线阅读,更多相关《C语言串口通信源代码.docx(12页珍藏版)》请在冰豆网上搜索。

C语言串口通信源代码.docx

C语言串口通信源代码

#include

#include

#include

#include

#defineCOM232 0x2f8

#defineCOMINT 0x0b

#defineMaxBufLen     500

#definePort8259      0x20

#defineEofInt       0x20

staticint comportaddr;

staticcharintvectnum;

staticunsignedcharmaskb;

staticunsignedcharBuffer[MaxBufLen];

staticint CharsInBuf,CircIn,CircOut;

staticvoid(interruptfar*OldAsyncInt)();

staticvoidinterruptfarAsyncInt(void);

voidInit_COM(intComPortAddr,unsignedcharIntVectNum,intBaud,

      unsignedcharData,unsignedcharStop,unsignedcharParity)

{

 unsignedcharHigh,Low;

 intf;

 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);

 

 Data=(Data-5)|((Stop-1)*4);

 if(Parity==2)Data=Data|0x18;

 elseif(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);

 if(IntVectNum==0x0c)outp(Port8259+1,maskb&0xef);

 elseoutp(Port8259+1,maskb&0xf7);              

}

staticvoidinterruptfarAsyncInt(void)

{

 disable();

 if(CharsInBuf

 Buffer[CircIn]=inp(comportaddr);

 if(CircIn

 elseCircIn=0;

 if(CircIn==CircOut)CircOut++;

 elseCharsInBuf++;

 enable();

 outp(Port8259,EofInt);

}

voidRestore(void)

{

 setvect(intvectnum,OldAsyncInt);

 outp(Port8259+1,maskb); 

}

intGetCharInBuf(unsignedchar*Char)

{

 intFlag;

 Flag=-1;

 if(CharsInBuf>0)

 {

  (*Char)=Buffer[CircOut];

  if(CircOut

  elseCircOut=0;

  CharsInBuf--;

  Flag=0;

 }

  returnFlag;

}

intSendChar(unsignedcharChar)

{

 if((inp(comportaddr+5)&0x20)==0)return-1;

 outp(comportaddr,Char);

 return0;

}

main()

{

 inti,c;

 unsignedcharInChar;

 Init_COM(COM232,COMINT,1200,8,1,0);

 while

(1)

 {

  if(kbhit())

    {if((InChar=getch())==27)break;

     elsewhile(SendChar(InChar));

    }

  if(GetCharInBuf(&InChar)==0) 

     printf("%c",InChar);

 }

Restore();

}

 

接收程序:

  #include

  #include

  #include

  #include

  #include

  #include

  #defineRXD0//接收

  #defineTXD0//发送

  #defineLSB0//波特率调节低8位

  #defineMSB1//波特率调节高8位

  #defineIER1//中断起用寄存器

  #defineIIR2//中断标识寄存器

  #defineLCR3//线路控制寄存器

  #defineMCR4//调制解调器控制寄存器

  #defineLSR5//线路状态寄存器

  #defineMSR6//调制解调器状态寄存器

  #defineIERV1

  #defineOUT20x08

  #defineERTS2

  #defineEDTR1

  #defineEMPTY0X20

  #defineREADY0X30

  #defineICREG0X20

  #defineIMASKREG0X21

  #defineEOI0X20

  #defineWAITCOUNT5000

  #defineBUFFLEN2048//用于存储字符的数组的界

  #defineALTE0X12

  #defineALTQ0X10

  #defineSER_BAUD_120096 

  #defineSER_BAUD_240048

  #defineSER_BAUD_96000x0C

  #defineSER_BAUD_192006

  #defineSER_STOP_10/*/1stopbitpercharacter*/

  #defineSER_STOP_24/*/2stopbitspercharacter*/

  #defineSER_BITS_50/*/send5bitcharacters*/

  #defineSER_BITS_61/*/send6bitcharacters*/

  #defineSER_BITS_72/*/send7bitcharacters*/

  #defineSER_BITS_83/*/send8bitcharacters*/

  #defineSER_PARITY_NONE0/*/noparity*/

  #defineSER_PARITY_ODD8/*/oddparity*/

  #defineSER_PARITY_EVEN24/*/evenparity*/

  intport;

  intComNum;

  unsignedportaddr;

  unsignedportf;

  unsignedintbaudtable[]={0x180,0xc0,0x60,0x30,0x18,0x0c,0x06};

  unsignedcharparitytable[]={0x08,0x18,0x00,0x28,0x38};

  unsignedcharbuffer[BUFFLEN];//recvbuf

  intbuffin=0;

  intbuffout=0;

  intincount=0;

  intoutcount=0;

  voidinterrupt(*vect_com)(...);

  voidputb(unsignedcharch)//writeachartotherecvbuf将中断得到的数据写到缓冲区

  {

  inttemp;

  temp=buffin;

  if(++buffin==BUFFLEN)

  buffin=0;

  if(buffin!

=buffout)

  {buffer[buffin]=ch;

  //printf("bufferin[%d]=%c",buffer[buffin]);

  //getch();

  }

  else

  buffin=temp;

  };

  unsignedchargetb()//readacharfromtherecvbuf

  {

  if(buffout!

=buffin)

  {

  if(++buffout==BUFFLEN)

  buffout=0;

  //printf("bufferout[%d]=%c",buffout,buffer[buffout]);

  return(buffer[buffout]);

  }

  else

  return(0xff);

  };

  /*unsignedcharsender(unsignedcharch)

  {

  outportb(portaddr2+TXD,ch);

  printf("\nsenderoutportdata=%c\n",ch);

  outcount++;

  return

(1);

  };

  */

  voidinterruptreceiver(...)

  {

  unsignedcharch;

  ch=inportb(portaddr+RXD);

  putb(ch);

  incount++;//记录接收了多少个数据。

  outportb(ICREG,EOI);

  };

  voidSerInit(intbaud,intconfiguration)

  {

  disable();

  outportb(portaddr+LCR,0x80);

  outportb(portaddr+LSB,baud);

  outportb(portaddr+MSB,0x00);

  outportb(portaddr+LCR,configurati

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 计算机软件及应用

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1