RS232串口应用资料
已有 1954 次阅读2005-10-9 17:09
|个人分类:接口实验田|系统分类:接口电路
RS232串口应用资料(PC端)
一、接口说明
DB9 DB25 符号 I/O属性 相应位置 说明
1 8 DCD <- MSR.7 Data Carrier Detect
2 3 RXD <- Receive Data
3 2 TXD -> Transmit Data
4 20 DTR -> MCR.0 Data Terminal Ready
5 7 GND --
6 6 DSR <- MSR.5 Data Set Ready
7 4 RTS -> MCR.1 Ready To Send
8 5 CTS <- MSR.4 Clear To Send
9 22 RI <- MSR.6 Ring Indicator
最大的信号电压范围-15V和15V
输出5~15V代表0,输出-15~-5V代表1,-5~5V之间的输出是未定义的
输入3~15V代表0,输入-15~-3V代表1,-3~3V之间的输入是不正确的输入
二、寄存器说明
一般PC串口地址(HEX),可通过调整CMOS更改
COM1-3F8,COM2-2F8,COM3-3E8,COM4-2E8
以COM1为例说明寄存器
地址 寄存器 说明
3F8 RBR Reciver Buffer Register (DLAB==0,读)
THR Transmit Holding Register (DLAB==0,写)
DLL Data Latch LSB (DLAB==1)
3F9 IER Interrupt Enable Register (DLAB==0)
DLM Data Latch MSB (DLAB==1)
3FA IIR Interrupt Identify Register (读)
FCR 16550 FIFO Control Register (写)
3FB LCR Line Control Register (DLAB=LCR.7)
3FC MCR Modem Control Register
3FD LSR Line Status Register (只读)
3FE MSR Modem Status Register (只读)
3FF Scratch Pad Register
三、应用API编程的简单例子
DCB ComDCB;
HANDLE hComm;
DWORD BytesRead;
COMSTAT ComStat;
DWORD dwErrorFlags;
unsigned char SendBytes[100];//发送缓冲区
unsigned char ucRxBuff[100]; //接收缓冲区
hComm = CreateFile("COM1",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,0);
if (hComm < 0)
{
MessageBox(Form1->Handle,"串行端口初始化错误!","ERROR", MB_OK);
Application->Terminate();return;
};
GetCommState(hComm,&ComDCB);
BuildCommDCB("9600,n,8,2",&ComDCB); //9600,无校验,8位,两个停止位
SetCommState(hComm,&ComDCB);
PurgeComm(hComm, PURGE_TXABORT| PURGE_RXABORT | PURGE_TXCLEAR| PURGE_RXCLEAR ) ; //清收发缓冲区
WriteFile(hComm,SendBytes,5,&BytesSent,NULL); //发送5个字节
Sleep(20);
ClearCommError(hComm,&dwErrorFlags,&ComStat);
if (ReadFile(hComm,ucRxBuff,ComStat.cbInQue,&BytesRead,NULL) && (BytesRead >= 10))
{//收到10个字节处理一下
};