网页:
https://msdn.microsoft.com/en-us/library/aa363436(VS.85).aspx
SetCommState 函数
根据设备控制块(device-control block (a DCB structure))的规范,配置通信设备。
这个函数重新初始化所有的硬件和控制设置。
但是不会清空输出和输入队列。
BOOL WINAPI SetCommState(
_In_ HANDLE hFile,
_In_ LPDCB lpDCB
);
参数:
hFile [in]
指向通信设备的句柄。CreateFile函数 返回的 句柄。
A handle to the communications device. The CreateFile function returns this handle.
lpDCB [in]
DCB结构体指针,该结构体包含对通信设备的配置信息。
Return value
函数成功,返回非0
函数失败,返回0
Remarks
SetCommState 函数 使用 DCB 结构体 指定希望的配置
GetCommState 函数 返回当前的配置
可以使用GetCommState 返回一个DCB结构体,然后修改需要修改的几个DCB结构体的变量。
这样可以确保DCB结构体中的其他的成员变量有合适的值。
如果DCB结构体的XonChar成员变量和XoffChar成员变量相等,SetCommState函数返回失败。
当SetCommState用来配置8250的时候,对于DCB结构体中的ByteSize 和StopBits 成员函数有如下的限制:
数据的位数必须是5到8位。
Examples
For an example, see Configuring a Communications Resource.
// serialTC.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#if 0
myDCB.BaudRate = CBR_9600; // 设置波特率9600
myDCB.fBinary = TRUE; // 设置二进制模式,此处必须设置TRUE
myDCB.fParity = FALSE; // 支持奇偶校验
myDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
myDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
myDCB.fDtrControl = DTR_CONTROL_DISABLE; // No DTR flow control
myDCB.fDsrSensitivity = FALSE; // DSR sensitivity
myDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
myDCB.fOutX = FALSE; // No XON/XOFF out flow control
myDCB.fInX = FALSE; // No XON/XOFF in flow control
myDCB.fErrorChar = FALSE; // Disable error replacement
myDCB.fNull = FALSE; // Disable null stripping
myDCB.fRtsControl = RTS_CONTROL_DISABLE; //No RTS flow control
myDCB.fAbortOnError = FALSE; // 当串口发生错误,并不终止串口读写
myDCB.ByteSize = 8; // 数据位,范围:4-8
myDCB.Parity = NOPARITY; // 校验模式
myDCB.StopBits = 0; // 1位停止位
#endif
void PrintCommState(DCB dcb)
{
// Print some of the DCB structure values
_tprintf( TEXT("\n BaudRate = %d, \n ByteSize = %d, \n Parity = %d, \n StopBits = %d\n"),
dcb.BaudRate,
dcb.ByteSize,
dcb.Parity,
dcb.StopBits );
_tprintf( TEXT("\n fBinary = %d, \n fParity = %d, \n fOutxCtsFlow = %d, \n fDtrControl = %d\n"),
dcb.fBinary,
dcb.fParity,
dcb.fOutxCtsFlow,
dcb.fDtrControl );
_tprintf( TEXT("\n fDsrSensitivity = %d, \n fTXContinueOnXoff = %d, \n fOutX = %d, \n fInX = %d\n"),
dcb.fDsrSensitivity,
dcb.fTXContinueOnXoff,
dcb.fOutX,
dcb.fInX );
_tprintf( TEXT("\n fErrorChar = %d, \n fNull = %d, \n fRtsControl = %d, \n fAbortOnError = %d\n"),
dcb.fErrorChar,
dcb.fNull,
dcb.fRtsControl,
dcb.fAbortOnError );
printf("\r\n\r\n\r\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
TCHAR *pcCommPort = TEXT("COM6"); // Most systems have a COM1 port
// Open a handle to the specified com port.
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // default security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL ); // hTemplate must be NULL for comm devices
if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}
// Initialize the DCB structure.
SecureZeroMemory(&dcb, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
// Build on the current configuration by first retrieving all current
// settings.
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}
PrintCommState(dcb); // Output to console
// Fill in some DCB values and set the com state:
// 57,600 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = CBR_9600; // baud rate
dcb.ByteSize = 8; // data size, xmit and rcv
dcb.Parity = NOPARITY; // parity bit
dcb.StopBits = ONESTOPBIT; // stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}
// Get the comm config again.
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}
PrintCommState(dcb); // Output to console
_tprintf (TEXT("Serial port %s successfully reconfigured.\n"), pcCommPort);
return (0);
}
运行结果:
BaudRate = 9600,
ByteSize = 8,
Parity = 0,
StopBits = 0
fBinary = 1,
fParity = 0,
fOutxCtsFlow = 0,
fDtrControl = 1
fDsrSensitivity = 0,
fTXContinueOnXoff = 0,
fOutX = 0,
fInX = 0
fErrorChar = 0,
fNull = 0,
fRtsControl = 1,
fAbortOnError = 0
BaudRate = 9600,
ByteSize = 8,
Parity = 0,
StopBits = 0
fBinary = 1,
fParity = 0,
fOutxCtsFlow = 0,
fDtrControl = 1
fDsrSensitivity = 0,
fTXContinueOnXoff = 0,
fOutX = 0,
fInX = 0
fErrorChar = 0,
fNull = 0,
fRtsControl = 1,
fAbortOnError = 0
Serial port COM6 successfully reconfigured.
请按任意键继续. . .