串口流控制DCB结构体解析及设置

时间:2023-01-22 15:40:27

一、串口通信结构体意义解析:

typedef struct _DCB

{ DWORD DCBlength;

DWORD BaudRate; //波特率

DWORD fBinary :1;

DWORD fParity :1; //是否奇偶校验

DWORD fOutxCtsFlow :1; // CTS output flow control 指定CTS是否用于检测发送控制。当为TRUE时CTS为OFF,发送将被挂起。(发送清除)

DWORD fOutxDsrFlow :1; // DSR output flow control 指定DSR是否用于检测发送控制。(数据装备好) 当为TRUE是DSR为OFF,发送将被挂起。

DWORD fDtrControl :2; // DTR flow control type
//DTR_CONTROL_DISABLE值将DTR置为OFF,

//DTR_CONTROL_ENABLE值将DTR置为ON,

//DTR_CONTROL_HANDSHAKE 允许DTR"握手",

DWORD fDsrSensitivity :1; //若为TRUE,通讯驱动程序对DSR信号状态敏感驱动程序将忽略任何接收的字节数除非DSR调制解调器的输入线为高

DWORD fTXContinueOnXoff :1; //为TRUE,输入缓冲区字节已经XoffLim及驱动程序已经发送XoffChar停止接收字节时,仍然继续发送FALSE,输入缓冲区XonLim是空的,及驱动程序已经发送XonChar字符恢复接收字节传输后,才会继续接收。

DWORD fOutX :1; //发送方的行为定义,为TRUE时,接收到XoffChar之后便停止发送,接收到XonChar之后将重新开始发送;

DWORD fInX :1;  //接收方的行为定义,为TRUE时,接收缓冲区接收到代表缓冲区满的XoffLim之后,XoffChar发送出去;接收缓冲区空的Buffer达到XonLim之后,XonChar发送出去。

DWORD fErrorChar :1;

DWORD fNull :1;

DWORD fRtsControl :2; // RTS Control Flow

//RTS_CONTROL_DISABLE时,RTS置为OFF
//RTS_CONTROL_ENABLE时, RTS置为ON
//RTS_CONTROL_HANDSHAKE时,
//当接收缓冲区小于半满时RTS为ON
//当接收缓冲区超过四分之三满时RTS为OFF
//RTS_CONTROL_TOGGLE时,
//当接收缓冲区仍有剩余字节时RTS为ON ,否则缺省为OFF

DWORD fAbortOnError :1; // abort reads/writes on error,为TRUE时,有错误发生时中止读和写操作

DWORD fDummy2 :17;

WORD wReserved;

WORD XonLim; //指定在XON字符发送之前接收缓冲区中空缓冲区可允许的最小字节数

WORD XoffLim; //指定在XOFF字符发送这前接收缓冲区中数据缓冲可允许的最小字节数

BYTE ByteSize;

BYTE Parity; //奇偶校验方式

BYTE StopBits; //停止位

char XonChar;  //请求发送方继续发送时的字符 0x11

char XoffChar; //请求发送方停止发送时的字符 0x13

char ErrorChar;

char EofChar;

char EvtChar;

WORD wReserved1;

} DCB, *LPDCB;

  

二、设置流控制属性:

           dcb.fDsrSensitivity = FALSE;          

           dcb.fTXContinueOnXoff = FALSE;

            dcb.fRtsControl = RTS_CONTROL_DISABLE;
            dcb.fDtrControl = DTR_CONTROL_ENABLE;

            switch (g_lpInst->flowControl)
            {
                case NoFlowControl:
                {
                    dcb.fOutxCtsFlow = FALSE;
                    dcb.fOutxDsrFlow = FALSE;
                    dcb.fOutX = FALSE;
                    dcb.fInX = FALSE;
                    break;
                }
                case CtsRtsFlowControl:
                {
                    dcb.fOutxCtsFlow = TRUE;
                    dcb.fOutxDsrFlow = FALSE;
                    dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                    dcb.fOutX = FALSE;
                    dcb.fInX = FALSE;
                    break;
                }
                case CtsDtrFlowControl:
                {
                    dcb.fOutxCtsFlow = TRUE;
                    dcb.fOutxDsrFlow = FALSE;
                    dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                    dcb.fOutX = FALSE;
                    dcb.fInX = FALSE;
                    break;
                }
                case DsrRtsFlowControl:
                {
                    dcb.fOutxCtsFlow = FALSE;
                    dcb.fOutxDsrFlow = TRUE;
                    dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                    dcb.fOutX = FALSE;
                    dcb.fInX = FALSE;
                    break;
                }
                case DsrDtrFlowControl:
                {
                    dcb.fOutxCtsFlow = FALSE;
                    dcb.fOutxDsrFlow = TRUE;
                    dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                    dcb.fOutX = FALSE;
                    dcb.fInX = FALSE;
                    break;
                }
                case XonXoffFlowControl:
                {
                    dcb.fOutxCtsFlow = FALSE;
                    dcb.fOutxDsrFlow = FALSE;
                    dcb.fOutX = TRUE;
                    dcb.fInX = TRUE;
                    dcb.XonChar = 0x11;
                    dcb.XoffChar = 0x13;
                    dcb.XoffLim = 100;
                    dcb.XonLim = 100;
                    break;
                }