comポートを構成しようとすると、dcbの設定が失敗します
-
10-10-2019 - |
質問
シリアルポート(COM8など)を使用するC ++ MFCアプリケーションを作成しようとしています。 DCBを設定しようとするたびに失敗します。誰かが私が間違っていることを指摘できれば、本当に感謝しています。
DCB dcb = {0};
dcb.DCBlength = sizeof(DCB);
port.Insert( 0, L"\\\\.\\" );
m_hComm = CreateFile(
port, // Virtual COM port
GENERIC_READ | GENERIC_WRITE, // Access: Read and write
0, // Share: No sharing
NULL, // Security: None
OPEN_EXISTING, // The COM port already exists.
FILE_FLAG_OVERLAPPED, // Asynchronous I/O.
NULL // No template file for COM port.
);
if ( m_hComm == INVALID_HANDLE_VALUE )
{
TRACE(_T("Unable to open COM port."));
ThrowException();
}
if ( !::GetCommState( m_hComm, &dcb ) )
{
TRACE(_T("CSerialPort : Failed to get the comm state - Error: %d"), GetLastError());
ThrowException();
}
dcb.BaudRate = 38400; // Setup the baud rate.
dcb.Parity = NOPARITY; // Setup the parity.
dcb.ByteSize = 8; // Setup the data bits.
dcb.StopBits = 1; // Setup the stop bits.
if ( !::SetCommState( m_hComm, &dcb ) ) // <- Fails here.
{
TRACE(_T("CSerialPort : Failed to set the comm state - Error: %d"), GetLastError());
ThrowException();
}
ありがとう。
追加情報: 生成されたエラーコードは87です:「パラメーターが正しくありません。」おそらくマイクロソフトのもの 多くの 便利なエラーコード。 j/k
解決 3
使用を使用して問題を解決することができました BuildCommDCB
:
DCB dcb = {0};
if ( !::BuildCommDCB( _T("baud=38400 parity=N data=8 stop=1"), &dcb ) )
{
TRACE(_T("CSerialPort : Failed to build the DCB structure - Error: %d"), GetLastError());
ThrowException();
}
他のヒント
私のお金はこれにあります:
dcb.StopBits = 1;
MSDNドキュメント 停止についてこれを言っています:
使用するストップビットの数。このメンバーは、次の値のいずれかになります。
ONESTOPBIT 0 1 stop bit. ONE5STOPBITS 1 1.5 stop bits. TWOSTOPBITS 2 2 stop bits.
だから、あなたは1.5ストップビットを求めています。これは、どこから来たのか覚えていない非常に恐ろしく古風なことです。テレプリンター、おそらく。
このモードをサポートするドライバー/ハードウェアの可能性はスリムであるため、エラーがあると思います。
だから、それを変更してください dcb.StopBits = ONESTOPBIT;
ここに、いくつかの可能性が順調にありません。
GetCommState
ポートがまだ初期化されていないため、構造をごみで埋めています。このステップをスキップするだけです。- パリティ設定を制御する2つのパラメーターがあり、無効な組み合わせがあるかどうかは明確ではありません。
- ストップビットの値はビット数ではなく、魔法の数の定数です。値1が相当します
ONE5STOPBITS
他のパラメーターと組み合わせると無効になる可能性があります。
これは私のコードであり、それがうまく機能しています。
/* Try to open the port */
hCom = CreateFile(szPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (hCom != INVALID_HANDLE_VALUE) {
printf("Handle success\n");
}
dcb = { 0 };
dcb.DCBlength = sizeof(dcb);
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess) {
// Handle the error.
printf("GetCommState failed with error %d.\n", GetLastError());
CloseHandle(hCom);
return APP_ERROR;
}
// Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.
dcb = { 0 };
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = CBR_115200; // Set the baud rate
dcb.ByteSize = 8; // Data size, xmit, and rcv
dcb.Parity = NOPARITY; // No parity bit
dcb.StopBits = ONESTOPBIT; // One stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess) {
// Handle the error.
printf("SetCommState failed with error %d.\n", GetLastError());
CloseHandle(hCom);
return APP_ERROR;
}
}
printf("Serial port successfully reconfigured.\n");
関数に与えるパラメーターを見てください。エラーコードが示すように、それらはおそらく間違っています。 「SetCommstate 87」のGoogle検索では、パラメーター(ボーレートなど)がシリアルポートと互換性がないいくつかのインスタンスを示しています。
所属していません StackOverflow