Chinaunix首页 | 论坛 | 博客
  • 博客访问: 126248
  • 博文数量: 44
  • 博客积分: 2115
  • 博客等级: 大尉
  • 技术积分: 390
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-05 19:58
文章分类

全部博文(44)

文章存档

2012年(4)

2011年(1)

2009年(39)

我的朋友

分类: C/C++

2009-08-19 15:03:10

收集中.......

串口示例


//****************************************************************************
//
// 下面这些函数是使能串口 UART,并从串口发送接收数据,不同的硬件串口,程// 序都是类似的,只要改一下相应的硬件参数即可
//
//****************************************************************************


//****************************************************************************
//
// UARTEnable 设置串口UART,并使能
//
//****************************************************************************
long
UARTEnable(long lPort, long lDataRate, long lDataBits, long lStopBits,
long lParity, long lEvenParity)
{
unsigned char *pucPtr = (unsigned char *)HwBaseAddress;    //硬件的基地址
long lRates[12] = { 115200, 76800, 57600, 38400, 28800, 19200, 14400, 9600,
4800, 2400, 1200, 110 };                                //串口波特率
long lDivisors[12] = { 1, 2, 3, 5, 7, 11, 15, 23, 47, 95, 191, 2094 };
long lIdx, lConfig;


//
// 赋波特率值
//
for(lIdx = 0; lIdx < 12; lIdx++)
{
if(lRates[lIdx] == lDataRate)
{
break;
}
}
if(lIdx == 12)
{
return(0);
}
lConfig = lDivisors[lIdx];

//
// 设置有效的数据位宽度
//
switch(lDataBits)
{
case 5:
{
lConfig |= HwUartControlDataLength5;
break;
}

case 6:
{
lConfig |= HwUartControlDataLength6;
break;
}

case 7:
{
lConfig |= HwUartControlDataLength7;
break;
}

case 8:
{
lConfig |= HwUartControlDataLength8;
break;
}

default:
{
return(0);
}
}

//
// 设置停止位个数
//
if(lStopBits == 2)
{
lConfig |= HwUartControlTwoStopBits;
}
else if(lStopBits != 1)
{
return(0);
}

//
// 设置奇偶位校验
//
if(lParity)
{
lConfig |= HwUartControlParityEnable;

//
// 偶数位
//
if(lEvenParity)
{
//
// 改变奇数位为偶数位 (默认是奇数位).
//
lConfig |= HwUartControlParityEven;
}
}

//
// 设置,使能 UART.
//

//
// 关闭 RTS.
//
// pucPtr[HwPortB] &= ~HwPortBRTS;

//
// 打开 UART.
//
*((unsigned long *)(pucPtr + HwControl)) |= HwControlUartEnable;

//
// 设置 UART.
//
*((unsigned long *)(pucPtr + HwUartControl)) =
lConfig | HwUartControlFifoEnable;

}


//****************************************************************************
//
// UARTDisable 关闭 UART.
//
//****************************************************************************
void
UARTDisable(long lPort)
{
unsigned char * volatile pucPtr = (unsigned char *)HwBaseAddress;


//
// 如果UART已经关闭,返回
//
if(!lPort1Enabled)
{
return;
}

//
// 检查传送数据的 FIFO 是否为空,若不为空,循环等待.
//
while(*((unsigned long *)(pucPtr + HwStatus)) & HwStatusUartTxBusy)
{
}
//
// 关闭 UART.
//
*((unsigned long *)(pucPtr + HwControl)) &= ~HwControlUartEnable;
//
// 标记 UART 为关 
//
lPort1Enabled = 0;
}
}

//****************************************************************************
//
// UARTSendChar 发送一个字符到串口 UART.
//
//****************************************************************************
void
UARTSendChar(long lPort, char cChar)
{
unsigned char * volatile pucPtr = (unsigned char *)HwBaseAddress;

//
// 循环等待直到传送数据的UART FIFO 为空.
//
while(*((unsigned long *)(pucPtr + HwStatus)) & HwStatusUartTxFifoFull)
{
}
//
// 写字符到串口 UART.
//
pucPtr[HwUartData] = cChar;
}


//****************************************************************************
//
// UARTReceiveChar 从串口 UART 接收字符
//
//****************************************************************************
char
UARTReceiveChar(long lPort)
{
unsigned char * volatile pucPtr = (unsigned char *)HwBaseAddress;
//
// 循环等待直到接收数据的UART FIFO 内有数据
//
while(*((unsigned long *)(pucPtr + HwStatus)) &
HwStatusUartRxFifoEmpty)
{
}
//
// 从串口 UART 读出数据,并返回
//
return(pucPtr[HwUartData]);

}

//****************************************************************************
//
// UARTCharReady 判断是否在串口有数据等待接收
//
//****************************************************************************
long
UARTCharReady(long lPort)
{
unsigned char * volatile pucPtr = (unsigned char *)HwBaseAddress;

//
// 检查是否有数据在UART FIFO 等待
//
if(*((unsigned long *)(pucPtr + HwStatus)) & HwStatusUartRxFifoEmpty)
{
//
// 没有数据,返回 0
//
return(0);
}

//
// 有数据,返回 1
//
return(1);

************************************************************************************************************************************************
串口的C编程


#ifndef _RS232_H

#define _RS232_H

#include 

#include 

#include 

 

class RS232 {

   private:

    HANDLE  Comdrv;

    DWORD   Baudrate(long baudrate);

   public:

    int    Open(char *name,int Insize,int Outsize,long baudrate,int bytesize,int parity,int stopbit);

    int    Close(void);

    int    Clear(void);

    int    ClearIn(void);

    int    Length(void);

    DWORD  ModemStatus(void);

    int    Read(char *buffer,int len);

    int    Write(char *buffer,int len);

};

 

DWORD RS232::Baudrate(long baudrate)

{  switch (baudrate)

   {  case 110    : return CBR_110;

      case 300    : return CBR_300;

      case 600    : return CBR_600;

      case 1200   : return CBR_1200;

      case 2400   : return CBR_2400;

      case 4800   : return CBR_4800;

      case 9600   : return CBR_9600;

      case 14400  : return CBR_14400;

      case 19200  : return CBR_19200;

      case 38400  : return CBR_38400;

      case 56000  : return CBR_56000;

      case 57600  : return CBR_57600;

      case 115200 : return CBR_115200;

      case 128000 : return CBR_128000;

      case 256000 : return CBR_256000;

   }

   return CBR_9600;

}

 

// MS_CTS_ON  The CTS (clear-to-send) signal is on.

//  MS_DSR_ON The DSR (data-set-ready) signal is on.

//  MS_RING_ON       The ring indicator signal is on.

//  MS_RLSD_ON       The RLSD (receive-line-signal-detect) signal is on.

DWORD RS232::ModemStatus(void)

{  DWORD stat;

   if (GetCommModemStatus(Comdrv,&stat)) return stat;

   return 0L;

}

 

int RS232::Length(void)

{  COMSTAT comstate;

   DWORD   error;

   ClearCommError(Comdrv,&error,&comstate);

   if (error==0) return (int)comstate.cbInQue;

   return -1;

}

 

int RS232::Open(char *name,int Insize,int Outsize,long baudrate,int bytesize,int parity,int stopbit)

{  COMMTIMEOUTS commtmout;

   DCB          comdcb;

   Comdrv=CreateFile(name,GENERIC_READ+GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

   if (Comdrv==INVALID_HANDLE_VALUE) return 0;

   SetCommMask(Comdrv,EV_RXCHAR);

   SetupComm(Comdrv,Insize,Outsize);

   commtmout.ReadIntervalTimeout=0xFFFFFFFF;

   commtmout.ReadTotalTimeoutMultiplier=0;

   commtmout.ReadTotalTimeoutConstant=0;

   commtmout.WriteTotalTimeoutMultiplier=0;

   commtmout.WriteTotalTimeoutConstant=5000;

   SetCommTimeouts(Comdrv,&commtmout);

   GetCommState(Comdrv,&comdcb);

   comdcb.BaudRate=Baudrate(baudrate);

   comdcb.ByteSize=bytesize; //8;

   comdcb.Parity=parity; //NOPARITY;

   comdcb.StopBits=stopbit; //ONESTOPBIT;

   SetCommState(Comdrv,&comdcb);

   return 1;

}

 

int RS232::Close(void)

{  if (CloseHandle(Comdrv)) return 1;

   return 0;

}

 

int RS232::Read(char *buffer,int len)

{  DWORD nread;

   if (ReadFile(Comdrv,buffer,len,&nread,NULL)) return (int)nread;

   return -1;

}

 

int RS232::Write(char *buffer,int len)

{  DWORD nwrite;

   if (WriteFile(Comdrv,buffer,len,&nwrite,NULL)) return (int)nwrite;

   return -1;

}

 

int RS232::Clear(void)

{  if (PurgeComm(Comdrv,PURGE_TXCLEAR|PURGE_RXCLEAR)) return 1;

   return 0;

}

 

int RS232::ClearIn(void)

{  if (PurgeComm(Comdrv,PURGE_RXCLEAR)) return 1;

   return 0;

}
 

#pragma pack()

#endif
阅读(642) | 评论(0) | 转发(0) |
0

上一篇:串口API通信函数编程

下一篇:C++/C试题

给主人留下些什么吧!~~