win32 SetCommState



Win32 Setcommstate



ウェブページ:

https://msdn.microsoft.com/en-us/library/aa363436(VS.85).aspx

SetCommState関数
デバイス制御ブロック(DCB構造)の仕様に従って通信デバイスを構成します。
この機能は、すべてのハードウェアと制御設定を再初期化します。
ただし、出力キューと入力キューはクリアされません。



BOOL WINAPI SetCommState( _In_ HANDLE hFile, _In_ LPDCB lpDCB )

パラメータ:

hFile [in]

通信機器へのハンドル。 CreateFile関数によって返されるハンドル。
通信デバイスへのハンドル。 CreateFile関数はこのハンドルを返します。



lpDCB [in]

DCB構造体ポインタ。この構造体には、通信デバイスの構成情報が含まれています。

戻り値

関数は成功し、ゼロ以外を返します
関数は失敗し、0を返します

備考

SetCommState関数DCB構造体を使用して、目的の構成を指定します
GetCommState関数は、現在の構成を返します
GetCommStateを使用してDCB構造を返し、変更が必要ないくつかのDCB構造の変数を変更できます。
これにより、DCB構造内の他のメンバー変数が適切な値を持つことが保証されます。
DCB構造体のXonCharメンバー変数とXoffCharメンバー変数が等しい場合、SetCommState関数は失敗を返します。
SetCommStateを使用して8250を構成する場合、DCB構造体のByteSizeおよびStopBitsメンバー関数には次の制限があります。
データの桁数は5〜8桁である必要があります。



例については、通信リソースの構成を参照してください。

// serialTC.cpp: Define the entry point of the console application. // #include 'stdafx.h' #include #include #include #if 0 myDCB.BaudRate = CBR_9600 // Set the baud rate 9600 myDCB.fBinary = TRUE // Set the binary mode, TRUE must be set here myDCB.fParity = FALSE // Support parity check 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 // When an error occurs on the serial port, the serial port read and write is not terminated myDCB.ByteSize = 8 // Data bits, range: 4-8 myDCB.Parity = NOPARITY // Check mode myDCB.StopBits = 0 // 1 stop bit #endif void PrintCommState(DCB dcb) { // Print some of the DCB structure values _tprintf( TEXT(' BaudRate = %d, ByteSize = %d, Parity = %d, StopBits = %d '), dcb.BaudRate, dcb.ByteSize, dcb.Parity, dcb.StopBits ) _tprintf( TEXT(' fBinary = %d, fParity = %d, fOutxCtsFlow = %d, fDtrControl = %d '), dcb.fBinary, dcb.fParity, dcb.fOutxCtsFlow, dcb.fDtrControl ) _tprintf( TEXT(' fDsrSensitivity = %d, fTXContinueOnXoff = %d, fOutX = %d, fInX = %d '), dcb.fDsrSensitivity, dcb.fTXContinueOnXoff, dcb.fOutX, dcb.fInX ) _tprintf( TEXT(' fErrorChar = %d, fNull = %d, fRtsControl = %d, fAbortOnError = %d '), dcb.fErrorChar, dcb.fNull, dcb.fRtsControl, dcb.fAbortOnError ) printf(' ') } 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. ', 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. ', 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. ', GetLastError()) return (3) } // Get the comm config again. fSuccess = GetCommState(hCom, &dcb) if (!fSuccess) { // Handle the error. printf ('GetCommState failed with error %d. ', GetLastError()) return (2) } PrintCommState(dcb) // Output to console _tprintf (TEXT('Serial port %s successfully reconfigured. '), 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. Please press any key to continue...